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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.