Page 1 of 1

Store foreach as variable outside the loop

Posted: Tue Nov 17, 2009 10:44 am
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

Re: Store foreach as variable outside the loop

Posted: Tue Nov 17, 2009 10:54 am
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

Re: Store foreach as variable outside the loop

Posted: Tue Nov 17, 2009 11:50 am
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.

Re: Store foreach as variable outside the loop

Posted: Tue Nov 17, 2009 11:56 am
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

Re: Store foreach as variable outside the loop

Posted: Tue Nov 17, 2009 12:07 pm
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;
}

Re: Store foreach as variable outside the loop

Posted: Tue Nov 17, 2009 12:09 pm
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!!

Re: Store foreach as variable outside the loop

Posted: Tue Nov 17, 2009 1:27 pm
by alioso_oopa
or could you put them all into a variable?
ex: $newvar = list($vartot[1], $vartot[2] etc...);

Thanks again!

Re: Store foreach as variable outside the loop

Posted: Tue Nov 17, 2009 1:32 pm
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 :)

Re: Store foreach as variable outside the loop

Posted: Tue Nov 17, 2009 1:38 pm
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

Re: Store foreach as variable outside the loop

Posted: Tue Nov 17, 2009 1:39 pm
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