将数组数组值存储在一个逗号分隔的字符串中
I have this return array
$leaseArray = Array ( [0] => Array ( [LeaseNumber] => OL/2011/0343 ) [1] => Array ( [LeaseNumber] => 184 ) [2] => Array ( [LeaseNumber] => OL/2011/0118 ) [3] => Array ( [LeaseNumber] => OL/2016/1759 ) [4] => Array ( [LeaseNumber] => OL/2013/0858 ) [5] => Array ( [LeaseNumber] => OL/2012/0535 ) [6] => Array ( [LeaseNumber] => OL/2017/2208 ) [7] => Array ( [LeaseNumber] => 2355 ) )
I want to save all the values of LeaseNumber in to one comma separated string
like $string = "OL/2011/0343 , 184 , OL/2011/0118 , OL/2016/1759"
please help me
$lease = array();
for($i=0;$i<=count($leaseArray); $i++){
$lease = $leaseArray[$i]['LeaseNumber'];
}
我有这个返回数组 p>
$ leaseArray = Array( [0] =&gt;数组([LeaseNumber] =&gt; OL / 2011/0343)[1] =&gt;数组([LeaseNumber] =&gt; 184)[2] =&gt;数组([LeaseNumber] =&gt; OL / 2011/0118)[3] =&gt;数组([LeaseNumber] =&gt; OL / 2016/1759)[4] =&gt;数组([LeaseNumber] =&gt; OL / 2013/0858)[5] = &gt;数组([LeaseNumber] =&gt; OL / 2012/0535)[6] =&gt;数组([LeaseNumber] =&gt; OL / 2017/2208)[7] =&gt;数组([LeaseNumber] =&gt; 2355))
code> pre>
我想将LeaseNumber的所有值保存到一个逗号分隔的字符串
like $ string =“OL / 2011/0343, 184,OL / 2011/0118,OL / 2016/1759“ code> p>
请帮助我 p>
$ lease = array ();
for($ i = 0; $ i&lt; = count($ leaseArray); $ i ++){
$ lease = $ leaseArray [$ i] ['LeaseNumber'];
}
代码> pre>
div>
If you want a one-liner then this could work:
$csv = implode( ' , ', array_map( function( $a ){ return $a[ 'LeaseNumber' ]; }, $leaseArray ) );
expanded:
$csv = implode( ' , ', // step 2: implode on ' , ': space-comma-space
array_map( // step 1: pass $lease array into array_map so that we can get a new array
function( $a ){ return $a[ 'LeaseNumber' ]; }, // Accept each array in $a and only return the LeaseNumber. array_map will build a new array out of just these values
$leaseArray // the array to be processed
)
);
In essence, this is the same as:
$csv = implode( ' , ', array_column( $a, 'LeaseNumber' ) );
but array_map()
allows you to transform the data before output/return if you need to.
If you want a one-liner then this is the way
$csv = implode(' , ', array_column($a, 'LeaseNumber'));
As I said in the comments, 1 line, 2 function calls.
One-liners are great but sometimes hard to read. If you want to stick with what you have here are some note
<?php
$array = array(array("LeaseNumber" => "OL/2011/0343"), array("LeaseNumber"=> 184 ), array("LeaseNumber"=> "OL/2011/0118") , array("LeaseNumber"=> "OL/2016/1759"), array("LeaseNumber"=> "OL/2013/0858"), array("LeaseNumber"=> "OL/2012/0535"), array("LeaseNumber"=> "OL/2017/2208"), array("LeaseNumber"=> 2355));
$lease = array();
//Not equal to the count it starts at 1 not 0
for($i=0;$i<count($array); $i++){
//lease is an array add to the index not overwrite
$lease[] = $array[$i]['LeaseNumber'];
}
//You needed to finish with this
$csv = implode(", ",$lease);
echo $csv;
As an fyi switch to a foreach in this case makes life easier:
$lease = array();
foreach($array as $obj){
$lease[] = $obj["LeaseNumber"];
}