title: php build simpler array from a multidimensional (json reponse) array
body: <p>I am trying to get data from a multidimensional array I got from a Facebook response, into a simpler array.</p>
  
  <p>the json data is as seen below.</p>
  
  <pre><code>{
  "albums": {
      "data": [
          {
              "photos": {
                  "data": [
                      {
                          "name": "a photo name",
                          "source": "https://fbcdn-sphotos-d-a.akamaihd.net/hphotos-ak-xlp1/t31.0-8/s720x720/XXXXXXXXXXXX",
                          "picture": "https://fbcdn-photos-d-a.akamaihd.net/hphotos-ak-xfp1/v/t1.0-0/XXXXXXXXXXXXXX",
                      },
                      {
                          "name": "a photo name",
                          "source": "https://fbcdn-photos-d-a.akamaihd.net/hphotos-ak-xtp1/v/t1.0-0/p480x480/XXXXXXXXXXXXXXXXXX",
                          "picture": "https://fbcdn-photos-d-a.akamaihd.net/hphotos-ak-xtp1/v/t1.0-0/s130x130/XXXXXXXXXXXX",
                      },
                      {
                          "name": "a photo name",
                          "source": "https://fbcdn-photos-d-a.akamaihd.net/hphotos-ak-xtf1/v/t1.0-0/p480x480/XXXXXXXXXXXXXX",
                          "picture": "https://fbcdn-photos-d-a.akamaihd.net/hphotos-ak-xtf1/v/t1.0-0/s130x130/XXXXXXXXXXXXXXX",
                      },
                      {
                          "name": "a photo name",
                          "source": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-0/p480x480/XXXXXXXXXXXXXXXXXX",
                          "picture": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-0/s130x130/XXXXXXXXXXXX",
                      }
                  ],
                  "paging": {
                      "cursors": {
                          "before": "MTXXXXXXXXXXXXXXXZD",
                          "after": "MXXXXXXXXXXXXXZD"
                      }
                  }
              },
          },
          {
              "photos": {
                  "data": [
                      {
                          "source": "https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-xfp1/v/t1.0-9/XXXXXXXXXXXXXXX",
                          "picture": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xfp1/v/t1.0-0/s130x130/XXXXXXXXXXXX",
                      },
                      {
                          "name": "XXXXXXXXX",
                          "source": "https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/XXXXXXXXXXX",
                          "picture": "https://fbcdn-photos-a-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-0/s130x130/XXXXXXXXXXXX",
                      },
                      {
                          "name": "XXXXXXXXXXX",
                          "source": "https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-9/XXXXXXXXXXX",
                          "picture": "https://fbcdn-photos-c-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-0/p130x130/XXXXXXXXXXX",
                      },
                      {
                          "name": "a photo name",
                          "source": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXXXXX",
                          "picture": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/v/t1.0-0/s130x130/XXXXXXXXXXXX",
                      },
                      {
                          "name": "a photo name",
                          "source": "https://fbcdn-photos-c-a.akamaihd.net/hphotos-ak-xpa1/t31.0-0/XXXXXXXXXXX",
                          "picture": "https://fbcdn-photos-c-a.akamaihd.net/hphotos-ak-xpt1/v/t1.0-0/XXXXXXXXXX",
                      },
                      {
                          "name": "a photo name",
                          "source": "https://fbcdn-photos-a-a.akamaihd.net/hphotos-ak-xaf1/t31.0-0/p480x480/XXXXXXXXXX",
                          "picture": "https://fbcdn-photos-a-a.akamaihd.net/hphotos-ak-xat1/v/t1.0-0/s130x130/XXXXXXXX",
                      },
                      {
                          "name": "a photo name",
                          "source": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXX",
                          "picture": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/v/t1.0-0/s130x130/XXXXXXXX",
                      }
                  ],
                  "paging": {
                      "cursors": {
                          "before": "MTXXXXXXXXXXXXXXXgZDXXXX",
                          "after": "MTcXXXXXXXXXXXXXXXXXD"
                      }
                  }
              },
          },
          {
              "photos": {
                  "data": [
                      {
                          "source": "https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-xta1/v/t1.0-9/1",
                          "picture": "https://fbcdn-photos-a-a.akamaihd.net/hphotos-ak-xta1/v/t1.0-0/s130x130/",
                      },
                      {
                          "name": "XXXXXXXXXXXXXXXXX",
                          "source": "https://scontent.xx.fbcdn.net/v/t1.0-9/11",
                          "picture": "https://fbcdn-photos-c-a.akamaihd.net/hphotos-ak-xaf1/v/t1.0-0/s130x130/11",
                      }
                  ],
                  "paging": {
                      "cursors": {
                          "before": "MOIXXXXXXXXXXXXX",
                          "after": "MTXXXXXXXXXXXXXD"
                      }
                  }
              },
          },
          {
              "photos": {
                  "data": [
                      {
                          "source": "https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-xpl1/t31.0-8/s720x720/",
                          "picture": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xft1/v/t1.0-0/s130x130/",
                      }
                  ],
                  "paging": {
                      "cursors": {
                          "before": "MTXXXXXXXXXXXSJKSDJZDZD",
                          "after": "MTdXXXXXXXXDKSJKDNXXXXXXXXXpZD"
                      }
                  }
              },
          }
      ],
      "paging": {
          "cursors": {
              "before": "MXXXXXXXXXXXXXXD",
              "after": "MXXXXXXXXXXXXJR"
          }
      }
  },
  "feed": {
      "data": [
          {
          },
          {
              "attachments": {
                  "data": [
                      {
                          "media": {
                              "image": {
                                  "height": 480,
                                  "src": "https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-xpf1/t31.0-8/s720x720/",
                                  "width": 720
                              }
                          }
                      }
                  ]
              }
          },
          {
              "attachments": {
                  "data": [
                      {
                          "media": {
                              "image": {
                                  "height": 102,
                                  "src": "https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-xta1/v/t1.0-9/",
                                  "width": 197
                              }
                          }
                      }
                  ]
              }
          },
          {
              "attachments": {
                  "data": [
                      {
                          "media": {
                              "image": {
                                  "height": 276,
                                  "src": "https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-xpl1/t31.0-8/s720x720/",
                                  "width": 720
                              }
                          }
                      }
                  ]
              }
          },
          {
          },
          {
              "attachments": {
                  "data": [
                      {
                          "description": "XXXXXXXXXXXXKE",
                          "media": {
                              "image": {
                                  "height": 540,
                                  "src": "https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-9/",
                                  "width": 540
                              }
                          }
                      }
                  ]
              }
          },
          {
              "attachments": {
                  "data": [
                      {
                          "description": "XXXXXXXXXXJ",
                          "media": {
                              "image": {
                                  "height": 375,
                                  "src": "https://scontent.xx.fbcdn.net/v/t1.0-9/",
                                  "width": 500
                              }
                          }
                      }
                  ]
              }
          },
          {
              "attachments": {
                  "data": [
                      {
                          "description": "XXXXXXXXXXXX",
                          "media": {
                              "image": {
                                  "height": 540,
                                  "src": "https://fbcdn-photos-a-a.akamaihd.net/hphotos-ak-xaf1/v/t1.0-0/p180x540/",
                                  "width": 720
                              }
                          }
                      }
                  ]
              }
          },
          {
              "attachments": {
                  "data": [
                      {
                          "description": "XXXXXXXXXXXXp",
                          "media": {
                              "image": {
                                  "height": 405,
                                  "src": "https://scontent.xx.fbcdn.net/v/t1.0-9/s720x720/",
                                  "width": 720
                              }
                          }
                      }
                  ]
              }
          },
          {
              "attachments": {
                  "data": [
                      {
                          "media": {
                              "image": {
                                  "height": 392,
                                  "src": "https://scontent.xx.fbcdn.net/v/t1.0-9/",
                                  "width": 626
                              }
                          }
                      }
                  ]
              }
          },
          {
              "attachments": {
                  "data": [
                      {
                          "description": "XXXXXXXX",
                          "media": {
                              "image": {
                                  "height": 255,
                                  "src": "https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/",
                                  "width": 208
                              }
                          }
                      }
                  ]
              }
          }
      ],
      "paging": {
          "previous": "https://graph.facebook.com/v2.5/1XXXXXXXXXXXX6/feed?fields=a",
          "next": "https://graph.facebook.com/v2.5/1SXXXXXXXXXXXXX96/feed?fields="
      }
  },
  }
  </code></pre>
  
  <p>I want to end up with an array like this:</p>
  
  <pre><code>Array
  (
  [photo] =&gt; Array
      (
          [0] =&gt; Array
              (
                  [description] =&gt; a photo name
                  [image_src] =&gt; https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXX
              )
          [1] =&gt; Array
              (
                  [description] =&gt; a photo name
                  [image_src] =&gt; https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXX
              )
          [2] =&gt; Array
              (
                  [description] =&gt; a photo name
                  [image_src] =&gt; https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXX
              )
  
              and so on......
      )
  )
  </code></pre>
  
  <p>But I get this:</p>
  
  <pre><code>Array
  (
  [photo] =&gt; Array
      (
          [0] =&gt; Array
              (
                  [description] =&gt; a photo name
              )
  
          [1] =&gt; Array
              (
                  [image_src] =&gt; https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXX
              )
          [2] =&gt; Array
              (
                  [description] =&gt; a photo name
              )
  
          [3] =&gt; Array
              (
                  [image_src] =&gt; https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXX
              )
  and so on...
  
  )
  )
  </code></pre>
  
  <p>This is the code for my current loop:</p>
  
  <pre><code>foreach ($raw_facebook['albums']['data'] as $photos) { 
           if ($photos) {
                  foreach ($photos['photos']['data'] as $photo) {
                      if ( (isset($photo['name']) &amp;&amp; array_key_exists('name', $photo)) &amp;&amp; (isset($photo['source']) &amp;&amp; $this-&gt;array_key_exists_recursive('source', $photo)) ) {
                          $facebook['photo'][]['description'] = $photo['name'];
                          $facebook['photo'][]['image_src'] = $photo['source'];
                      }
                  }
  
              }
  }
  </code></pre>
  
  <p>When I modify this code to this:</p>
  
  <pre><code>foreach ($raw_facebook['albums']['data'] as $photos) { 
              if ($photos) {
                  $i = 0;
                  foreach ($photos['photos']['data'] as $photo) {
                      if ( (isset($photo['name']) &amp;&amp; array_key_exists('name', $photo)) &amp;&amp; (isset($photo['source']) &amp;&amp; $this-&gt;array_key_exists_recursive('source', $photo)) ) {
                          $facebook['photo'][$i]['description'] = $photo['name'];
                          $facebook['photo'][$i]['image_src'] = $photo['source'];
                      }
                  }
                  $i++;
  
              }
  }
  </code></pre>
  
  <p>I get a result similar to what I need, but with only 6 inner arrays (when there should be like 10 of them), showing the iteration isn't going correctly.</p>
  
  <p>Please I'd appreciate to be pointed in the right direction.</p>