SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2012
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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
    [url] => #
    )

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

    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
    [url] => #
    )

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

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

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the order doesn't matter, just:

    PHP Code:
    foreach($Array as $Value) {
      
    $NewArray[$PageTabs[3]] = $Value;

    I'm sure there's a better way, but this works


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

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    Array
    (
    PageTab => Array
    (
    [
    page] => Item
    [url] => #
    )

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

    As each array key is called PageTab, there will only ever be one array.
    PHP Code:
    $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 

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    51
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    As Cups said, you can't have associative array with keys that share same name.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •