将多维数组转换为字符串并推送到新数组

我有一个像这样的数组:


array:6 [

  0 => array:3 [

    0 => "1"

    1 => "2"

    2 => "3"

  ]

  1 => array:3 [

    0 => "1"

    1 => "3"

    2 => "2"

  ]

  2 => array:3 [

    0 => "2"

    1 => "1"

    2 => "3"

  ]

从这个数组中,我必须形成一个数字 123, 132, .... 并将其推送到新数组。


我试过下面的代码,它返回给空值。


$finalArr = array();

array_map(function($item) use ($finalArr) {

    $list = implode($item);

    if (!in_array($list, $finalArr)) {

        array_push($finalArr, $list);

    }

}, $results);


dd($finalArr)


心有法竹
浏览 251回答 3
3回答

泛舟湖上清波郎朗

只需使用array_map对每个数组应用内爆。print_r(array_map("implode",$arr));演示: https : //3v4l.org/NG9Mf

慕莱坞森

请看下面的代码$data = array(&nbsp; '0' => array(&nbsp; &nbsp; '0' => "1",&nbsp; &nbsp; '1' => "2",&nbsp; &nbsp; '2' => "3",&nbsp; ),&nbsp; '1' => array(&nbsp; &nbsp; '0' => "1",&nbsp; &nbsp; '1' => "3",&nbsp; &nbsp; '2' => "2",&nbsp; ),&nbsp; '2' => array(&nbsp; &nbsp; '0' => "2",&nbsp; &nbsp; '1' => "1",&nbsp; &nbsp; '2' => "3",&nbsp; ),);$finalArray = array();foreach( $data as $d ) {&nbsp; &nbsp; $finalArray[] = implode('', $d);}echo '<pre>';print_r($finalArray);请在此处查看在线演示

拉风的咖菲猫

array_map('implode',&nbsp;$yourArray);这应该是您获得带有连接字符串的数组所需的全部内容。如果您之后需要将它们推送到另一个数组,您可以这样做:array_push($otherArray,&nbsp;...array_map('implode',&nbsp;$yourArray));
打开App,查看更多内容
随时随地看视频慕课网APP