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).
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'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 a multidimensional array returned by drupal render function in that array i want to choose some last values
for example
$array['static_name'][number]['changes_every_time']['some_name']['value_i_need'];
is there ant way we can skip the "changes_every_time" level while printing array
is there a way we can use wild character in there
like if i want to print
echo $array['static_name'][number][*]['some_name'][value_i_need];
some thing like this
Store answer "no".
But what you can do is do a foreach over the keys of $array['static_name'][number]
foreach($array['static_name'][number] as $val) {
echo $val['some_name'][value_i_need];
}
This way you don't have to care.
well, you cold iterate the array at that "level" with a foreach loop
Untested code:
foreach ($array['static_name'][number] as $key => $value) {
echo $value['some_name'][value_i_need];
}
I have a really strange problem with range();
According to docs :
Create an array containing a range of elements
But when I do :
foreach (range(900,950,1) as $art_id){
//ob_start();
//do stuff
//do a lot more stuff
echo $art_id;
//ob_get_clean(); }
or even
$arts_id = range (900, 920);
foreach ($arts_id as $art_id){
//ob_start();
//do stuff
//do a lot more stuff
echo $art_id;
//ob_get_clean(); }
The output is strangly repeating itself in a series like
"900,900,901,900,901,902,900,901,9002,903,900..."
meaning it is comming back to the first ID after each loop.
(1st iteration -> 900
2nd iteration -> 900,901
3rd iteration -> 900,901,902
...)
When I just put a manual array it works perfectly in order and no duplicates :
$arts_id = array(900,901,902,903,904,905,906,907,908,909,910...);
What Am I doing wrong (again ?? )
EDIT I
here is the whole script :
http://pastebin.com/ZHm3ub6n
It is actually a slightly modified version of the slashdot scraping example included in the simplehtmldom script . Nothing special.
It is executed inside WP but OUTSIDE the loop ..
It must be in the rest of your code, because this works fine.
please share more of the script.
It looks like the foreach is nested within a similair foreach,
$arts_id = range (900, 920);
foreach ($arts_id as $art_id){
foreach (range (900,$art_id) as $art_id2){
echo $art_id2."<br/>";
}
}
This produces an output you've described
EDIT
Personally i'd add the the function scraping_slashdot a reset of the variable $ret just in case.
for example:
$ret = array();
Currently the echo of $output is within the loop, which creates an output like the following:
Article 1
Article 1, Article 2
Article 1, Article 2, Article 3
etc.
place echo $output outside the loop, or $ouptut = ''; inside the loop.
I've been looking into this problem for a couple of days now and I just can't seem to figure it out.
I'm trying to do something simple, I thought, just looping through an array.
This is a screenshot of the array: http://cl.ly/image/3j2J3x1C3B0j
I'm trying to loop through all the 'Skills' array, there the "Skill' array and inside that grabbing the "Icon".
For this I made 2 loops:
foreach ($hero_data['skills'] as $skills)
{
foreach ($skills as $skill)
{
//print_r($skill['skill']);
}
}
Unfortunaly this doesn't work, in laravel. I'm getting the "Undefined index: skill" error. It does work when I tried it outside , as a standalone script.
Out side both of the loops I can select the icon with:
print_r($hero_data['skills']['active'][0]['skill']['icon']);
I'm sure I'm overlooking something stupid...
Thanks a lot for the help,
Looking at what you've said from the other solutions posted here, it's clear that you are looping through the sub arrays and not all of those sub arrays contain the keys that your further loops are looking for.
Try this:
foreach ($hero_data['skills']['active'] as $skills) {
if (isset($skills['skill']['icon'])) {
print_r($skills['skill']['icon']);
}
}
Because, for example, if $hero_data['skills']['active'][8] doesn't actually have a skill array or a ['skill']['icon'] array further down, then the loop will throw the errors you have been reporting.
The nested array keys you are looking for must be found in every iteration of the loop without fail, or you have to insert a clause to skip those array elements if they aren't found. And it seems like your $hero_data array has parts where there is no ['skill'] or ['icon'], so therefore try inserting one or more isset() checks in the loops. Otherwise, you need to find a way of guaranteeing the integrity of your $hero_data array.
Your game looks interesting by the way!
Inside skills you have an 'active' attribute and it contains the array you need, so you need to change your code to this:
foreach ($hero_data['skills'] as $skills)
{
foreach ($skills['active'] as $skill)
{
//print_r($skill['skill']);
}
}
Try:
foreach ($hero_data['skills'] as $skills)
{
foreach ($skills as $skillState)
{
foreach ($skillState as $skill)
{
print_r($skill['skill']);
}
}
}
You simply need to iterate the active index of the array. this should work :
foreach ($hero_data['skills']['active'] as $skills) {
print_r($skills['skill']['icon']);
}