garry Posted August 8, 2008 Share Posted August 8, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/118741-help-with-an-array/ Share on other sites More sharing options...
garry Posted August 8, 2008 Author Share Posted August 8, 2008 Anyone? I would really appreciate some help with this. Quote Link to comment https://forums.phpfreaks.com/topic/118741-help-with-an-array/#findComment-611397 Share on other sites More sharing options...
MasterACE14 Posted August 8, 2008 Share Posted August 8, 2008 what type of array is it? could you show the PHP array? Quote Link to comment https://forums.phpfreaks.com/topic/118741-help-with-an-array/#findComment-611401 Share on other sites More sharing options...
garry Posted August 8, 2008 Author Share Posted August 8, 2008 Well, I'm just running var dump on the variable I get from app (it's using smarty). How can I find out what type of array? I pasted the entire contents of the var_dump Quote Link to comment https://forums.phpfreaks.com/topic/118741-help-with-an-array/#findComment-611409 Share on other sites More sharing options...
MasterACE14 Posted August 8, 2008 Share Posted August 8, 2008 is structured like... <?php $array = array(); $array[1] = "lol"; $array['lol'] = 1; or is it more like.. <?php $array { name => "value"; } Quote Link to comment https://forums.phpfreaks.com/topic/118741-help-with-an-array/#findComment-611413 Share on other sites More sharing options...
garry Posted August 8, 2008 Author Share Posted August 8, 2008 Can't you see from the code I posted? It is like this: array(5) { ["id"]=> string(1) "1" ["link"]=> string(59) "/clients/2008/august/krisskappel/category.php?id_category=1" ["name"]=> string(4) "Home" Quote Link to comment https://forums.phpfreaks.com/topic/118741-help-with-an-array/#findComment-611416 Share on other sites More sharing options...
MasterACE14 Posted August 8, 2008 Share Posted August 8, 2008 if so, you should be able to access elements like this. I think. echo $array['5']['id']; Quote Link to comment https://forums.phpfreaks.com/topic/118741-help-with-an-array/#findComment-611425 Share on other sites More sharing options...
garry Posted August 8, 2008 Author Share Posted August 8, 2008 Yeah, I know that. But what I want to be able to do is find which number (the [5]) has the information that the current category is. Where does the 5 come from? Do you know? Quote Link to comment https://forums.phpfreaks.com/topic/118741-help-with-an-array/#findComment-611426 Share on other sites More sharing options...
MasterACE14 Posted August 8, 2008 Share Posted August 8, 2008 I was using your above example... array(5) { // <-- Thats the five ["id"]=> string(1) "1" // <-- thats the one Quote Link to comment https://forums.phpfreaks.com/topic/118741-help-with-an-array/#findComment-611429 Share on other sites More sharing options...
garry Posted August 8, 2008 Author Share Posted August 8, 2008 Oh, I was talking about the whole array that I posted in my very first post. Basically, I just need to go to the array which corresponds to the category in the url (?category_id=x) I need to get to the array which has the id => x in it. Do you understand this? Quote Link to comment https://forums.phpfreaks.com/topic/118741-help-with-an-array/#findComment-611435 Share on other sites More sharing options...
wildteen88 Posted August 8, 2008 Share Posted August 8, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/118741-help-with-an-array/#findComment-611443 Share on other sites More sharing options...
garry Posted August 8, 2008 Author Share Posted August 8, 2008 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 ( ) ) ) ) ) ) Quote Link to comment https://forums.phpfreaks.com/topic/118741-help-with-an-array/#findComment-611454 Share on other sites More sharing options...
garry Posted August 8, 2008 Author Share Posted August 8, 2008 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/118741-help-with-an-array/#findComment-611573 Share on other sites More sharing options...
DarkWater Posted August 8, 2008 Share Posted August 8, 2008 I was using your above example... array(5) { // <-- Thats the five ["id"]=> string(1) "1" // <-- thats the one The 5 shows how many elements are in that array, not the index of the array. Quote Link to comment https://forums.phpfreaks.com/topic/118741-help-with-an-array/#findComment-611674 Share on other sites More sharing options...
wildteen88 Posted August 8, 2008 Share Posted August 8, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/118741-help-with-an-array/#findComment-611774 Share on other sites More sharing options...
garry Posted August 8, 2008 Author Share Posted August 8, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/118741-help-with-an-array/#findComment-612062 Share on other sites More sharing options...
garry Posted August 9, 2008 Author Share Posted August 9, 2008 Bump.. please? Quote Link to comment https://forums.phpfreaks.com/topic/118741-help-with-an-array/#findComment-612244 Share on other sites More sharing options...
wildteen88 Posted August 9, 2008 Share Posted August 9, 2008 Not sure what you mean, but it has nothing to do with my code. It must be to do with what you're doing, you'll need to post the relevant code to your problem Quote Link to comment https://forums.phpfreaks.com/topic/118741-help-with-an-array/#findComment-612282 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.