Search code examples
phparraysarray-key

Match two array associates keys and replace reference array key with 1st array


Match 1st and 2nd array associates keys. And replace 1st array associates keys with 2nd array value. Information like below.

1st Array

  [
        [ ],
        [ ],
       {
         585: {
               firsthalf: "0",
               secondhalf: "1",
               goals: "1",
               outcome: ["loss"]
              },
         625: {
               firsthalf: "2",
               secondhalf: "2",
               goals: "4",
               outcome: ["win"]
              }
         },
         {
          609: {
                firsthalf: "2",
                secondhalf: "0",
                goals: "2",
                outcome: ["win"]
               },
          625: {
                firsthalf: "0",
                secondhalf: "1",
                goals: "1",
                outcome: ["loss"]
               }
           },

2nd Array

       [
        {654: "North Geelong Warriors FC"},
        {645: "Springvale White Eagles FC"},
        {637: "Brunswick City Soccer Club"},
        {625: "Melbourne Victory Youth FC"},
        {585: "Moreland City FC"},

Final out come should be like this

Final Array
       [
        [ ],
        [ ],
       {
         Moreland City FC: {
               firsthalf: "0",
               secondhalf: "1",
               goals: "1",
               outcome: ["loss"]
              },
         Melbourne Victory Youth FC: {
               firsthalf: "2",
               secondhalf: "2",
               goals: "4",
               outcome: ["win"]
              }
         },

Solution

  • $newArray = array();
    foreach($Array01 as $keys => $values){
    foreach($values as $key => $value){
            for ($x = 0; $x <= count($array02); $x++) {
                if (array_search($key, $array02[$x])){
                    $newkey = array_search($key,$array02[$x]);
                    $newArray[] = array($newkey => $value);
          }
        }
      }
    }
    print_r($$newArray);
    

    This is been sort it out.