Looping through an array

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
User avatar
is_blank
Forum Commoner
Posts: 36
Joined: Sat Jun 25, 2005 6:05 pm
Location: Tennessee, USA

Looping through an array

Post 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!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;)
User avatar
is_blank
Forum Commoner
Posts: 36
Joined: Sat Jun 25, 2005 6:05 pm
Location: Tennessee, USA

Post 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!
User avatar
is_blank
Forum Commoner
Posts: 36
Joined: Sat Jun 25, 2005 6:05 pm
Location: Tennessee, USA

Post by is_blank »

Oops. And a question: do the square brackets signify anything sepcial? i.e. $blah = $array[]?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

User avatar
is_blank
Forum Commoner
Posts: 36
Joined: Sat Jun 25, 2005 6:05 pm
Location: Tennessee, USA

Post by is_blank »

Gotcha. I see it there, now! (I had been using array_push()...this is much shorter!)

Thanks, guys! :D
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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].
Post Reply