I have two-dimensional array $id[][] and i'm accessing data by using two foreach loops
foreach ($id as $index=>$val)
{
foreach ($val as $ind=>$id_val)
{
...
}
}
but how to do that without first foreach, so looping process would start and end on specified index, for example $id[1]["and now looping with foreach second dimension"]. Number of records on second dimension may differ.
How to achieve that, like in other programming languages with just simply putting index of 1st dimension and beginning loopiing on second only ?
Related
I don't understand what happens, I have an array of associative arrays, and I want to iterate through it so I made a foreach loop but this loop modified my initial array and I don't know why, loot at the code :
1st case
var_dump($modules);
$tab = $modules;
foreach ($tab as $module){
var_dump($module);
}
The result :
On the left, is the initial array and at the right : the different values of the array. As you can see, it doesn't show the second one item but only the one. If I show the $tab inside of the loop, it is just an array of 2 items containing twice copies of the "jojo" item .
2nd case
However, if I write this :
for($i=0;$i<count($tab);$i++){
var_dump($tab[$i]);
}
It works fine as I want
and if I execute the for loop after the foreach loop the result is the same as the 1st case. Do you knwo why ?
I want to extract array of data multiple times by using foreach() loop from database in php, I have one scenario given below.
Note $results is containing database data :
$values = array(10,20,30,40);
$datas = $results;
foreach($values as $value) {
//some operations
foreach($datas as $data)
{
$time = $data->time;
// some operation
}
}
Here by doing DB-query I am extracting these data to "$results"
1528126233440,41602,48914,98,124,0,0,0
1528126248393,41602,48914,98,124,0,0,0
1528126251809,41602,48914,98,124,0,0,0
1528126256843,41602,48914,98,124,0,0,0
I have two different arrays
$values = array(10,20,30,40) // used for external loop
$datas = $results //used for internal loop
where as "$reasults" contains the database data.
lets say
for 1st iteration of external loop and when it execute internal loop I need this value [ [1528126233440, 41602], [1528126248393, 41602], [1528126251809, 41602], [1528126256843, 41602] ]
for 2nd iteration of external loop and wehen it execute internal loop I need this value [ [1528126233440, 98], [1528126248393, 98], [1528126251809, 98], [1528126256843, 98] ]
like this..
so I my case the first iteration of external loop I am able to print [ [1528126233440, 41602], [1528126248393, 41602], [1528126251809, 41602], [1528126256843, 41602] ]
when it comes to the 2nd iteration of external loop the internal loop is not processing.
I need in each iteration of external foreach() loop the inner foreach() loop should run every time**.
But in my case for the first iteration the external loop is working, when it comes to second iteration of external loop at that time the second loop is not working.
Any suggestion will great help for me
Thank you
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.
I have this object containing one row and many keys/variables with numbered names.
I need to pass each of them one at a time to another function. How do I loop through the keys instead of the rows?
The code would look like this:
foreach ($object['id'] as $row):
$i++;
$data['myInfo'][$i] = $this->get_data->getInfo('data1', 'id', $row->{'info'.$i.'_id'});`
but this obviously won't work since it's looping through the rows/instances of an object, and I have only one row in my $object['id'] object (with info1_id, info2_id, info3_id, info4_id... etc keys), so the loop stops after just one cycle. And I really don't feel like typing all of that extra code by hand, there's gotta be solution for this. :)
You can just iterate through your object like an array :
foreach ($object['id'] as $row) {
foreach ($row as $k => $v) {
$id = substr($k, 4, strpos($k, '_')-4);
$data['myInfo'][$id] = $this->get_data->getInfo('data1', 'id', $v);`
}
}
Thanks for the directions Alfwed, I didn't know you could use foreach loop for anything other than instances of an object or arrays (only started learning php a week or so ago), but now it looks pretty straight forward. that's how I did it:
foreach ($object['id'] as $row):
foreach ($row as $k=>$v):
$i++;
if ($k == print_r ('info'.$i.'_id',true)){
$data['myinfo'][$i] = $this->get_db->getRow('products', 'id','info'.$i.'_id');
<...>
in my case, I knew how many and where were those values, so I didn't have to worry about index values too much.
This question already has answers here:
How do you remove an array element in a foreach loop?
(6 answers)
Closed 6 years ago.
Hi I am trying to remove an array element using a foreach loop, but it does nothing. I need the index to be completely gone rather than making it null. Here is what I have tried:
foreach ($_SESSION['cart'] as &$arrays3) {
if($arrays3['id'] == $id){
unset($arrays3);
}
}
Note, the array value for each key contains an associative array.
You need to use the key from your foreach, and unset the variable directly (from the session):
foreach ($_SESSION['cart'] as $key => $arrays3) {
if($arrays3['id'] == $id){
unset($_SESSION['cart'][$key]);
}
}
Unsetting $arrays3 or any of its children will only be effective until the next iteration of the foreach loop, when it will be set again.
You are using a dangerous form of the foreach loop. You MUST always unset the reference variable after the loop:
foreach ($_SESSION['cart'] as &$arrays3) {}
unset($arrays3);
Otherwise things will break if that loop is used again.
And the reference really is not needed. foreach operates on a copy of the array, so changes to the key or value will not go back into the original array, but you can always access the original array, as shown in the answer of #scrowler.