Why my array does not sort correctly

I have two arrays $men, $sub , I want want to sort them with this condition: if($men["titre"] == $sub["menuParent"]) I put the content of the second array that contains $sub["menuParent"] in the first one.

Case:

//Array1 ($menu)

array(3) {
  ["menu1"] => array(3) {
    ["titre"] => string(6) "Title2"
    ["lien"] => string(0) ""
    ["order"] => string(1) "2"
  }
  ["menu2"] => array(3) {
    ["titre"] => string(6) "Title3"
    ["lien"] => string(10) "google.com"
    ["order"] => string(1) "3"
  }
  ["menu3"] => array(3) {
    ["titre"] => string(6) "Title1"
    ["lien"] => string(0) ""
    ["order"] => string(1) "1"
  }
}

//Array2 ($submenu)

array(3) {
  ["submenu1"] => array(3) {
    ["titre"] => string(9) "SubTitle2"
    ["menuParent"] => string(6) "Title2"
    ["order"] => string(1) "2"
  }
  ["submenu2"] => array(3) {
    ["titre"] => string(9) "SubTitle3"
    ["menuParent"] => string(6) "Title2"
    ["order"] => string(1) "3"
  }
  ["submenu3"] => array(3) {
    ["titre"] => string(9) "SubTitle1"
    ["menuParent"] => string(6) "Title1"
    ["order"] => string(1) "1"
  }
}

Here for exemple in:

array1 ["menu1"]["titre"] => string(6) "Title2"

array2 ["submenu1"]["menuParent"] => string(6) "Title2"

Now while ["menu1"]["titre"] & ["submenu1"]["menuParent"] values are the same "Title2", So I push :

["submenu1"] => array(3) {
    ["titre"] => string(9) "SubTitle2"
    ["menuParent"] => string(6) "Title2"
    ["order"] => string(1) "2"
}

in:

["menu1"] => array(3) {
    ["titre"] => string(6) "Title2"
    ["lien"] => string(0) ""
    ["order"] => string(1) "2"
  }

as child like this:

array(3) {
  ["menu1"] => array(3) {
    ["titre"] => string(6) "Title2"
    ["lien"] => string(0) ""
    ["order"] => string(1) "2"
    ["child"] => array(3) {
      [0] => array(3) {
        ["titre"] => string(9) "SubTitle2"
        ["menuParent"] => string(6) "Title2"
        ["order"] => string(1) "2"
      }
  }

So I do all this for whole elements`

Here is my try, but It push all the elements in ["menu2"] instead of ["submenu1"], ["submenu2"] IN ["menu1"] and ["submenu3"] IN ["menu3"]

My Try:

/*Injection submenu to Menu*/
foreach($menu as $men) {
    foreach($submenu as $sub) {
        if($men["titre"] == $sub["menuParent"]){
        $key = key($menu);
        $menu[$key]['child'][] = array($sub["titre"], $sub["menuParent"], $sub["order"]);
        //My array_push doesn't work also
        array_push($menu, array($sub["titre"], $sub["menuParent"], $sub["order"]))
        }

    }
}

Result:

array(3) {
  ["menu1"] => array(3) {
    ["titre"] => string(6) "Title2"
    ["lien"] => string(0) ""
    ["order"] => string(1) "2"
  }
  ["menu2"] => array(4) {
    ["titre"] => string(6) "Title3"
    ["lien"] => string(10) "google.com"
    ["order"] => string(1) "3"
    ["child"] => array(3) {
      [0] => array(3) {
        [0] => string(9) "SubTitle2"
        [1] => string(6) "Title2"
        [2] => string(1) "2"
      }
      [1] => array(3) {
        [0] => string(9) "SubTitle3"
        [1] => string(6) "Title2"
        [2] => string(1) "3"
      }
      [2] => array(3) {
        [0] => string(9) "SubTitle1"
        [1] => string(6) "Title1"
        [2] => string(1) "1"
      }
    }
  }
  ["menu3"] => array(3) {
    ["titre"] => string(6) "Title1"
    ["lien"] => string(0) ""
    ["order"] => string(1) "1"
  }
}

Where I'm at fault, If anyone can explain to me why and if I took the good way.

1 answer

  • answered 2018-06-17 18:45 Nigel Ren

    The problem is where your picking the key up, you can use the foreach ( as $key->$value) method to get the key of each item (foreach works on it's own copy of the array and so picking the key from the original array isn't the same as the current one). Also you could use &$men, the & allows you to alter the original array element rather than create a new entry and trying to push the new element into the array...

    foreach($menu as $key => $men) {
        foreach($submenu as $key1=>$sub) {
            if($men["titre"] == $sub["menuParent"]){
                $menu[$key]['child'][$key1] = array($sub["titre"], $sub["menuParent"], $sub["order"]);
            }
    
        }
    }
    

    gives...

    Array
    (
        [menu1] => Array
            (
                [titre] => Title2
                [lien] => 
                [order] => 2
                [child] => Array
                    (
                        [submenu1] => Array
                            (
                                [0] => SubTitle2
                                [1] => Title2
                                [2] => 2
                            )
    
                        [submenu2] => Array
                            (
                                [0] => SubTitle3
                                [1] => Title2
                                [2] => 3
                            )
    
                    )
    
            )
    
        [menu2] => Array
            (
                [titre] => Title3
                [lien] => google.com
                [order] => 3
            )
    
        [menu3] => Array
            (
                [titre] => Title1
                [lien] => 
                [order] => 1
                [child] => Array
                    (
                        [submenu3] => Array
                            (
                                [0] => SubTitle1
                                [1] => Title1
                                [2] => 1
                            )
    
                    )
    
            )
    
    )
    

    Update:

    Code using &$men, note that in setting the data it no longer uses the key, but uses the $men variable.

    foreach($menu as &$men) {
        foreach($submenu as $key=>$sub) {
            if($men["titre"] == $sub["menuParent"]){
                $men['child'][$key] = array($sub["titre"], $sub["menuParent"], $sub["order"]);
            }
        }
    }
    unset($men);  // Good practice to ensure reference is reset