PHP Array: How to Inherit Key name from first Array value
I need to use PHP to get from this Array to the next Array,
[results] => Array
(
[row] => Array
(
[0] => Array
(
[col0] => "banana"
[col1] => "grape"
[col2] => "apple"
)
[1] => Array
(
[col0] => "ford"
[col1] => "chevy"
[col2] => "chrysler"
)
)
)
[results] => Array
(
[row] => Array
(
[banana] => Array
(
[col0] => "banana"
[col1] => "grape"
[col2] => "apple"
)
[ford] => Array
(
[col0] => "ford"
[col1] => "chevy"
[col2] => "chrysler"
)
)
)
Please keep in mind tha开发者_运维知识库t the array row
has no set size or length.
$data = array(...); // your data
foreach ( $data['results']['row'] as $k => $v ) {
unset($data['results']['row'][$k]);
$data['results']['row'][$v['col0']] = $v;
}
$new_array = array();
foreach ($array as $key => $value) {
foreach ($value as $key2 => $value2) {
$new_array[$key][$key2][$value2['col0']] = $value2;
}
}
var_dump($new_array);
$farray = array(); //your array
foreach($faray['results']['row'] as $key => $val)
{
echo $key;
echo '<hr>';
print_r($val);
echo '<br>';
}
array_fill_keys is a function in php.
refer it http://www.php.net/manual/en/function.array-fill-keys.php
精彩评论