Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Search multidimensional array?

Hello all,

After completing shirts4mike with PHP arrays I thought I'd give it a go with a website I'm currently building using a larger array. I haven't started with MySQL yet but I'm trying to find a way to loop through and search the array elements but I can't figure it out.

Lets say that all the products are displayed on the page, and when one is clicked, it should redirect to a product.php?id page with a GET variable to show the product information relating to that product. Is there a way of doing this? I can use a foreach loop but I have to specify the subset array such as main catergory, then subcat, then cat1, then code etc. Is there like a wildcard I can use for looping through each subset array or something like that?

I hope that makes sense, I understand that using PHP arrays is probably not the best approach for something like this but as I said I haven't started with MySQL yet sooo :)

Any help or advice would be appreciated :)

Thanks

Adam

  $products = array();
        $products["Main Category"] = array(
            "subcat" => array (
                "cat1" => array (
                    "101" => array (
                        "code" => "102",
                        "name" => "Product 1"
                    ),
                    "102" => array (
                        "code" => "102",
                        "name" => "Product 2"
                    )
                ),
                "cat2" => array (
                    "101" => array (
                        "code" => "102",
                        "name" => "Product 1"
                    ),
                    "102" => array (
                        "code" => "102",
                        "name" => "Product 2"
                    )
                )
            )
        );
        $products["Second Category"] = array(
            "subcat" => array (
                "cat1" => array (
                    "101" => array (
                        "code" => "102",
                        "name" => "Product 1"
                    ),
                    "102" => array (
                        "code" => "102",
                        "name" => "Product 2"
                    )
                ),
                "cat2" => array (
                    "101" => array (
                        "code" => "102",
                        "name" => "Product 1"
                    ),
                    "102" => array (
                        "code" => "102",
                        "name" => "Product 2"
                    )
                )                                     
            )
        );

3 Answers

Peter Ramsing
Peter Ramsing
16,814 Points

I'm not sure I'm understanding the question but what is the end goal?

You're trying to display all the products and then if a user clicks on a product they are taken to a more detailed descriptor? Could you maybe elaborate on your bigger picture for this? There are a lot of ways to go about with what you're doing but what one is best can be determined by what you're trying to ultimately do.

Thanks for the reply,

Basically as you said it, when a user clicks on a product it takes them to a page where they can see more detailed information about it. I'm pretty much confused about the best way of going about it. I was going to use GET variables which can be used to check if the product exists in the products array and then show that information on products.php page when the user clicks on a product. But how would I check if the GET variable value matches a product id from the products array? I can only seem to get it to work by typing something like:

$get = $_GET['id'];

if (isset($products["Main Category"]["subcat"][$get])){
  //show product info
}

It's like I have to go into the subset arrays to check if the product exists, I was wondering if there is a better way around it, something like:

$get = $_GET['id'];

if (isset($products[$get])) {
  //show product info
}

But that won't work, is there a way of doing something like array_search to loop through all the subset arrays to try and find a product matching the id in the GET variable?

I hope that helps :)

Peter Ramsing
Peter Ramsing
16,814 Points

I think where you're finding your confusion is how you're storing your data. I might suggest storing it differently. Potentially storing all the data in one array and using keys for the categories.

I've seen Kevin Korte have some good insight on php sorts of things. I wonder if he has a better direction than me.

I'm partial to pushing data to a database and querying it but that is a bit larger of a task than I think you're going for.

Does this help if you put in some get values and if conditions.

<?php
 $products = array();
        $products["Main Category"] = array(
            "subcat" => array (
                "cat1" => array (
                    "101" => array (
                        "code" => "102",
                        "name" => "Product 1"
                    ),
                    "102" => array (
                        "code" => "102",
                        "name" => "Product 2"
                    )
                ),
                "cat2" => array (
                    "101" => array (
                        "code" => "102",
                        "name" => "Product 1"
                    ),
                    "102" => array (
                        "code" => "102",
                        "name" => "Product 2"
                    )
                )
            )
        );


        foreach($products as $maincat => $value){

            echo $maincat;
           echo "<br />";
            $value;

            foreach($value as $subcat => $val2){

                 echo $subcat;
                 echo "<br />";               
                $val2;

               foreach($val2 as $cat => $val3){
                echo $cat;
                echo "<br />";
               $val3;

               foreach($val3 as $prodnum => $val4){
                echo $prodnum;
                echo "<br />";
               $val4;

                foreach($val4 as $prod => $val5 ){

                    echo $prod;
                    echo " = ";
                    echo $val5;
                    echo "<br />";
                    }
               }
               }


        }
        }       

        ?>

Thanks for the reply,

Didn't think of doing it that way, makes more sense haha!

I'll give it a go :)

Adam