Page 1 of 1

Looping through an array

Posted: Sat Jul 02, 2005 12:25 pm
by is_blank
If someone could help me learn this, I'd be quite grateful! I've never written a loop in PHP before, let alone tried what I'm about to explain. There may also be an array function to do what I need; I'm just not sure what to use. Here's the problem:

I'll have several incoming arrays containing values that I want to peel off into two main arrays to work with later. For instance:

Code: Select all

$some_stuff = array('TAG', 'text', 'TAG2', 'text2', 'TAG3', 'text3',...);
$some_other_stuff = array ('TAG4', 'text4', 'TAG5', 'text5',...);
From that, I need to generate two major arrays:

Code: Select all

$tags = array('TAG', 'TAG2', 'TAG3', 'TAG4', 'TAG5',...);
$content = array('text', 'text2', 'text3', 'text4', 'text5',...);
I imagine I need to simply loop through $some_stuff, for instance, and push the first element into $tags and the next element into $content, repeating until I'm out of $some_stuff, right?

Could anyone show me how to set up such a loop?
Thanks!

Posted: Sat Jul 02, 2005 12:36 pm
by Chris Corbyn
Well if your arrays are always perfectly formed like that, a foreach() loop with the keys pulled in will be great since we can do odds & evens like so:

Code: Select all

$some_stuff = array('TAG', 'text', 'TAG2', 'text2', 'TAG3', 'text3');
$some_other_stuff = array ('TAG4', 'text4', 'TAG5', 'text5');

//Arrays to store off into
$tags = array();
$content = array();

//Loop over the array
foreach ($some_stuff as $key => $value) {
    if ($key%2) { //Is EVEN key number (is divisable by 2)
        $tags[] = $value;
    } else {
        $content[] = $value;
    }
}
If it was just coincedence that your values alternate tag/content/tag/content/tag/content... then the foreach loop needs something else inside it. Generally speaking, if you want to loop over an array foreach() is the best solution ;)

Posted: Sat Jul 02, 2005 1:00 pm
by is_blank
Yes, I forgot to mention (kind of crucial): the arrays *will* always be formed that way, otherwise something is *broken*, so no problem there. Thanks for the fast help, I'll give this a try!

Posted: Sat Jul 02, 2005 1:32 pm
by is_blank
Oops. And a question: do the square brackets signify anything sepcial? i.e. $blah = $array[]?

Posted: Sat Jul 02, 2005 1:41 pm
by Chris Corbyn
is_blank wrote:Oops. And a question: do the square brackets signify anything sepcial? i.e. $blah = $array[]?
How do I explain this simply? Hmm...

OK, arrays use square brackets to indentify the keys. $arrayname['key'] = 'value'; type of thing.

You can assign a key like I just did in the above line, that's called an associative array (associative since they key is asscoiated with that value) and you'd call the value back by refferring to $arrayname['key'].

Now, typically, if we don't give a key name like that and just do $arrayname[] (leave the brackets empty), then PHP will assign a numeric key to it, starting at zero and incrementing up by one for each new value we add to the array.

So lets say I did this:

Code: Select all

$foo = array();

$foo[] = 'value1';
$foo[] = 'value2';
$foo[] = 'value3';

$foo['keyname'] = 'value4';
now, 'value1' exists in $foo[0], 'value2' exists in $foo[1], 'value3' exists in $foo[2] and 'value4' is in $foo['keyname'].

Not sure that came out too well but I hope it *kind of* makes sense ;)

Posted: Sat Jul 02, 2005 1:59 pm
by timvw

Posted: Sat Jul 02, 2005 2:11 pm
by is_blank
Gotcha. I see it there, now! (I had been using array_push()...this is much shorter!)

Thanks, guys! :D

Posted: Sat Jul 02, 2005 4:58 pm
by bokehman
is_blank wrote:i.e. $blah = $array[]?
You cant do that. You can only use $array[] in write mode not in read mode as in your example. You need to specify a key when in read mode: $blah = $array[key].