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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Group array by key

    Hi,

    I have the following multidimensional array:

    PHP Code:
    $testarray = array(
       array(
    0,"group1","test1"),
       array(
    1,"group2","test2"),
       array(
    2,"group1","test3"),
       array(
    3,"group3","test4"),
       array(
    4,"group2","test5")
    ); 
    The array is dynamic so more may be added at any time i.e. the groups are not in sequence.

    I need to perform a conditional statement on the array so that each item is echoed. I currently have this:

    PHP Code:
    while (list($key,$val) = each($testarray)) {
    echo 
    $val[2];

    Which outputs as:

    Code:
    test1
    test2
    test3
    test4
    test5
    The problem is I need to keep each group together so that the output is:

    Code:
    test1
    test3
    test2
    test5
    test4
    Can anyone help me achieve this?

  • #2
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    John

  • #3
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks John,

    I've having trouble applying this to my example. I'm a total beginner


  •  

    Posting Permissions

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