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 11 of 11
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts

    Exclamation Multi dimentional array help please!

    Hi All,

    i have 5 sets of info a category and its description i am trying to put them into 1 array and display the data using php

    here is my code
    PHP Code:
        <?php
        
    if($_SESSION['page'] == "entertainment")
        {
            
    $category_list = array( 
                             
    "Category" => array("DVD","CD","Video Games","Blu-Ray","Books"),
                                
    "Sub" => array("The latest blockbuster DVDs.""Chart hit CDs from the hottest artists.","Top video games from all the major platforms including xbox 360, ps3, nintendo wii","The latest blockbuster Blu-Ray DVDs.","Bestselling hardback, paperback titles from the best authors")
                             );
        }
    ?>

        <div id="category_left">
        <?php
        $x
    =0;
        foreach(
    $category_list as $category){
        echo 
    $category["Category"][$x];
        echo 
    $category["Category"][$x];
        
    $x++;
        }
    ?>
        </div>
    any ideas?
    thanks

  • #2
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    why not do it as simply

    Category,Sub
    Category,Sub
    Category,Sub


    instead of what your doing atm which is

    (cat,cat,cat,cat,cat),(sub,sub,sub,sub,sub)

    so it'd be something like (bit rusty here only just getting back into things after xmas break)

    PHP Code:
    $category_list = array();
    $category_list[] = array("DVD","The latest blockbuster DVDs.");
    $category_list[] = array("CD","blah");

        
    $x=0;
        foreach(
    $category_list as $category){
        echo 
    $category["Category"][$x];
        echo 
    $category["Category"][$x+1];
        
    $x++;
        } 
    (code above is actually wrong I'm just too tired and rusty to work it out ) should give you a pointer for structuring the array better though
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    thanks mate solved it

    PHP Code:
        if($_SESSION['page'] == "entertainment")
        {
        $category_list = array();
        $category_list[] = array("category"=>"DVD","desc"=>"The latest blockbuster DVDs.");
        $category_list[] = array("category"=>"CD","desc"=>"Chart hit CDs from the hottest artists.");
        $category_list[] = array("category"=>"Video Games","desc"=>"Top video games from all the major platforms including xbox 360, ps3, nintendo wii");
        $category_list[] = array("category"=>"Blu-Ray","desc"=>"The latest blockbuster Blu-Ray DVDs.");
        $category_list[] = array("category"=>"Books","desc"=>"Bestselling hardback, paperback titles from the best authors");
        }?>

        <div id="category_left">
        <?php
        $x
    =0;
        foreach(
    $category_list as $category){
        echo 
    $category["category"];
        echo 
    "<br />";
        echo 
    $category["desc"];
        echo 
    "<br />";
        
    $x++;
        }
    cheers

  • #4
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    I was just about to post my solution when I see you have solved it. Since mine is slightly different I will post it any ways.

    PHP Code:
    <?php

    $category_list 
    = array('DVD' => 'The latest blockbuster DVDs.',
    'CD' => 'Chart hit CDs from the hottest artists.',
    'Video Games' => 'Top video games from all the major platforms including xbox 360, ps3, nintendo wii',
    'Blu-Ray' => 'The latest blockbuster Blu-Ray DVDs.',
    'Books' => 'Bestselling hardback, paperback titles from the best authors');

    $counter 1;

    foreach (
    $category_list as $category => $sub_category) {

    echo 
    "$counter ";
    echo 
    "<b>$category</b><br>";
    echo 
    "$sub_category<br><hr>";
    $counter++;

    }

    ------------
    Leonard Whistler

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    Hi Len,

    thanks for your reply, its nice to see another solution will try and remomber that looks much neater than mine

    cheers
    Luke

  • #6
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    I thought about lens way its basically making the key the name of your category then you just need a single array to store your information rather than an array of arrays as i suggested
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #7
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    decided to go the way you guys suggested, it works nicely

    what would i do if i wanted to add more data to the array for example for each of the categorys how would i add a new piece of data e.g

    PHP Code:
    $category_list = array('DVD' => 'The latest blockbuster DVDs.' => 'More data here',
    'CD' => 'Chart hit CDs from the hottest artists.'=> 'More data here',
    'Video Games' => 'Top video games from all the major platforms including xbox 360, ps3, nintendo wii'=> 'More data here',
    'Blu-Ray' => 'The latest blockbuster Blu-Ray DVDs.'=> 'More data here',
    'Books' => 'Bestselling hardback, paperback titles from the best authors'=> 'More data here'); 
    then do this
    PHP Code:
        foreach ($category_list as $category => $sub_category => $test
    for example. i tried the above but could not get it to work it breaks my page.

    any help would be appreciated
    thanks
    Luke

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    bump...

  • #9
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Leonard Whistler

  • #10
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    hello mate,

    i've seen that page but because the way you showed me before is different to those on there i thought i'd continue using your solution

    or are they different because there are more items in the array?

    cheers
    Luke

  • #11
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Another solution. This is easy to read and produces nice output.


    PHP Code:
    <?php

    //array with each string a CSV (=>) seperated value.
    $category_list = array('DVD=>The latest blockbuster DVDs.=>$12.22 - $34.66',
    'CD=>Chart hit CDs from the hottest artists.=>$19.99 - $29.99',
    'Video Games=>Top video games from all the major platforms including xbox 360, ps3, nintendo wii=>$9.99 - $23.88',
    'Blu-Ray=>The latest blockbuster Blu-Ray DVDs.=>$1.55 - $12.99',
    'Books=>Bestselling hardback, paperback titles from the best authors=>$0.88 - $5.00');

    echo 
    "<table>";
    echo 
    "<th>#</th><th>Product</th><th>Description</th><th>Price Range</th>";

    $counter 1;
    foreach (
    $category_list as $category) {

    // string to array, in this case a 3 element array.
    $data explode('=>',$category);
    echo 
    "<tr><td>$counter</td><td>$data[0]</td><td>$data[1]</td><td>$data[2]</td></tr>";
    $counter++;

    echo 
    "</table>";


    ---------
    Leonard Whistler


  •  

    Posting Permissions

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