如何将第一个值转换为数组名称php的键

时间:2019-03-25 08:47:18

标签: php arrays key

如何将第一个值转换为数组和php的键。嗨,我正在从事一些阵列操作。以下是我拥有的数组:

$data= array
(
    array('city', 'california','big city'),
    array('address', 'this is','address', 'zzz'),
    array('something', 'item 3','details 3'),
);

Array
(
    [0] => Array
        (
            [0] => city
            [1] => california
            [2] => big city
        )
    [1] => Array
        (
            [0] => address
            [1] => this is
            [2] => address
            [3] => zzz
    [2] => Array
        (
            [0] => something
            [1] => item 3
            [2] => details 3
        )
        ..........
)

我期望得到这个结果:

Array
(
    [city] => Array
        (
            [0] => city
            [1] => california
            [2] => big city
        )
    [address] => Array
        (
            [0] => address
            [1] => this is
            [2] => address
            [3] => zzz
    [something] => Array
        (
            [0] => something
            [1] => item 3
            [2] => details 3
        )
        ..............
)

上面提到的是结果。我已经尝试过使用数组函数array_keys和array_values,但是它不起作用。给我伸出援手

1 个答案:

答案 0 :(得分:0)

尝试使用forEach

 $data= array
(
    array('city', 'california','big city'),
    array('address', 'this is','address', 'zzz'),
    array('something', 'item 3','details 3'),
);
 $res=[];
foreach($data as $val){
    $res[$val[0]] = $val;
}
print_r($res);