Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将数组值的数组存储在一个逗号分隔的字符串中_Php_Arrays_String - Fatal编程技术网

Php 将数组值的数组存储在一个逗号分隔的字符串中

Php 将数组值的数组存储在一个逗号分隔的字符串中,php,arrays,string,Php,Arrays,String,我有这个返回数组 $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] => Arra

我有这个返回数组

 $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 ) )
我想将LeaseNumber的所有值保存在一个逗号分隔的字符串中 比如
$string=“OL/2011/0343184,OL/2011/0118,OL/2016/1759”

请帮帮我

$lease = array();
        for($i=0;$i<=count($leaseArray); $i++){
            $lease = $leaseArray[$i]['LeaseNumber'];
        }
$lease=array();

对于($i=0;$i如果您想要一个单行程序,那么这可以工作:

$csv = implode( ' , ', array_map( function( $a ){ return $a[ 'LeaseNumber' ]; }, $leaseArray ) );
扩大:

$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
    )
);

本质上,这与:

$csv = implode( ' , ', array_column( $a, 'LeaseNumber' ) );

但是
array\u map()
允许您在输出/返回之前转换数据,如果需要的话。

如果您想要一个单行程序,那么这就是方法

$csv = implode(' , ', array_column($a, 'LeaseNumber'));

正如我在评论中所说的,一行两个函数调用。

一行代码很好,但有时很难阅读。如果你想坚持你所拥有的,这里有一些注意事项

<?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;

到目前为止,你已经尝试了什么?我尝试了for循环和while循环,但没有给出预期的结果。你的工作如何,然后我可以用1行代码和2个函数调用来完成,只是说。。。。,
$lease = array();
foreach($array as $obj){
    $lease[] = $obj["LeaseNumber"];
}