Grzegorz
Grzegorz

Reputation: 49

How to display big array with filtering

I have got one big array.
The content of that array is:

Array
(
    [0] => Array
        (
            [id] => 12
            [user_id] => 1
            [date] => 2013-10-21 23:01:52
            [type] => 1
            [quantity] => 0
            [value] => 1700
        )

    [1] => Array
        (
            [id] => 13
            [user_id] => 1
            [date] => 2013-10-21 23:01:52
            [type] => 0
            [quantity] => 0
            [value] => 90
        )

    [2] => Array
        (
            [id] => 16
            [user_id] => 1
            [date] => 2013-10-21 23:01:52
            [type] => 0
            [quantity] => 0
            [value] => 0

    [3] => Array
        (
            [id] => 19
            [user_id] => 1
            [date] => 2013-10-31 02:49:12
            [type] => 0
            [quantity] => 0
            [value] => 0
            [bills] => Array
                (
                    [0] => Array
                        (
                            [id] => 5
                            [data_id] => 19
                            [quantity] => 10
                            [value] => 15
                        )

                    [1] => Array
                        (
                            [id] => 5
                            [data_id] => 19
                            [quantity] => 20
                            [value] => 1
                        )

                    [2] => Array
                        (
                            [id] => 5
                            [data_id] => 19
                            [quantity] => 1
                            [value] => 50
                        )

                )

        )

)

I want to display this array in foreach. So i have:

echo '<ol>';
foreach ( $this->data as $d )
{
    echo '<li><strong>'.$d['name'].'</strong><br /></li>';

    if ( $d['bills'] )
    {
        echo '<ul>';
        foreach ( $d['bills'] as $b )
        {
            echo '<li>';
            echo $b['name'];
            echo '</li>';
        }
        echo '</ul>';
    }
}
echo '</ol>';

It's simple, until I want to display only arrays containing a key['type'] == 1. I have no idea how can I do that.

In MySQL it's only have to add 'WHERE type = 1'. I learn about PHP arrays so sorry if that filtering is simple function to do. Best cheers!

Upvotes: 0

Views: 70

Answers (2)

John Conde
John Conde

Reputation: 219804

You can use a simple IF statement with continue to check to see if type equals 1. If it doesn't you can skip it.

foreach ( $this->data as $d )
{
    if ($d['type'] != 1)  continue;

Upvotes: 1

George Brighton
George Brighton

Reputation: 5151

If you actually need to create a filtered array, you could use array_filter() with a callback:

$filtered = array_filter($this->data, function($element) {
    return $element['type'] == 1;
});

Otherwise, the simplest solution is probably just to put something like this at the top of your foreach loop:

if($d['type'] != 1) {
    continue;
}

Upvotes: 2

Related Questions