Search code examples
phparraysassociative-arrayarray-push

Dynamically retrieve array from another array and push it to another associative array as key value pair


I'm getting results from an api call.

I'm storing it in an array $phone_nums. The structure of array is like this:

Result:

    array (size=2)
      0 => 
        array (size=4)
          'is_error' => int 0
          'version' => int 3
          'count' => int 3
          'values' => 
            array (size=3)
              0 => 
                array (size=4)
                  "id"            ="1"
                  'contact_id'    = "207"
                  'phone'         = "8888888888"
                  'phone_id_type' = "2"
              1 => 
                array (size=4)

                  "id"            ="2"
                  'contact_id'    = "207"
                  'phone'         = "8475895894"
                  'phone_id_type' = "2"
              2 => 
                array (size=4)
                  "id"            ="2"
                  'contact_id'    = "207"
                  'phone'         = "48948594894"
                  'phone_id_type' = "2"

      1 => 
        array (size=5)
          'is_error' => int 0
          'version' => int 3
          'count' => int 1
          'id' => int 160
          'values' => 
            array (size=1)
              0 => 
                array (size=4)
                  "id"            ="1"
                  'contact_id'    = "207"
                  'phone'         = "48948594894"
                  'phone_id_type' = "2"

Now I have to fetch phone number, ph.no type from this array and add to a new associative array $ph_maps with key being contact_id and map corresponding ph numbers to it something like this.

$ph_maps = ("207"=>array(48782387489,4874843899,90483499908), 208=>array(789732187,38983298,938938)

Here is my code. there is some problem with it.

for ($i=0; $i < count($phone_nums); $i++) { 
    for ($j=0; $j < $phone_nums[$i]['count']; $j++) {
        $ph_maps = array();
        $ph_maps[$phone_nums["values"][$i]["contact_id"]] = array($phone_nums[$i]['values'][$j]['phone']);
    }      

Solution

  • 1 - You're resetting a new array each time in your loops with $ph_maps = array();, erasing the previous entries. You current result is probably an array with a single contact_id / phone entry. Put it outside of the loops.

    2 - In your second loop, you're not adding a new entry to your array for each contact_id, but setting a new unique one. You've to add [] to force a new entry to be made.

    3 - You're adding a new array for each phone number, while in your desired output you seems to simply want the value, so you should remove the = array(...).

    4 - You contact_id key wasn't correct : use $phone_nums[$i]["values"][$j]["contact_id"] instead of $phone_nums["values"][$i]["contact_id"]

    $ph_maps = array();
    for ($i=0; $i < count($phone_nums); $i++) { 
        for ($j=0; $j < $phone_nums[$i]['count']; $j++) { 
            $ph_maps[$phone_nums[$i]["values"][$j]["contact_id"]][] = $phone_nums[$i]['values'][$j]['phone'];
        } 
    }