foreach loop modifies an array without I want - php

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 ?

Related

how to call foreach loop multiple time for extracting the data from data base

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

array isn't combining with loop in php

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.

PHP multidimensional array, getting specified data only through 1 foreach loop

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 ?

nested foreach loop array resets

how do I stop nested foreach loops from resetting the internal array pointer
for example
foreach ($example as $example2)
{
foreach ($xample as $xample2);
{
}
}
so for example if, $example & $xample contained the array (1,2,3)
i want to do something like this
foreach ($example as $example2)
{
do something with array[1]
foreach ($xample as $xample2);
{
do something else with array[1]
}
}
then go on to 2,3 ect...
hope my question is clear because from what i understand is the internal array pointer is being reset during the 2nd loop http://us2.php.net/manual/en/control-structures.foreach.php
EDIT
so this is what is happening right now
say $example contains the following values 1,2,3 & $xample contains the same values
foreach ($example as $example2)
{
echo ($example2)
foreach $xample as xample2)
{
echo ($xample2)
}
}
the output looks something like this, first loop outputs 1,2,3, the 2nd loop just outputs 1,1,1
what I want is to have, first loop 1,2,3 2nd loop 1,2,3
EDIT 2
code is pasted here http://codepad.org/r1py8HR5
the output shows that there are 5 $examples & 7 $xample being echoed
and &xample contains the same $fname2 7 times
Each array has its own array pointer. So nesting your foreach loops does not require any precautions on your side, it will work just like you coded it.
UPDATE:
In response to your edit, I think your error is somewhere else. I recreated your example and it seems to work as you would like (see http://codepad.org/nJrZqCoO).

adding an element to multidimensional array when within foreach loop (PHP)

I'm trying to check if a certain category is allready selected by looping through an array of categories also I want to add another element to the array whci is just a bit to indicate is the category selcated
my categories array looks like this
0=>array(category_id=>12,category_name=>"blogger")
1=>array(category_id=>13,category_name=>"dancer")
etc...
now the code i'm trying goes like that:
foreach ($userCategories as $key=>$category) {
if($category['category_id'] == $mediaDetails['currentCategory']) {
$category['current'] = 1;
} else {
$category['current'] = 0;
}
}
when executing
die(var_dump($userCategories));
I expect to get an array similar to
0=>array(category_id=>12,category_name=>"blogger",current=>0)
1=>array(category_id=>13,category_name=>"dancer",current=>1)
but instead I get the same array I had before the foreach loop
any ideas?
Thanks
It looks like $category is not getting passed by reference.
Try $userCategories[$key]['current']=1 instead, and see how that works.

Categories