Jump to content

Filtering array


Andrew R

Recommended Posts

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  :D

Link to comment
https://forums.phpfreaks.com/topic/229471-filtering-array/
Share on other sites

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]

Link to comment
https://forums.phpfreaks.com/topic/229471-filtering-array/#findComment-1182274
Share on other sites

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;
}

Link to comment
https://forums.phpfreaks.com/topic/229471-filtering-array/#findComment-1182275
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.