How to Display a PHP Array Properly?

Discussion in 'PHP' started by mokimofiki, Dec 14, 2011.

  1. #1
    Im not sure how to take the array below and echo each value set individually. Any help would be great.

    Array
    (
        [0] => Array
            (
                [catid] => 89
                [entryid] => 383
                [title] => Adult R&B
            )
    
        [1] => Array
            (
                [catid] => 89
                [entryid] => 384
                [title] => Classic Soul
            )
    
        [2] => Array
            (
                [catid] => 89
                [entryid] => 572
                [title] => Early Funk
            )
    
        [3] => Array
            (
                [catid] => 89
                [entryid] => 573
                [title] => Female Soul and R&B
            )
    
        [4] => Array
            (
                [catid] => 89
                [entryid] => 385
                [title] => Funk
            )
    
        [5] => Array
            (
                [catid] => 89
                [entryid] => 574
                [title] => Hot R&B
            )
    Code (markup):
    The array above is stored in a variable called $programlisting and should be displayed as their own line i.e.:

    Adult R&B has a Category ID of 89 and an Entry ID of 383
    Classic Soul has a Category ID of 89 and an Entry ID of 384
    etc.

    Thank you in advance.
     
    Solved! View solution.
    mokimofiki, Dec 14, 2011 IP
  2. #2
    To echo the array in it's entirety:
    print_r($array);
    PHP:
    And to echo a certain 'key' (as they're called):
    echo $array[52]; //echo's the 51st (counting 0, first value) value in the array
    PHP:
    What you've got is a multidimensional array. These are arrays with arrays inside. If you wanted to echo out 'Classic Soul', you'd use
    echo $programlisting[0]['title'];
    PHP:
    That means you need to loop through the array. Try playing with the join() command in PHP, then parsing it to your liking, if that's what you mean.

    Hope I could help!
     
    Jaxo, Dec 14, 2011 IP
  3. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #3
    Thank you ... I actually just ended up exploding the array into parts needed to pull them in as needed although your 3rd example is basically the same thing I did
     
    mokimofiki, Dec 15, 2011 IP
  4. pipsoup

    pipsoup Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You can use print_r($arr) or checking the php manual to have more method for array printing.
     
    pipsoup, Dec 30, 2011 IP