Page 1 of 1

Only show array values if they match a condition

Posted: Fri Oct 15, 2010 12:53 pm
by clavigne
Hi all,

This seems like it would be straightforward, but I can't get it to work.

I have a multidimensional array where a container array contains an 'item' array that holds data arranged into their own arrays... like this:

Code: Select all

Array
(
    [item] => Array
        (
            [0] => Array
                (
                    [id] => 10548
                    [type] => valuex
                    [info] => text

The array structure is not mine and I can't alter it.

I'd like to loop through the array and show the [item][#][info] entries, but only for entries where the [item][#][type] is a certain value.

I'd also like to only show 10 entries at a time in some kind of pagination system, which I have already worked out.

I just can't get the array to sort itself the way I'd like using for and if statements. Something I thought would work gives me an 'execution time exceeded' warning:

Code: Select all

for($i=0; $i<=10;) {
    if ($array['item'][$i]['type'] == "valuex") {
        echo $array['item'][$i]['info']."<br />";
        $i++;
    }
}
The array has more than 700 entries, but it's not something I can alter.

Any ideas?

Re: Only show array values if they match a condition

Posted: Fri Oct 15, 2010 3:21 pm
by josh

Re: Only show array values if they match a condition

Posted: Fri Oct 29, 2010 10:44 pm
by clavigne
Thank you... I got it working from the examples given for that function!