SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2007
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multidimensional array with 'header' - how to traverse?

    Hi,

    been pulling my hair out with this array issue. Here's what I've got:


    $my_array = array("Heading1",array("value1","value2","value3"),
    "Heading2", array("value4","value5"),
    "Heading3", array("value6","value7","value8"));

    question is, how can I loop throguh this array and get, in turn, each heading and each value in the sub-arrays?

    Thanks in advance.

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,407
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    Use a foreach loop. If the value inside the loop is an array, use another foreach loop to get the values. If not, it's a header.

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2007
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you!
    That's a simple solution ...

    I was using foreach's inside foreach's and rapidly disappearing up my own fundament.

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    55 Post(s)
    Tagged
    0 Thread(s)
    How about re-thinking the overall array structure (temporarily) ?

    PHP Code:
    foreach (array_chunk($my_array2) as $item) {
        list(
    $header$array) = $item;
        
    // ...

    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2007
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    How about re-thinking the overall array structure (temporarily) ?

    PHP Code:
    foreach (array_chunk($my_array2) as $item) {
        list(
    $header$array) = $item;
        
    // ...

    if only jellyhead (me) could get his head around that!

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,407
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by wellyfish View Post
    if only jellyhead (me) could get his head around that!
    http://www.php.net/manual/en/function.array-chunk.php

    It takes two elements of the array at once. Since you've got header-valuearray-header-valuearray-etc, taken two elements means you get an array which contains a header and an array of values.

    http://www.php.net/list
    With list you then assign the header and the value array to two distinct variables, the first one contains the header, the second the value array.

    After that, you can elaborate each as you like.

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2007
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, I get it now - a more elegant solution perhaps.

    Thanks guys!

  8. #8
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    55 Post(s)
    Tagged
    0 Thread(s)
    @wellyfish, apologies if the code was confusing or unfamiliar to you. I tend to assume, perhaps mistakenly, that folks will go directly to the manual to read up on functions that they don't know about. (e.g. http://php.net/array_chunk)

    Thanks guido2004 for the descriptions.
    Salathe
    Software Developer and PHP Manual Author.

  9. #9
    SitePoint Zealot
    Join Date
    Dec 2007
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    @wellyfish, apologies if the code was confusing or unfamiliar to you. I tend to assume, perhaps mistakenly, that folks will go directly to the manual to read up on functions that they don't know about. (e.g. http://php.net/array_chunk)

    Thanks guido2004 for the descriptions.
    There's no need to apologise mate.
    Just grateful for the help and your elegant solution.

  10. #10
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,407
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Salathe View Post
    Thanks guido2004 for the descriptions.
    Thank you for the array_chunk. I tend to forget the existence of many PHP functions. A very elegant solution indeed


Bookmarks

Posting Permissions

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