title: 4chan's API: How to Loop through json decoded array using PHP
body: <p>I'm trying to get all the <strong>.wembs</strong> and <strong>.gifs</strong> url from <strong>4chan/wsg/</strong> board so I can download it to my server. I'm having trouble looping through json decoded array in PHP.</p>
  
  <p>Here's my PHP <code>/threads_update.php</code> :</p>
  
  <pre><code>&lt;?php
  $jsonfile = file_get_contents('./threads.json');
  $json = json_decode($jsonfile, true);
  
  for($i = 0; $i &lt; count($json); $i++)
  {
    for($x = 0; $x &lt; count($json); $x++)
    {
      $empatch = $json[$i]['threads'][$x]['no'];
      $webmslinks[$x] = 'https://a.4cdn.org/wsg/thread/'.$empatch.'.json';
  
      echo $i.'-'.$x.' '.'&lt;a href='.$webmslinks[$x].' target="_blank&gt;'.$webmslinks[$x].'&lt;/a&gt;&lt;br&gt;';
    }
  }
  
  echo '&lt;pre&gt;' . print_r($json, true) . '&lt;/pre&gt;'; #printing json decoded array in a nice way
  ?&gt;
  </code></pre>
  
  <p>in <code>/threads_update.php</code>, I'm trying to get all the links to the jsons of all the threads in <strong>4chan/WSG</strong> board so I can get the detail on the posts. If I managed to get all the links, I want to cronjob it to download every * minute to my server so it updates the json.</p>
  
  <p>I practiced a bit on getting the .wembs and .gifs link from the <code>catalog.json</code> of 4chan. here's the PHP code <code>/download_webm.php</code></p>
  
  <pre><code>&lt;?php
  $jsonfile = file_get_contents('./catalog.json');
  $json = json_decode($jsonfile, true);
  
  for($i = 0; $i &lt; sizeof($json); $i++)
  {
    for($x = 0; $x &lt; sizeof($json); $x++)
    {
  $empatwebm = $json[$i]['threads'][$x]['tim'];
  $empatext = $json[$i]['threads'][$x]['ext'];
  
  $webmslinks[$x] = 'http://i.4cdn.org/wsg/'.$empatwebm.$empatext;
  echo $i.'-'.$x.' '.'&lt;a href='.$webmslinks[$x].' target="_blank&gt;'.$webmslinks[$x].'&lt;/a&gt;&lt;br&gt;';
    }
  }
  
  #echo '&lt;pre&gt;' . print_r($json, true) . '&lt;/pre&gt;';
  ?&gt;
  </code></pre>
  
  <p>I want to focus on the problem in <code>/threads_update.php</code> though, here's the json decoded array I'm using in <code>/threads_update.php (threads.json)</code> I cut it a bit because it's too long:</p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [page] =&gt; 1
              [threads] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [no] =&gt; 247727
                              [last_modified] =&gt; 1422731400
                          )
  
                      [1] =&gt; Array
                          (
                              [no] =&gt; 915253
                              [last_modified] =&gt; 1453541378
                          )
  
                      [2] =&gt; Array
                          (
                              [no] =&gt; 914600
                              [last_modified] =&gt; 1453541285
                          )
  
                      [3] =&gt; Array
                          (
                              [no] =&gt; 914648
                              [last_modified] =&gt; 1453541160
                          )
  
                      [4] =&gt; Array
                          (
                              [no] =&gt; 915256
                              [last_modified] =&gt; 1453541160
                          )
  
                      [5] =&gt; Array
                          (
                              [no] =&gt; 897749
                              [last_modified] =&gt; 1453540855
                          )
  
                      [6] =&gt; Array
                          (
                              [no] =&gt; 859722
                              [last_modified] =&gt; 1453540665
                          )
  
                      [7] =&gt; Array
                          (
                              [no] =&gt; 915689
                              [last_modified] =&gt; 1453540443
                          )
  
                      [8] =&gt; Array
                          (
                              [no] =&gt; 915791
                              [last_modified] =&gt; 1453540122
                          )
  
                      [9] =&gt; Array
                          (
                              [no] =&gt; 908252
                              [last_modified] =&gt; 1453540105
                          )
  
                      [10] =&gt; Array
                          (
                              [no] =&gt; 840187
                              [last_modified] =&gt; 1453539750
                          )
  
                      [11] =&gt; Array
                          (
                              [no] =&gt; 869373
                              [last_modified] =&gt; 1453539744
                          )
  
                      [12] =&gt; Array
                          (
                              [no] =&gt; 912342
                              [last_modified] =&gt; 1453539519
                          )
  
                      [13] =&gt; Array
                          (
                              [no] =&gt; 891852
                              [last_modified] =&gt; 1453539003
                          )
  
                      [14] =&gt; Array
                          (
                              [no] =&gt; 888763
                              [last_modified] =&gt; 1453538876
                          )
  
                  )
  
          )
  
      [1] =&gt; Array
          (
              [page] =&gt; 2
              [threads] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [no] =&gt; 873565
                              [last_modified] =&gt; 1453538275
                          )
  
                      [1] =&gt; Array
                          (
                              [no] =&gt; 892015
                              [last_modified] =&gt; 1453537537
                          )
  
                      [2] =&gt; Array
                          (
                              [no] =&gt; 838897
                              [last_modified] =&gt; 1453536675
                          )
  
                      [3] =&gt; Array
                          (
                              [no] =&gt; 892915
                              [last_modified] =&gt; 1453536388
                          )
  
                      [4] =&gt; Array
                          (
                              [no] =&gt; 909050
                              [last_modified] =&gt; 1453536039
                          )
  
                      [5] =&gt; Array
                          (
                              [no] =&gt; 910942
                              [last_modified] =&gt; 1453535407
                          )
  
                      [6] =&gt; Array
                          (
                              [no] =&gt; 882013
                              [last_modified] =&gt; 1453535284
                          )
  
                      [7] =&gt; Array
                          (
                              [no] =&gt; 848161
                              [last_modified] =&gt; 1453535096
                          )
  
                      [8] =&gt; Array
                          (
                              [no] =&gt; 873593
                              [last_modified] =&gt; 1453534602
                          )
  
                      [9] =&gt; Array
                          (
                              [no] =&gt; 878501
                              [last_modified] =&gt; 1453534316
                          )
  
                      [10] =&gt; Array
                          (
                              [no] =&gt; 910000
                              [last_modified] =&gt; 1453534025
                          )
  
                      [11] =&gt; Array
                          (
                              [no] =&gt; 880261
                              [last_modified] =&gt; 1453533807
                          )
  
                      [12] =&gt; Array
                          (
                              [no] =&gt; 915089
                              [last_modified] =&gt; 1453533162
                          )
  
                      [13] =&gt; Array
                          (
                              [no] =&gt; 878873
                              [last_modified] =&gt; 1453532816
                          )
  
                      [14] =&gt; Array
                          (
                              [no] =&gt; 870501
                              [last_modified] =&gt; 1453532749
                          )
  
                  )
  
          )
  
      [2] =&gt; Array
          (
              [page] =&gt; 3
              [threads] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [no] =&gt; 915195
                              [last_modified] =&gt; 1453532318
                          )
  
                      [1] =&gt; Array
                          (
                              [no] =&gt; 914909
                              [last_modified] =&gt; 1453531117
                          )
  
                      [2] =&gt; Array
                          (
                              [no] =&gt; 903535
                              [last_modified] =&gt; 1453529930
                          )
  
                      [3] =&gt; Array
                          (
                              [no] =&gt; 908885
                              [last_modified] =&gt; 1453529620
                          )
  
                      [4] =&gt; Array
                          (
                              [no] =&gt; 889312
                              [last_modified] =&gt; 1453529423
                          )
  
                      [5] =&gt; Array
                          (
                              [no] =&gt; 915043
                              [last_modified] =&gt; 1453529897
                          )
  
                      [6] =&gt; Array
                          (
                              [no] =&gt; 879280
                              [last_modified] =&gt; 1453528792
                          )
  
                      [7] =&gt; Array
                          (
                              [no] =&gt; 879632
                              [last_modified] =&gt; 1453528350
                          )
  
                      [8] =&gt; Array
                          (
                              [no] =&gt; 913957
                              [last_modified] =&gt; 1453528312
                          )
  
                      [9] =&gt; Array
                          (
                              [no] =&gt; 896705
                              [last_modified] =&gt; 1453528267
                          )
  
                      [10] =&gt; Array
                          (
                              [no] =&gt; 847720
                              [last_modified] =&gt; 1453528237
                          )
  
                      [11] =&gt; Array
                          (
                              [no] =&gt; 838871
                              [last_modified] =&gt; 1453528084
                          )
  
                      [12] =&gt; Array
                          (
                              [no] =&gt; 866201
                              [last_modified] =&gt; 1453527850
                          )
  
                      [13] =&gt; Array
                          (
                              [no] =&gt; 913175
                              [last_modified] =&gt; 1453526674
                          )
  
                      [14] =&gt; Array
                          (
                              [no] =&gt; 833726
                              [last_modified] =&gt; 1453526453
                          )
  
                  )
  
          )
  
      [3] =&gt; Array
          (
              [page] =&gt; 4
              [threads] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [no] =&gt; 904445
                              [last_modified] =&gt; 1453525838
                          )
  
                      [1] =&gt; Array
                          (
                              [no] =&gt; 914580
                              [last_modified] =&gt; 1453524490
                          )
  
                      [2] =&gt; Array
                          (
                              [no] =&gt; 845312
                              [last_modified] =&gt; 1453522034
                          )
  
                      [3] =&gt; Array
                          (
                              [no] =&gt; 899118
                              [last_modified] =&gt; 1453521816
                          )
  
                      [4] =&gt; Array
                          (
                              [no] =&gt; 912517
                              [last_modified] =&gt; 1453521372
                          )
  
                      [5] =&gt; Array
                          (
                              [no] =&gt; 885270
                              [last_modified] =&gt; 1453520982
                          )
  
                      [6] =&gt; Array
                          (
                              [no] =&gt; 913473
                              [last_modified] =&gt; 1453520859
                          )
  
                      [7] =&gt; Array
                          (
                              [no] =&gt; 909015
                              [last_modified] =&gt; 1453519499
                          )
  
                      [8] =&gt; Array
                          (
                              [no] =&gt; 915270
                              [last_modified] =&gt; 1453518011
                          )
  
                      [9] =&gt; Array
                          (
                              [no] =&gt; 867625
                              [last_modified] =&gt; 1453517740
                          )
  
                      [10] =&gt; Array
                          (
                              [no] =&gt; 884229
                              [last_modified] =&gt; 1453515954
                          )
  
                      [11] =&gt; Array
                          (
                              [no] =&gt; 902461
                              [last_modified] =&gt; 1453514881
                          )
  
                      [12] =&gt; Array
                          (
                              [no] =&gt; 796602
                              [last_modified] =&gt; 1453514879
                          )
  
                      [13] =&gt; Array
                          (
                              [no] =&gt; 823926
                              [last_modified] =&gt; 1453514458
                          )
  
                      [14] =&gt; Array
                          (
                              [no] =&gt; 907820
                              [last_modified] =&gt; 1453513381
                          )
  
                  )
  
          )
  
      [4] =&gt; Array
          (
              [page] =&gt; 5
              [threads] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [no] =&gt; 748132
                              [last_modified] =&gt; 1453511379
                          )
  
                      [1] =&gt; Array
                          (
                              [no] =&gt; 907219
                              [last_modified] =&gt; 1453510720
                          )
  
                      [2] =&gt; Array
                          (
                              [no] =&gt; 890615
                              [last_modified] =&gt; 1453508766
                          )
  
                      [3] =&gt; Array
                          (
                              [no] =&gt; 728826
                              [last_modified] =&gt; 1453508251
                          )
  
                      [4] =&gt; Array
                          (
                              [no] =&gt; 770452
                              [last_modified] =&gt; 1453507296
                          )
  
                      [5] =&gt; Array
                          (
                              [no] =&gt; 891223
                              [last_modified] =&gt; 1453506455
                          )
  
                      [6] =&gt; Array
                          (
                              [no] =&gt; 884923
                              [last_modified] =&gt; 1453505426
                          )
  
                      [7] =&gt; Array
                          (
                              [no] =&gt; 903103
                              [last_modified] =&gt; 1453505232
                          )
  
                      [8] =&gt; Array
                          (
                              [no] =&gt; 897296
                              [last_modified] =&gt; 1453504416
                          )
  
                      [9] =&gt; Array
                          (
                              [no] =&gt; 904456
                              [last_modified] =&gt; 1453503832
                          )
  
                      [10] =&gt; Array
                          (
                              [no] =&gt; 455445
                              [last_modified] =&gt; 1453503725
                          )
  
                      [11] =&gt; Array
                          (
                              [no] =&gt; 907417
                              [last_modified] =&gt; 1453503134
                          )
  
                      [12] =&gt; Array
                          (
                              [no] =&gt; 906474
                              [last_modified] =&gt; 1453502455
                          )
  
                      [13] =&gt; Array
                          (
                              [no] =&gt; 913944
                              [last_modified] =&gt; 1453501859
                          )
  
                      [14] =&gt; Array
                          (
                              [no] =&gt; 904009
                              [last_modified] =&gt; 1453500314
                          )
  
                  )
  
          )
  
      [5] =&gt; Array
          (
              [page] =&gt; 6
              [threads] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [no] =&gt; 908709
                              [last_modified] =&gt; 1453497962
                          )
  
                      [1] =&gt; Array
                          (
                              [no] =&gt; 909604
                              [last_modified] =&gt; 1453497092
                          )
  
                      [2] =&gt; Array
                          (
                              [no] =&gt; 867205
                              [last_modified] =&gt; 1453497003
                          )
  
                      [3] =&gt; Array
                          (
                              [no] =&gt; 906638
                              [last_modified] =&gt; 1453495282
                          )
  
                      [4] =&gt; Array
                          (
                              [no] =&gt; 903238
                              [last_modified] =&gt; 1453494874
                          )
  
                      [5] =&gt; Array
                          (
                              [no] =&gt; 914875
                              [last_modified] =&gt; 1453494475
                          )
  
                      [6] =&gt; Array
                          (
                              [no] =&gt; 891512
                              [last_modified] =&gt; 1453493842
                          )
  
                      [7] =&gt; Array
                          (
                              [no] =&gt; 885175
                              [last_modified] =&gt; 1453489777
                          )
  
                      [8] =&gt; Array
                          (
                              [no] =&gt; 889144
                              [last_modified] =&gt; 1453487185
                          )
  
                      [9] =&gt; Array
                          (
                              [no] =&gt; 895273
                              [last_modified] =&gt; 1453486290
                          )
  
                      [10] =&gt; Array
                          (
                              [no] =&gt; 904116
                              [last_modified] =&gt; 1453486052
                          )
  
                      [11] =&gt; Array
                          (
                              [no] =&gt; 899281
                              [last_modified] =&gt; 1453484508
                          )
  
                      [12] =&gt; Array
                          (
                              [no] =&gt; 861555
                              [last_modified] =&gt; 1453492549
                          )
  
                      [13] =&gt; Array
                          (
                              [no] =&gt; 878540
                              [last_modified] =&gt; 1453504940
                          )
  
                      [14] =&gt; Array
                          (
                              [no] =&gt; 885595
                              [last_modified] =&gt; 1453481124
                          )
  
                  )
  
          )
  </code></pre>
  
  <p><strong>The problem</strong> is when I loop the inner array, It skipped odd or even numbers, so didn't get all the data I need. Also, is there a more efficient way using foreach or another loops?</p>
  
  <p>I can't wrap my head around this array structure, sorry for my lack of intelligence and written English. I appreciate the help and have a great day :).</p>