多维数组的内爆

时间:2016-12-22 04:05:24

标签: php arrays

//对于下面的数组我需要将name的值分隔为分号 输出应该是 莫妮卡; pradnesh

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [name] => Monica
                    [address] => Surat
                    [mobile_number] => 8956231245
                    [telephone_number] => 
                    [email_id] => monica@kritva.com
                    [DOB] => 0000-00-00
                    [gender] => female
                    [PAN_number] => ASDFG4567A
                    [customer_type] => SALES
                    [dependency_type] => Retail
                    [cust_id] => 9055954
                    [state] => Gujarat
                    [city] => Surat
                    [zipcode] => 752852
                    [exist_from] => 2016-12-20
                    [edit_date] => 0000-00-00
                    [staff_id] => 
                    [BA_id] => 
                    [id] => 31
                    [nationality] => Indian
                )

    )

[1] => Array
    (
        [0] => Array
            (
                [name] => Pradnesh
                [address] => Surat
                [mobile_number] => 8956231245
                [telephone_number] => 
                [email_id] => pradnesh.valapkar@kritva.com
                [DOB] => 0000-00-00
                [gender] => male
                [PAN_number] => GHJKL9876S
                [customer_type] => NRI
                [dependency_type] => BA
                [cust_id] => 2736738
                [state] => Gujarat
                [city] => Surat
                [zipcode] => 895623
                [exist_from] => 2016-12-21
                [edit_date] => 0000-00-00
                [staff_id] => 
                [BA_id] => 5822043
                [id] => 33
                [nationality] => Indian
            )

    )

)

2 个答案:

答案 0 :(得分:1)

首先将所有名称提取到数组$ names,然后像这样使用implode:

$names = array_map(function($v){return $v[0]['name'];}, $array);
implode(',', $names);

答案 1 :(得分:1)

这可能会告诉你方向。

<?php 
$data = array(
    array(
        array(
          'name' => 'Monica',
          'address' => 'surat'
        ),
        array(
          'name' => 'Priya',
          'address' => 'surat'
        )
    ),
    array(
        array(
          'name' => 'Pradnesh',
          'address' => 'surat'
        ),
        array(
          'name' => 'test',
          'address' => 'surat'
        )
    )
);

array_map(function ($entry) {
    echo implode(';',array_map(function ($d) {
       return $d['name'];
    }, $entry));
}, $data);
?>

<强>输出

Monica;PriyaPradnesh;test