使数组在关联数组中

时间:2017-09-06 01:32:59

标签: php arrays associative-array

我有一个带有按键

的数组
Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Some ,name
            [codes] => 36101,36055,36071
        )

    [1] => Array
        (
            [id] => 31
            [name] => somename
            [codes] => 42049,42103,34003,42127,42095,36091,42113
        )
    [2] => Array
        (
            [id] => 27
            [name] => somename
            [codes] => 42107,36119,42009,36121,42087,42033,42083
        )
)

所以,我试图通过"和#34;在[代码]处将阵列打破一个级别。尝试爆炸但没有得到结果或可能是我做错了什么!谁可以帮我这个事 ?!

TIA

1 个答案:

答案 0 :(得分:0)

试试这个

假设你的数组是这样的

$arrayName = array(
    '0' => array(
        'id' => '1', 
        'name' => 'Some ,name', 
        'codes' => '36101,36055,36071', 
        ), 
    '1' => array(
        'id' => '2', 
        'name' => 'Some ,name', 
        'codes' => '42049,42103,34003,42127,42095,36091,42113', 
        ), 
    );

<强>代码

foreach($arrayName as $key => $item)
{
    $newArray = explode(',', $item['codes']);
    $arrayName[$key]['codes'] = $newArray;

}

输出

enter image description here