Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post

    Foreach on multidimensional array

    Hi everyone,

    Probably a simple question, however good old Google couldn't help.

    I have this array :

    PHP Code:
    $array['name'][1]='John';
    $array['birthday'][1]='25 June';

    $array['name'][2]='Mary';
    $array['birthday'][2]='13 May'
    I am trying to write a foreach script that will display :

    name : John
    birthday : 25 June
    name : Mary
    birthday : 13 May

    Simple. In other words, I need the key(?) echoed, in this case "name" or "birthday" and then the value, and for each numerical index [1,2,3,4,etc]

    I've got this code so far, but it shows the numerical index and not the string index :

    PHP Code:
    foreach ($array as $tempone) {
        foreach (
    $tempone as $key=>$temptwo) {
          echo 
    "$key: $temptwo""\n";
        }
        echo 
    "\n";
      } 
    This code currently, and incorrectly (for my needs) outputs :

    0 : John
    1 : Mary
    0 : 25 June
    1 : 13 May

    Could anyone help me out?

    Thanks a lot!
    Last edited by DR.Wong; 08-23-2009 at 06:39 PM. Reason: added output of current code
    -DR.Wong

    Wheres the food at?

  • #2
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    foreach($array['name'] as $key => $name)
    {
        echo 
    $name.$array['birthday'][$key];    
    }
    ?> 

  • #3
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post
    Thanks for the reply phil!

    But lets assume as well that we won't know exactly what parameters there will be in the array.

    For example, there could be name, birthday and address.
    -DR.Wong

    Wheres the food at?

  • #4
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    <?php
    $array
    ['name'][1]='John'
    $array['birthday'][1]='25 June'
    $array['address'][1]='123 made up avenue';
    $array['name'][2]='Mary'
    $array['birthday'][2]='13 May';
    $array['address'][2]='1 road thing';

    print_r($array);
    foreach(
    $array['name'] as $key => $name)
    {
        echo 
    $name.$array['birthday'][$key].$array['address'][$key];    
    }
    ?>
    no worries, im here to serve (since you did have ago yourself).

  • #5
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    or

    PHP Code:
    <?php
    $name 
    = array('phil''luke''john');
    $age = array('12''13''14');
    $address = array('123 thing close''124 thing close''125 thing close');

    foreach(
    $name as $key => $name)
    {
        echo 
    $name." ".$age[$key]." ".$address[$key]."<br />\n";    
    }
    ?>
    Last edited by Phil Jackson; 08-23-2009 at 06:59 PM. Reason: cant spell

  • #6
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post
    Thanks again for the reply.

    The new code also assumes that we know exactly what parameters there are going to be in the array.

    Perhaps I did not provide enough info.

    I have a class that reads information out of an XML file.

    The class reads the info from the XML file into a multi dimensional array.

    The catch is that not all of the entries in the XML file will have the same data.

    For example, the XML file contents may appear as :

    Code:
    <contact>
    <name>John</name>
    <birthday>25 June</birthday>
    </contact>
    
    <contact>
    <name>Mary</name>
    <birthday>14 May</birthday>
    </contact>
    
    <contact>
    <name>Andrew</name>
    <birthday>3 August</birthday>
    <address>1 Made up avenue</address>
    </contact>
    
    <contact>
    <name>George</name>
    <birthday>3 March</birthday>
    <address>1 Fake Street</address>
    <telephone>555-5555</telephone>
    </contact>
    You see, the fields will be random. There is no way of knowing what is going to end up in the array.
    -DR.Wong

    Wheres the food at?

  • #7
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    yeah but do you know what the max amount of fields would be and what POTENTIALLY could be there??

  • #8
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    surely with your script that sorts data into array you can, if no data exists, store as null?
    then if set as null dont display info for that field?

    EDIT:
    after further reading, you XML would have to have a blank field to do that. i.e. <address></address> to store as null.
    Last edited by Phil Jackson; 08-23-2009 at 07:09 PM.

  • #9
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post
    I guess we could make it a max of say 10 fields (for now).

    But I have no idea what the fields could potentially be.

    This is for an estate agency and they correspond with others and store an xml database of their associates.

    Each associate has some sort of registration number that has a different name, I myself don't understand it, but the way that the database exists at the moment is something like :

    <contact>
    <ctv_no>34434324234</ctv_no>
    </contact>

    Where ctv_no sits, it could be ANYTHING else.

    Also, a lot of them have multiple numbers.

    So the script has to be expandable.

    UPDATE :

    I'm just writing the web-display side of this.. Some other program generates all of this.

    Sucks I know
    Last edited by DR.Wong; 08-23-2009 at 07:18 PM.
    -DR.Wong

    Wheres the food at?

  • #10
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    also take a look at this i just wrote for ya!

    PHP Code:
    $data "
    <contact>
    <name>John</name>
    <birthday>25 June</birthday>
    </contact>

    <contact>
    <name>Mary</name>
    <birthday>14 May</birthday>
    </contact>

    <contact>
    <name>Andrew</name>
    <birthday>3 August</birthday>
    <address>1 Made up avenue</address>
    </contact>

    <contact>
    <name>George</name>
    <birthday>3 March</birthday>
    <address>1 Fake Street</address>
    <telephone>555-5555</telephone>
    </contact>
    "
    ;
    if(
    preg_match_all("#<contact>((?!<\/contact>).)*#is"$data$results))
    {
        foreach(
    $results[0] as $foo)
        {
            echo 
    $foo."<br /><br />";    
        }
    }
    ?> 

  • Users who have thanked Phil Jackson for this post:

    DR.Wong (08-23-2009)

  • #11
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    output being:


    John 25 June

    Mary 14 May

    Andrew 3 August
    1 Made up avenue


    George 3 March
    1 Fake Street
    555-5555


    you could run many tests on $foo to get what data you need.

  • #12
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    thank me as much as you want

  • #13
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post
    Haha... and he knows he's the man already!!!!!!!

    THANK YOU SO MUCH!
    -DR.Wong

    Wheres the food at?

  • #14
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    no probs my man!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •