ban ban - 10 days ago 5
JSON Question

i want to display my data with foreach

i am having trouble in displaying my data

$results = curl_exec($sh);
if (curl_errno($sh)) {
echo 'Error:' . curl_error($sh);
}
$count=0;
$user = json_decode($results);

foreach($user->$count as $mydata)
{

echo $mydata->campaignId . "\n";
count++;

}


and this is my data:

[limit] => 100
[offset] => 0
[rows] => Array
(
[0] => Array
(
[actions] => Array
(
[0] => DELETE
)

[advertiserCost] => 0
[ap] => 0.52
[approvalStatus] =>
[bid] =>
[biddingStatus] => ACTIVE
[bids] => 0
[campaignCountry] => Indonesia
[campaignId] => 77c81b87-5476-4efb-be38-a58bfb8cc5b1)
[1] => Array
(
[actions] => Array
(
[0] => DELETE
)

[advertiserCost] => 0
[ap] => 3
[approvalStatus] =>
[bid] =>
[biddingStatus] => ACTIVE
[bids] => 0
[campaignCountry] => United States
[campaignId] => 1cf85acc-123c-4ef1-8986-05fcd7a7ba93)


i want to echo my each campaignid and store it in a variable. i don know the syntax if [0]->campaign id. help please. my data earlier was wrong here is the new data

Answer

On the function foreach you dont need a count, like the for function.

$results = curl_exec($sh);

if (curl_errno($sh)) {
    echo 'Error:' . curl_error($sh);
}

$user = json_decode($results, true);

foreach($user['rows'] as $mydata)
{
      echo $mydata['campaignId'] . "\n<br>";
}

Dont forget the second param for json_decode to get an array, not an object.