doutong1890 2018-04-18 19:10
浏览 339
已采纳

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

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'];
        }
  • 写回答

3条回答 默认 最新

  • dongqiao8421 2018-04-18 19:22
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 模拟电路求复阻抗和传递函数,请各位拍照写一下解答过程
  • ¥60 不懂得怎么运行下载来的代码
  • ¥15 CST导出3D模型图为什么和软件显示不一样?
  • ¥15 加热反应炉PLC控制系统设计(相关搜索:梯形图)
  • ¥15 python 用Dorc包报错,我的写法和网上教的是一样的但是它显示无效参数,是什么问题
  • ¥15 经过滑动平均后的一维信号还原用什么结构好呢?
  • ¥15 指定IP电脑的访问设置
  • ¥30 matlab ode45 未发现警告,但是运行出错
  • ¥15 为什么devc++编译项目会失败啊
  • ¥15 vscode platformio