开发者

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.

Any help would be appreciated!

Thank you so much!


$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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜