0

I have loop inception going on and I could use some help please.

I have to loop through some curl calls to an API, and get the records to then push to another API. Each call from this endpoint returns only 30 records, so I also need to loop through if there are more than 30.

I am trying to dump out an array at the end of all loops to see what I thought would be all the data, but it is only outputting 1 record.

Am I missing something dumb here?

$project_ids = array(
    '111111',
    '222222',
    );

$array = array();

foreach ($project_ids as $proj_id) {

    $go_again = true;
    $page = 1;
    $per_page = 30;

    while ( $go_again ) {

        $curl = curl_init($baseurl . $key_url);

        $keyPOSTdata = array(
            "date_format" => "d-m-Y",
            "page" => $page,
            "projects_id" => $proj_id,
        );

        curl_setopt($curl, CURLOPT_POST, false);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($keyPOSTdata));
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

        $response = curl_exec($curl);

        $result = json_decode($response);
        $total =  $result->body->total_count;
        $new_total = $total - ($per_page * $page);

        if( $total - ($per_page * $page ) > 0 ) {
            ++$page;
        }
        else {
            $page = 1;
            $go_again = false;
        }

        curl_close($curl);

        foreach ($result->body as $item) {
            $array['attribute1'] = $item->attribute1;
            $array['attribute2'] = $item->attribute2;
            $array['attribute3'] = $item->attribute3;
        }

    }

}

echo '<pre>';
print_r($array);
echo '</pre>';
exit();
0

4 Answers 4

4

You are, indeed, rewriting the array on each loop. Notice you don't initialize a new row for each record, you're only writing on the "attributeX" field over and over. This should work:

foreach ($result->body as $item) {
    $record = [];
    $record['attribute1'] = $item->attribute1;
    $record['attribute2'] = $item->attribute2;
    $record['attribute3'] = $item->attribute3;
    $array[] = $record;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Ah yes, makes sense now why rewriting was happening ha. However, I get no change in output when changing to this method.. still just one item being dumped...
I think you’re not iterating over every element of the response, only over the body of one element. You’ll probably need two foreach inside the while loop. Can you please edit in your question a sample response?
So I got it working, by moving the new $record temp array, and the $array[] assignment to inside the foreach loop...
Right. Sorry, was a bit hard doing this without looking at the actual data. Glad you got it working! I edited the answer to reflect what worked for you.
0

If you want your items per project ID, then you probably want:

foreach ($result->body as $i => $item) {
    $record[$proj_id][$i]['attribute1'] = $item->attribute1;
    $record[$proj_id][$i]['attribute2'] = $item->attribute2;
    $record[$proj_id][$i]['attribute3'] = $item->attribute3;
}

1 Comment

Not sure this would work as the end array is the format I need to push to other API so cannot be multidimensional
0

You are overwriting here!

$array['attribute1'] = $item->attribute1;
$array['attribute2'] = $item->attribute2;
$array['attribute3'] = $item->attribute3;

Every loop will overwrite the values of these values.

You can resolve this in two ways,

foreach ($result->body as $key => $item) {
    $record[$key]['attribute1'] = $item->attribute1;
    $record[$key]['attribute2'] = $item->attribute2;
    $record[$key]['attribute3'] = $item->attribute3;
}

OR

foreach ($result->body as $key => $item) {
    $record['attribute1'][] = $item->attribute1;
    $record['attribute2'][] = $item->attribute2;
    $record['attribute3'][] = $item->attribute3;
}

It depends on your requirement, how you want array.

Comments

0
$orderItemsHtml[] = array();

foreach ($order->products as $product) {
   $thisItem= [];
   $tempSKU                      = $product->product_sku;
   $tempQty                      = $product->pivot->quantity;

   $thisItem['name']             = $product->product_name;
   $thisItem['sku']              = $tempSKU;
   $thisItem['units']            = $tempQty;
   $thisItem['selling_price']    = $product->sale_price;
   
   $orderItemsHtml[]             = $thisItem;
}

Try this, should work.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.