使用逗号和括号将对象/数组转换为数组

时间:2015-11-16 21:48:34

标签: php arrays codeigniter

我们的结果:

Array (
[0] => Array ( [0] => Province [1] => Party [2] => Age [3] => Name [4] => Gender )
[1] => Array ( [0] => Quebec [1] => NDP [2] => 22 [3] => Liu, Laurin [4] => Female )
[2] => Array ( [0] => Quebec [1] => Bloc Quebecois [2] => 22 [3] => Mourani, Maria [4] => Female ) 
)

我想要一个看起来像这样的结果:如何转换成这样的?

array(
['Province'=>'Quebec','Party'=>'NDP','Age'=>22,'Name'=>'Liu, Laurin','Gender'=>'Female'],
['Province'=>'Quebec','Party'=>'Bloc Quebecois','Age'=>43,'Name'=>'Mourani, Maria','Gender'=>'Female']
)

OR

array(
['Province', 'Party', 'Age', 'Name', 'Gender'],
['Quebec', 'NDP', 22, 'Liu, Laurin', 'Female'],
['Quebec', 'Bloc Quebecois', 43, 'Mourani, Maria', 'Female']
)

1 个答案:

答案 0 :(得分:1)

使用array_combine()为每行设置标题非常简单,只需walking the array设置这些标题:

$headers = array_shift($myArray);
array_walk(
    $myArray,
    function(&$row) use ($headers) {
        $row = array_combine($headers, $row);
    }
);