Author Topic: Foreach loop trough two dimensional array  (Read 902 times)

0 Members and 1 Guest are viewing this topic.

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Foreach loop trough two dimensional array
« on: January 17, 2014, 02:50:13 pm »
Greetings EZ members. I recently started learning PHP and I understand how foreach loop work on one dimensional array, but I failed to understand how that can be used on two dimensional array. I have code like this:

Code: (php) [Select]
<?php
$person = array(
array("Michael","14","New York"),
array("Peter","21","London"),
array("John","30","Moskva")
);
?>
Is there any way to call members of the array to be presented like this:

Michael is 14 years old and he live in New York.
Peter is 21 years old and he live in London.
John is 30 years old and he live in Moskva.
Vae Victis - suffering to the conquered

Offline proxx

  • Avatarception
  • Global Moderator
  • Titan
  • *
  • Posts: 2803
  • Cookies: 256
  • ФФФ
    • View Profile
Re: Foreach loop trough two dimensional array
« Reply #1 on: January 17, 2014, 03:33:50 pm »
Not perse php specific but bascially you can nest loops to extract data the same way one could populate them.
Wtf where you thinking with that signature? - Phage.
This was another little experiment *evillaughter - Proxx.
Evilception... - Phage

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Re: Foreach loop trough two dimensional array
« Reply #2 on: January 17, 2014, 04:46:23 pm »
Could you please show me that on one example?
Vae Victis - suffering to the conquered

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Foreach loop trough two dimensional array
« Reply #3 on: January 17, 2014, 04:59:51 pm »
A loop inside a loop is what proxx meant.
Here's some pseudocode

Nested loops:
Code: [Select]
foreach (item_in_array) {
    foreach (item_in_array) {
        // do shit
    }
}

or what works in your case:

Code: [Select]
foreach (item_in_array) {
        print item[0] is item[1] years old and he live in item[2].
    }
}
« Last Edit: January 17, 2014, 05:00:05 pm by Kulverstukas »

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Re: Foreach loop trough two dimensional array
« Reply #4 on: January 17, 2014, 05:04:32 pm »
Code: (php) [Select]
<?php
        $person 
= array(
        array(
"Michael","14","New York"),
        array(
"Peter","21","London"),
        array(
"John","30","Moskva")
        );
       foreach (
$person as $item) echo "{$item[0]} is {$item[1]} years old and he live in {$item[2]} <br />";
?>
I did it, thank you both, it worked perfectly!!! May the cookies be with you guys. :D
Vae Victis - suffering to the conquered

Offline ande

  • Owner
  • Titan
  • *
  • Posts: 2664
  • Cookies: 256
    • View Profile
Re: Foreach loop trough two dimensional array
« Reply #5 on: January 17, 2014, 05:04:41 pm »
Code: [Select]
foreach (item_in_array) {
        print item[0] is item[1] years old and he live in item[2].
    }
}

[gist]anonymous/8475967[/gist]


EDIT: Awww, too late. Oh well.
« Last Edit: January 17, 2014, 05:05:20 pm by ande »
if($statement) { unless(!$statement) { // Very sure } }
https://evilzone.org/?hack=true