Only show array values if they match a condition

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
clavigne
Forum Newbie
Posts: 7
Joined: Tue May 11, 2010 10:53 am

Only show array values if they match a condition

Post 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?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Only show array values if they match a condition

Post by josh »

clavigne
Forum Newbie
Posts: 7
Joined: Tue May 11, 2010 10:53 am

Re: Only show array values if they match a condition

Post by clavigne »

Thank you... I got it working from the examples given for that function!
Post Reply