Store foreach as variable outside the loop
Moderator: General Moderators
-
alioso_oopa
- Forum Newbie
- Posts: 5
- Joined: Tue Nov 17, 2009 10:36 am
Store foreach as variable outside the loop
Hi,
This is my first post and I hope I can find some help with my problem.
I am trying to get all results from an array with foreach function, and store it in a variable outside the loop to use it for a bunch of if else statements in the same page (using strstr to check the array), but outside the foreach brackets.
I am not sure if it is the best practice but doing so would be great help.
Please let me know if it possible in any way.
Thanks
This is my first post and I hope I can find some help with my problem.
I am trying to get all results from an array with foreach function, and store it in a variable outside the loop to use it for a bunch of if else statements in the same page (using strstr to check the array), but outside the foreach brackets.
I am not sure if it is the best practice but doing so would be great help.
Please let me know if it possible in any way.
Thanks
- iankent
- Forum Contributor
- Posts: 333
- Joined: Mon Nov 16, 2009 4:23 pm
- Location: Wales, United Kingdom
Re: Store foreach as variable outside the loop
Hope I've understood correctly - imo the easiest way to get a mysql result into an array is:
$arr will then contain the full resultset
hth
Code: Select all
$arr = array();
while($line = mysql_fetch_assoc($result) {
$arr[] = $line;
}hth
-
alioso_oopa
- Forum Newbie
- Posts: 5
- Joined: Tue Nov 17, 2009 10:36 am
Re: Store foreach as variable outside the loop
Thank you so much for your quick answer. Maybe this will help. Here is my foreach:
$vartot returns all values, but outside the brackets it only returns the last value, and I need them all.
I tried to use the mysql_fetch_assoc, but with no success.
I am still pretty new to some of the php functions.Thanks for your help.
Code: Select all
foreach ($var->field_availability_checkboxes as $varavail) {
$vartot = $varavail['value'];
echo $vartot;
}I tried to use the mysql_fetch_assoc, but with no success.
I am still pretty new to some of the php functions.Thanks for your help.
- iankent
- Forum Contributor
- Posts: 333
- Joined: Mon Nov 16, 2009 4:23 pm
- Location: Wales, United Kingdom
Re: Store foreach as variable outside the loop
Its because you've not made $vartot an array, and you're assigning the values directly to it instead of to a new item in the array. E.g., where you're saying:
you need to have
the [] part tells php to put the value on the right ($varavail['value']) into a new element in the array, whereas without it you're just overwriting the previous value.
Strictly speaking you should also do this before your loop:
hth 
edit: sorry about the mysql_fetch_assoc bit - I'd incorrectly assumed (through not reading properly :p) that you were trying to get the items from a mysql result into an array
Code: Select all
$vartot = $varavail['value'];Code: Select all
$vartot[] = $varavail['value'];Strictly speaking you should also do this before your loop:
Code: Select all
$vartot = array();edit: sorry about the mysql_fetch_assoc bit - I'd incorrectly assumed (through not reading properly :p) that you were trying to get the items from a mysql result into an array
-
alioso_oopa
- Forum Newbie
- Posts: 5
- Joined: Tue Nov 17, 2009 10:36 am
Re: Store foreach as variable outside the loop
this is what i had done at the beginning, from what I could gather in the manual. I did exactly that and created the empty array before the loop, and put the brackets after my variable.
And somehow i always end up with 'Array' as a value for all results. That's why I was getting confused and posted here. btw, this forum in incredible. Thanks!
this is what i have now
And somehow i always end up with 'Array' as a value for all results. That's why I was getting confused and posted here. btw, this forum in incredible. Thanks!
this is what i have now
Code: Select all
$vartot = array();
foreach ($var->field_availability_checkboxes as $varavail) {
$vartot[] = $varavail['value'];
echo $vartot;
}- iankent
- Forum Contributor
- Posts: 333
- Joined: Mon Nov 16, 2009 4:23 pm
- Location: Wales, United Kingdom
Re: Store foreach as variable outside the loop
That's correct - if you echo an array (as you're doing with echo $vartot) it'll display Array, because there's no way to 'echo' an array, its an object.
If you want to quickly output an array you can use print_r($vartot);, but other than that to get back at the values you'll need to use another foreach, while, or access them by index (e.g. $vartot[0], $vartot[1], etc.)
and yes, this forum is great, only found it yesterday and wish I'd found it years ago!!
If you want to quickly output an array you can use print_r($vartot);, but other than that to get back at the values you'll need to use another foreach, while, or access them by index (e.g. $vartot[0], $vartot[1], etc.)
-
alioso_oopa
- Forum Newbie
- Posts: 5
- Joined: Tue Nov 17, 2009 10:36 am
Re: Store foreach as variable outside the loop
or could you put them all into a variable?
ex: $newvar = list($vartot[1], $vartot[2] etc...);
Thanks again!
ex: $newvar = list($vartot[1], $vartot[2] etc...);
Thanks again!
- iankent
- Forum Contributor
- Posts: 333
- Joined: Mon Nov 16, 2009 4:23 pm
- Location: Wales, United Kingdom
Re: Store foreach as variable outside the loop
yep, two ways
will give you a string such as:
a,b,c,d,e,f
alternatively:
again, will give you a string such as:
a,b,c,d,e,f
hth
Code: Select all
$vartot = array();
foreach ($var->field_availability_checkboxes as $varavail) {
$vartot[] = $varavail['value'];
}
$new_string = implode(",",$vartot);
a,b,c,d,e,f
alternatively:
Code: Select all
$vartot = array();
foreach ($var->field_availability_checkboxes as $varavail) {
$new_string .= $varavail['value'] . ',';
}
$new_string = rtrim($new_string,',');
a,b,c,d,e,f
hth
-
alioso_oopa
- Forum Newbie
- Posts: 5
- Joined: Tue Nov 17, 2009 10:36 am
Re: Store foreach as variable outside the loop
implode! exactly what I needed. I spend bit of time with the mighty manual, but sometimes there is nothing like being helped by somebody knowledgeable... Finally got it to work. thanks! 
- iankent
- Forum Contributor
- Posts: 333
- Joined: Mon Nov 16, 2009 4:23 pm
- Location: Wales, United Kingdom
Re: Store foreach as variable outside the loop
npalioso_oopa wrote:implode! exactly what I needed. I spend bit of time with the mighty manual, but sometimes there is nothing like being helped by somebody knowledgeable... Finally got it to work. thanks!