Shortening a Multidimentional Array?

Hey guys, so when i use fetch_assoc() to get stuff from the database it returns as a multidimentional array and looks like this:

Array
(
[0] => Array
(
[page] => Item
=> #
)

[1] => Array
    (
        [page] => Item
         => #
    )

)

I would like to combine it with another array and put both into a new array to make it look like this :

Array
(
PageTab => Array
(
[page] => Item
=> #
)

PageTab => Array
    (
        [page] => Item
         => #
    )

)

Do you have any suggestions on the best way to do this?

If the order doesn’t matter, just:


foreach($Array as $Value) {
  $NewArray[$PageTabs[3]] = $Value;
}

I’m sure there’s a better way, but this works :slight_smile:

If you need to merge an array, then array_merge()

Array
(
PageTab => Array
(
[page] => Item
 => #
)

PageTab => Array
(
[page] => Item
 => #
)
)

As each array key is called PageTab, there will only ever be one array.


$ans['PageTab']=array('q' => 'a', 'a'=>2);
$ans['PageTab']=array('q' => 'a', 'a'=>2);
$ans['PageTab']=array('q' => 'b', 'a'=>3);

var_dump($ans);

// gives
array
  'PageTab' => 
    array
      'q' => string 'b' (length=1)
      'a' => int 3


As Cups said, you can’t have associative array with keys that share same name.