Jump to content

Help with an array..


garry

Recommended Posts

Hi, I am using prestashop to make a site for a client and need some help with an array.. Basically, this array has categories and I want it to get only the subcategories for the page the user is currently on. Here's an example of the array I get:

 

array(5) {
  ["id"]=>
  string(1) "1"
  ["link"]=>
  string(59) "/clients/2008/august/krisskappel/category.php?id_category=1"
  ["name"]=>
  string(4) "Home"
  ["desc"]=>
  string(0) ""
  ["children"]=>
  array(4) {
    [0]=>
    array(5) {
      ["id"]=>
      string(1) "3"
      ["link"]=>
      string(59) "/clients/2008/august/krisskappel/category.php?id_category=3"
      ["name"]=>
      string(11) "Accessories"
      ["desc"]=>
      string(35) "Wonderful accessories for your iPod"
      ["children"]=>
      array(1) {
        [0]=>
        array(5) {
          ["id"]=>
          string(1) "6"
          ["link"]=>
          string(59) "/clients/2008/august/krisskappel/category.php?id_category=6"
          ["name"]=>
          string(11) "Subcategory"
          ["desc"]=>
          string(11) "Description"
          ["children"]=>
          array(0) {
          }
        }
      }
    }
    [1]=>
    array(5) {
      ["id"]=>
      string(1) "2"
      ["link"]=>
      string(59) "/clients/2008/august/krisskappel/category.php?id_category=2"
      ["name"]=>
      string(5) "iPods"
      ["desc"]=>
      string(110) "Now that you can buy movies from the iTunes Store and sync them to your iPod, the whole world is your theater."
      ["children"]=>
      array(0) {
      }
    }
    [2]=>
    array(5) {
      ["id"]=>
      string(1) "4"
      ["link"]=>
      string(59) "/clients/2008/august/krisskappel/category.php?id_category=4"
      ["name"]=>
      string(7) "Laptops"
      ["desc"]=>
      string(246) "The latest Intel processor, a bigger hard drive, plenty of memory, and even more new features all fit inside just one liberating inch. The new Mac laptops have the performance, power, and connectivity of a desktop computer. Without the desk part."
      ["children"]=>
      array(0) {
      }
    }
    [3]=>
    array(5) {
      ["id"]=>
      string(1) "5"
      ["link"]=>
      string(59) "/clients/2008/august/krisskappel/category.php?id_category=5"
      ["name"]=>
      string(13) "Test Category"
      ["desc"]=>
      string(0) ""
      ["children"]=>
      array(1) {
        [0]=>
        array(5) {
          ["id"]=>
          string(1) "7"
          ["link"]=>
          string(59) "/clients/2008/august/krisskappel/category.php?id_category=7"
          ["name"]=>
          string(5) "test2"
          ["desc"]=>
          string(0) ""
          ["children"]=>
          array(0) {
          }
        }
      }
    }
  }
}

 

So basically, the Home is what everything goes in, so I don't need to worry about that, it's just for the children of home I need to worry about. For example, lets look at the category "Accessories". Accessories is a main category and it's subcategory is "subcategory". Basically what I want to do is, if the user is on the accessories page already, show them the subcategories for accessories. I can get the category id for the page they're already on with $_GET['category_id']; so it's just a matter of getting the right stuff from the array. I hope i've explained this right.

 

I would really appreciate any help!

Link to comment
https://forums.phpfreaks.com/topic/118741-help-with-an-array/
Share on other sites

Rather than use var_dump to print the array can you post the output of print_r instead, it'll make the array structure more clear to read.

echo '<pre>' . print_r($your_array_var, true) . '</pre>';

 

Also can you post details of how that array is constructed.

Link to comment
https://forums.phpfreaks.com/topic/118741-help-with-an-array/#findComment-611443
Share on other sites

I'm not 100% on how the array is constructed, it's done by the application. I'm just trying to alter the results. Here's the result of your print_r:

 

Array
(
    [id] => 1
    [link] => /clients/2008/august/krisskappel/category.php?id_category=1
    [name] => Home
    [desc] => 
    [children] => Array
        (
            [0] => Array
                (
                    [id] => 3
                    [link] => /clients/2008/august/krisskappel/category.php?id_category=3
                    [name] => Accessories
                    [desc] => Wonderful accessories for your iPod
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 6
                                    [link] => /clients/2008/august/krisskappel/category.php?id_category=6
                                    [name] => Subcategory
                                    [desc] => Description
                                    [children] => Array
                                        (
                                        )

                                )

                        )

                )

            [1] => Array
                (
                    [id] => 2
                    [link] => /clients/2008/august/krisskappel/category.php?id_category=2
                    [name] => iPods
                    [desc] => Now that you can buy movies from the iTunes Store and sync them to your iPod, the whole world is your theater.
                    [children] => Array
                        (
                        )

                )

            [2] => Array
                (
                    [id] => 4
                    [link] => /clients/2008/august/krisskappel/category.php?id_category=4
                    [name] => Laptops
                    [desc] => The latest Intel processor, a bigger hard drive, plenty of memory, and even more new features all fit inside just one liberating inch. The new Mac laptops have the performance, power, and connectivity of a desktop computer. Without the desk part.
                    [children] => Array
                        (
                        )

                )

            [3] => Array
                (
                    [id] => 5
                    [link] => /clients/2008/august/krisskappel/category.php?id_category=5
                    [name] => Test Category
                    [desc] => 
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 7
                                    [link] => /clients/2008/august/krisskappel/category.php?id_category=7
                                    [name] => test2
                                    [desc] => 
                                    [children] => Array
                                        (
                                        )

                                )

                        )

                )

        )

)

Link to comment
https://forums.phpfreaks.com/topic/118741-help-with-an-array/#findComment-611454
Share on other sites

You'll have to create a function for this. You'll have to loop through the array and search for the requested id. I have come up with this

function array_multi_search($search, $array)
{
    $r = null;
    foreach($array as $key => $value)
    {
        if($key == 'id' && $value == $search)
        {
            unset($array['children']);
            return $array;
        }
        elseif(is_array($value))
        {
            $r = array_multi_search($search, $value);

            if(is_array($r))
            {
                return $r;
            }
        }
    }

    return false;

}

$result = array_multi_search(3, $arr);
echo '<pre>' . print_r($result, true) . '</pre>';

$result = array_multi_search(7, $arr);
echo '<pre>' . print_r($result, true) . '</pre>';

 

change $arr to your array variable name.

Link to comment
https://forums.phpfreaks.com/topic/118741-help-with-an-array/#findComment-611774
Share on other sites

Wow, you're a genius! That function worked great, thanks a lot.

 

Now I just have one more little problem and that's it. I'm not sure if this can be done.. but basically, when a user clicks on a sub-category, they are just assigned another id_category in the url, e.g 7. So when they're in the subcategory, the sidebar on the left will try to show the subcategories for the subcategory, which I don't want it to do. What I want it to do is still show the subcategories for the main categories (main categories are the children to home). Is there a way to do this?

Link to comment
https://forums.phpfreaks.com/topic/118741-help-with-an-array/#findComment-612062
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.