Link to home
Start Free TrialLog in
Avatar of FrankTech
FrankTech

asked on

Detect How Many Times a Two-dimensional Array Contains a Value

I need something like the PHP function in_array , but for multidimensional arrays.

    I have a PHP array named $rowarray, which is an array of arrays (array of table rows). Simplified example:

Array ( [0] =>
   Array ( [0] => WinXP [Version] => WinXP [1] => Pro [Category] => Pro)
   Array ( [0] => Win98 [Version] => Win98 [1] => SP1 [Category] => SP1)
   Array ( [0] => WinXP [Version] => WinXP [1] => Pro [Category] => Pro)
 )

I need to detect whether the overall 2-dimensional array contains a given value somewhere in the array. Actually I need to know how many times it appears, if at all.

With a flat array, it's easy to detect whether the array contains a value or not:

     if (in_array("WinXP", $array)) {
        echo "Contains WinXP";
     }

But that does not report how many times the value appears in the array, and it does not work on 2-dimensional arrays.

How can I detect how often (if at all) a given value appears in the overall 2-dimensional array?  E.g., I want to find out: "How many rows contain the value WinXP ?"
ASKER CERTIFIED SOLUTION
Avatar of favoretti
favoretti
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
IF I'm understanding correctly, it seems you could initialize counter variables and then run a loop over your array of arrays, right?  It's different if you can go deeper than 2-dim arrays, but assuming you just have 2-dim arrays you might try something like:


$XP_count=0;
//initialize other counters here too
 
foreach($array_of_arrays as $array=>$contents){
        if(in_array("WinXP", $array)){
              $XP_count++;
       }
       //run the same stmt for all variables you're looking for
}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of FrankTech
FrankTech

ASKER

favoretti and rowejd,
    I am testing both suggestions and can't get either to work so far. They always report 0, even though I know the row arrays contain the value.  I'll try again now with a different array. Thanks.
Both were returning 0 because I was actually searching for part of a value. For example, instead of searching for "WinXP", I was searchig for "XP".
    And actually, that is what I need to do: be able to find not only the whole value (e.g. "WinXP") but any part of the value (e.g. "XP").
    So I guess I mis-stated the question. I should have asked, how to find how many "rows" contain a value that contains a specified string? (E.g., how many rows contain that string within a value?)
    How would I do that?
To complicate matters, I actually need to know whether the string appears within the value of a certain element - and ignore whether it appears in others.
    For example, if one of the sub-arrays (rows) is:

Array ( [0] => Win98 [Version] => Win98 [1] => SP1 [Category] => SP1)

then I need to ignore any occurance of the string if it is in [1].  I only want to detect whether it appears in [0].  (There could be situations where it appears in both, but I only want to know if it appears in a specified one.)
You could use the same idea we gave you above, but instead of testing whether that full value is in the array, you could do your if statement with the function strpos(); - which returns a true or false if it finds a substring within a larger string.  http://us.php.net/strpos

Now I'm a bit confused by the way you list your arrays above, so you might have to tweak this code to find your values, but this is a general idea.
$XP_count=0;
//initialize other counters here too
 
foreach($array_of_arrays as $array=>$contents){
        foreach($contents as $name=>$value){
            if(strpos('XP', $name)){
                $XP_count++;
            }
       }
       //run the same stmt for all variables you're looking for
}

Open in new window

$find = 'xp';

if ( in_array ( strpbrk ( $array, $find) , $array ) ) {
>>>then I need to ignore any occurance of the string if it is in [1].  I only want to detect whether it appears in [0].  (There could be situations where it appears in both, but I only want to know if it appears in a specified one.)

WHen you use a foreach loop, it strips your array down another level, right?  So as many dimensions as you have you can keep saying:

foreach($array as $name=>$value){
   //do something
}

If you don't want to return a count for WinXP if it occurs in [1] or something, then just run an if statement against the $name in your foreach loop.
I simplified it and reversed the order of strpos arguments, and it seems to work. How about this?

$XPcount=0;
 
foreach($rowarray as $row) {
     if ( strpos($row[0], 'XP') !== false ) {
         $XPcount++;
     }
}

echo $XPcount;
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Aye, sorry, was on my way home, couldn't correct the solution :)

But the last piece of code looks perfectly correct.