have to execute a call to an API x amount of times. The call returns one array for each loop and I have to append the arrays together for one master array. I won't know how many times the loop.

$pages is the amount of times I need to loop.

 for ( $i = 0; $i< $pages; $i++ ) {
         $searchcustomers[$i] = $sc->call('GET', '/admin/customers.json?limit=5&page=$i');
               $mastercustomerlist = array_merge_recursive($searchcustomers[$i]);
                   }

I know this is totally wrong. I'm not sure how to get that master list and append the arrays automatically in the loop without knowing the name

    Well, you've already found [man]array_merge_recursive[/man]; [man]array_merge[/man] is simpler and does what you want - which is merge the existing master list with the new-retrieved list to get a new master list.

      When I use array_merge and out out the $mastercustomerlist it doesn't append the second array to the first. It just displays the first array

        These functions take at least 2 arguments, you're calling it with only one. The functions merged the arguments together and returns the result. Calling it with only one array creates nothing to merge.

          This is my code, but what it's doing is just repeating the first array not appending the second to the first. I know there are 2 arrays in this loop:

          $allCustomers = array(); for ($i = 1; $i <= $pages; $i += 1) { 
          
          $searchcustomers[$i] = $sc->call('GET','/admin/customers.json?limit=250&page=$i&fields=first_name'); $allCustomers = array_merge($allCustomers, $searchcustomers[$i]); 
          
          } 
          
          echo "<pre>"; print_r($allCustomers); echo "</pre>";
            omarel;11054147 wrote:

            This is my code, but what it's doing is just repeating the first array not appending the second to the first. I know there are 2 arrays in this loop:

            $allCustomers = array(); for ($i = 1; $i <= $pages; $i += 1) { 
            
            $searchcustomers[$i] = $sc->call('GET','/admin/customers.json?limit=250&page=$i&fields=first_name'); $allCustomers = array_merge($allCustomers, $searchcustomers[$i]); 
            
            } 
            
            echo "<pre>"; print_r($allCustomers); echo "</pre>";

            Your URL to fetch is in a single quoted string, which does not do variable interpolation, so you are requesting the LITERAL page "$i". I'm guessing on the other end it doesn't recognize this input and defaults to returning page 1. If you want to request a page equal to the $i in the current iteration of the loop you'll need to either use double quoted string or concatenation.

              Derokorian;11054149 wrote:

              Your URL to fetch is in a single quoted string, which does not do variable interpolation, so you are requesting the LITERAL page "$i". I'm guessing on the other end it doesn't recognize this input and defaults to returning page 1. If you want to request a page equal to the $i in the current iteration of the loop you'll need to either use double quoted string or concatenation.

              OMG!! Thank you so much! That was it. This was driving me crazy. Thank you!

                Write a Reply...