I have a foreach loop here:
foreach($decode_rewardsAPIResponse->rewardUnlockStatus as $key=>$value){
echo $value->rewardID;
}
what I am trying to achieve is to be able to get the next item in the loop's rewardID. I have tried using next($value->rewardID) but had no luck.
Here is a sample of the data i'm receiving:
{"currentMilestone":2,"rewardUnlockStatus":[{"rewardID":1,"stepsRequired":0,"unlockedForUser":true},{"rewardID":2,"stepsRequired":0,"unlockedForUser":true},{"rewardID":3,"stepsRequired":2200,"unlockedForUser":false},{"rewardID":4,"stepsRequired":5200,"unlockedForUser":false},{"rewardID":5,"stepsRequired":8200,"unlockedForUser":false},{"rewardID":6,"stepsRequired":11200,"unlockedForUser":false},{"rewardID":7,"stepsRequired":14200,"unlockedForUser":false},{"rewardID":8,"stepsRequired":17200,"unlockedForUser":false},{"rewardID":9,"stepsRequired":20200,"unlockedForUser":false},{"rewardID":10,"stepsRequired":23200,"unlockedForUser":false},{"rewardID":11,"stepsRequired":26200,"unlockedForUser":false},{"rewardID":12,"stepsRequired":29200,"unlockedForUser":false},{"rewardID":13,"stepsRequired":32200,"unlockedForUser":false},{"rewardID":14,"stepsRequired":35200,"unlockedForUser":false},{"rewardID":15,"stepsRequired":38200,"unlockedForUser":false}]}
Related
Looking for some advise with regards to multidimensional Arrays, pushing the vars into a POST call within a foreach loop.
I currently have a foreach loop containing an IF clause:
foreach ($responseData['data'] as $key => $value) {
if (strtotime($value['attributes']['created_time']) > $currentDateMinusThirtyMinutes) {
array_push($emails, $value['attributes']['email']);
}
}
This is supposed to fetch the Email for all arrays which meet the condition of being created in the last 30 minutes.
However, now i am stuck, I need to fetch multiple other variables and call on a POST call similar to the below:
https://developer.example.co.za/{$PAGE}/create?reference={$ID}¤cy=ZAR&amount={$AMOUNT}&firstname={$FIRST}&lastname={$LAST}&email={$EMAIL}&sendmail=true
What i need to know is, is there a better way to treat the foreach loop?
Is foreach the right way to go?
In essence I would need to run through each of the $responseData['data'] returns and make a POST call to the above URL.
Any advise would be appreciated.
Fixed this by creating a Data_array which stored the values I required, then adding the POST call inside the foreach loop which then did the trick.
The foreach ended up looking like this:
foreach ($responseData['data'] as $key => $value) {
if (strtotime($value['attributes']['created_time']) > $currentDateMinusThirtyMinutes) {
$data_array[] = array(
"id" => $value['attributes']['id'],
***add rest of vars you want***
);
***Add POST call or whatever else you want to do***
}
}
Thanks #darklightcode for pointing me to the idea of splitting out just the variables I wanted.
I don't have enough knowledge about this criteria:
I want to loop inside the for each loop to get all values in a particular column.
For example: I got the values from DB through get_result and store the result in $results.
After that use:
for each($results as $result)
❴
$output = $result->message
❵
Where message is a column in DB.
I want to loop over all the messages instead of storing last one by replacing.
Can you please give me suggestions on how to loop inside for each?
Try this:
$output[] = $result ->message;
Now $output will contain all messages on index 0, 1, 2 ...
You are facing the issue because:
$output=$result ->message;
the above line is present inside the loop, and each new iteration onerride the old value.
Well if you just looking for foreach inside foreach then you can try the following.
<?php
foreach($results as $result){
$output=$result->message;
foreach($output as $messages){
echo $messages;
}
}
?>
You don't need to put the message into another variable. You can do whatever you need to do inside the loop. For example, if you are displaying the messages, you can get it done inside the loop:
foreach ($results AS $result) {
echo $results->message . "<br>";
}
In PHP I am getting an array from foreach. I am JSON encoding and showing all the results at once. This works fine. But what if I need to display array items one by one as foreach still continues?
<?php
$array = //somearray;
$data_arr = array();
foreach($array as $arr){
//do something
$data_arr[] = $arr;
}
echo json_encode(array('success'=>true,'data'=>$data_arr));
//here i can display the data in my jquery using each function
//i can display the whole data at once after the foreach is completed
//what i need is i want to display first element of data_arr after its loop is completed and then the second element and so on
?>
Did you try http://www.php.net/array_shift ? its php function you need to call this function after foreach
<?php
foreach($array as $arr){
//do something
$data_arr[] = $arr;
}
print_r array_shift($data_arr);
//Print First array value from this $data_arr list.
?>
Thanks.
What you looking for is Streaming Response.
Checkout following link to learn how to do that.
https://www.sitepoint.com/php-streaming-output-buffering-explained/
i've a array problem couldn't just solve it:
here's the code that is already in a foreach loop. i'm getting the $row->class_id value from this loop. it works fine and get me the total students row within an array and i preserved it in $total_student_of_this_class variable. i used another foreach loop for storing the result of first loop and then second loop and so on. but this loop gives only first loop result.
i need to combine the all array of total iteration of the loop.
$total_student_of_this_class =
$this->db->select('student_id')->where('class_id',
$row->class_id)->get('student')->result_array();
echo count($total_student_of_this_class);
// prints 2 in the first row of the output table and 5 in the second
row for me.
$total_student = array();
foreach ($total_student_of_this_class as $tt)
{
$total_student[] = $tt;
}
echo '<pre>';
print_r($total_student_of_this_class);
echo '</pre>'
echo count($total_student);
// prints only 2 outside the loop (not 7)
pls someone help me.
The problem here is seems to be with $total_student = array();: initializing array inside an other loop it always starts it from zero values array, so you get only results from one iteration inside it and doesn't collect all datas.
You can also look at array_merge php function.
foreach loop only repeating the last element of the arary?
java Script Code :
<?php foreach($_REQUEST["itemprice"] as $itemprice)
{
?>
+ "&itemname_price[]=<?php echo $itemname?>_price=" + <?=$itemname?>_price.value
<?php
}
?>
Code to request that data
$items_price = array();
foreach($_REQUEST['itemname_price'] as $itemname_pric) {
$items_price[]=$itemname_pric;
}
print_r($items_price);
foreach is used to get the values from an array, and what you are trying here is to get data from a specific array key. I guess that's why you are getting a single value. You can refer to
http://php.net/manual/en/control-structures.foreach.php to get the exact use of foreach.
Let me know if you have anything to know.