Store foreach as variable outside the loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alioso_oopa
Forum Newbie
Posts: 5
Joined: Tue Nov 17, 2009 10:36 am

Store foreach as variable outside the loop

Post by alioso_oopa »

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
User avatar
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

Post by iankent »

Hope I've understood correctly - imo the easiest way to get a mysql result into an array is:

Code: Select all

$arr = array();
while($line = mysql_fetch_assoc($result) {
    $arr[] = $line;
}
$arr will then contain the full resultset

hth
alioso_oopa
Forum Newbie
Posts: 5
Joined: Tue Nov 17, 2009 10:36 am

Re: Store foreach as variable outside the loop

Post by alioso_oopa »

Thank you so much for your quick answer. Maybe this will help. Here is my foreach:

Code: Select all

foreach ($var->field_availability_checkboxes as $varavail) {
$vartot = $varavail['value'];
echo $vartot;
}
$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.
User avatar
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

Post by iankent »

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:

Code: Select all

$vartot = $varavail['value'];
you need to have

Code: Select all

$vartot[] = $varavail['value'];
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:

Code: Select all

$vartot = array();
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
alioso_oopa
Forum Newbie
Posts: 5
Joined: Tue Nov 17, 2009 10:36 am

Re: Store foreach as variable outside the loop

Post by alioso_oopa »

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

Code: Select all

$vartot = array();
foreach ($var->field_availability_checkboxes as $varavail) {
$vartot[] = $varavail['value'];
echo $vartot;
}
User avatar
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

Post by iankent »

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!!
alioso_oopa
Forum Newbie
Posts: 5
Joined: Tue Nov 17, 2009 10:36 am

Re: Store foreach as variable outside the loop

Post by alioso_oopa »

or could you put them all into a variable?
ex: $newvar = list($vartot[1], $vartot[2] etc...);

Thanks again!
User avatar
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

Post by iankent »

yep, two ways

Code: Select all

$vartot = array();
foreach ($var->field_availability_checkboxes as $varavail) {
$vartot[] = $varavail['value'];
}
$new_string = implode(",",$vartot);
 
will give you a string such as:
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,',');
 
again, will give you a string such as:
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

Post by alioso_oopa »

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! :D
User avatar
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

Post by iankent »

alioso_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! :D
np :) tbh implode and explode always makes me laugh, reminds me of lemmings 8O
Post Reply