Andrew R Posted March 3, 2011 Share Posted March 3, 2011 Hi there I have the following array . $products = array( array( Code => "CF1", Items => "Sand Tray", Cat => 'Tray', ), array( Code => "CF2", Items => "Mobile Computer Table", Cat => 'Table', ), array( Code => "CF3", Items => "General Service Trolley", Cat => 'Trolley', ), array( Code => "CF4", Items => "TV Trolley", Cat => 'Trolley', ), array( Code => "CF5", Items => "Overhead Projector Trolley", Cat => 'Trolley', ), array( Code => "CF6", Items => "Book Trolley", Cat => 'Tolley', ), array( Code => "CF7", Items => "Stacking Chairs", Cat => 'Chairs', ), ); What I want to do is select each by 'Cat'. For example I simply want to display all the trolleys. I was wondering how I do this? What functions would I use in PHP? Thanks a million Quote Link to comment https://forums.phpfreaks.com/topic/229471-filtering-array/ Share on other sites More sharing options...
bh Posted March 3, 2011 Share Posted March 3, 2011 Hi, try with array_walk_recursive function. Quote Link to comment https://forums.phpfreaks.com/topic/229471-filtering-array/#findComment-1182272 Share on other sites More sharing options...
ZacTopher Posted March 3, 2011 Share Posted March 3, 2011 I would just use a foreach foreach ($products as $product) { $newProductsArray[$product['Cat']][] = $product; print_r($newProductsArray); } this would produce an array like this Array ( [Tray] => Array ( [0] => Array ( [Code] => CF1 [items] => Sand Tray [Cat] => Tray ) ) [Table] => Array ( [0] => Array ( [Code] => CF2 [items] => Mobile Computer Table [Cat] => Table ) ) [Trolley] => Array ( [0] => Array ( [Code] => CF3 [items] => General Service Trolley [Cat] => Trolley ) [1] => Array ( [Code] => CF4 [items] => TV Trolley [Cat] => Trolley ) ) ) [/code] Quote Link to comment https://forums.phpfreaks.com/topic/229471-filtering-array/#findComment-1182274 Share on other sites More sharing options...
Kieran Menor Posted March 3, 2011 Share Posted March 3, 2011 To be honest, it would seem like you would me much better off using a database. For a specific solution, you could do something like this: function filterByCat($dataset, $cat) { $result = array(); foreach($dataset as $row) { if($row['Cat'] == $cat) { $result[] = $row; } } return $result; } Quote Link to comment https://forums.phpfreaks.com/topic/229471-filtering-array/#findComment-1182275 Share on other sites More sharing options...
ZacTopher Posted March 3, 2011 Share Posted March 3, 2011 Boom.dk: then you wouldn't need that function at all. You would just do that where you fetch the data Quote Link to comment https://forums.phpfreaks.com/topic/229471-filtering-array/#findComment-1182276 Share on other sites More sharing options...
Kieran Menor Posted March 3, 2011 Share Posted March 3, 2011 Duh. The function was a solution to his specific problem, since he doesn't seem to be using a database. Quote Link to comment https://forums.phpfreaks.com/topic/229471-filtering-array/#findComment-1182278 Share on other sites More sharing options...
ZacTopher Posted March 3, 2011 Share Posted March 3, 2011 Well still you're showing him something that is overkill Quote Link to comment https://forums.phpfreaks.com/topic/229471-filtering-array/#findComment-1182283 Share on other sites More sharing options...
Kieran Menor Posted March 3, 2011 Share Posted March 3, 2011 Not really. The topic title is "Filtering array". In his post he mentions that he would like get all the entries that have a certain "Cat" value. My function does exactly that. Quote Link to comment https://forums.phpfreaks.com/topic/229471-filtering-array/#findComment-1182287 Share on other sites More sharing options...
ZacTopher Posted March 3, 2011 Share Posted March 3, 2011 yes but first you want him to get the info from the database then send the fetched array to another function instead of just using that function to do it all Quote Link to comment https://forums.phpfreaks.com/topic/229471-filtering-array/#findComment-1182290 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.