0

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 that the array row has no set size or length.
Any help would be appreciated!
Thank you so much!

4 Answers 4

4
$data = array(...); // your data
foreach ( $data['results']['row'] as $k => $v ) {
  unset($data['results']['row'][$k]);
  $data['results']['row'][$v['col0']] = $v;
}
Sign up to request clarification or add additional context in comments.

Comments

0
$new_array = array();
foreach ($array as $key => $value) {
    foreach ($value as $key2 => $value2) {
         $new_array[$key][$key2][$value2['col0']] = $value2;
    }
}

var_dump($new_array);

2 Comments

I mean notices.. However: Undefined index - You should declare everything before.
I fixed some errors in the code, saw a better solution from @hsz
0
$farray = array(); //your array
foreach($faray['results']['row'] as $key => $val)
{
    echo $key;
    echo '<hr>';
    print_r($val);
    echo '<br>';

}

Comments

0

array_fill_keys is a function in php.

refer it http://www.php.net/manual/en/function.array-fill-keys.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.