Jump to content

[SOLVED] Filter values in multidimensional array?


Recommended Posts

Hi I'm trying to filter my multidimensional array based of the ID passed. I'm almost there but I missed a step somewhere. Here is the code below when I try to filter the array by ID =1 ie: product.php?id=1

 

Array
      (
          [brand] => Array
              (
                  [Type] => Array
                      (
                          [0] => Array
                              (
                                  [iD] => 1
                                  [Name] => Plus
                                  [size] => XL
                                  [Price] => 100


                              )
                          [1] => Array
                              (
                                  [iD] => 3
                                  [Name] => Shox
                                  [size] => XL
                                  [Price] => 100


                              )
                          [2] => Array
                              (
                                  [iD] => 2
                                  [Name] => Zoom
                                  [size] => XL
                                  [Price] => 100


                              )

                          [3] => Array
                              (
                                  [iD] => 1
                                  [Name] => Plus
                                  [size] => XL
                                  [Price] => 100


                              )

                      )

              )

      )

function array_filter_multi($input, $filter="", $keepMatches=true) {
        if (!is_array($input))
                return ($input==$filter xor $keepMatches==false) ? $input : false;
        while (list ($key,$value) = @each($input)){
                $res = array_filter_multi($value, $filter,$keepMatches);
                if ($res !== false)
                        $out[$key] = $res;
        }
        return $out;
}


$newArray = array_filter_multi($result['Brand']['Type'], $filter= $_GET[id], $keepMatches=true);

print_r($newArray);

 

 

Gives me the output of:

 

Array ( [0] => Array ( [iD] => 1 ) [1] => Array ( [iD] => 1 ) )

 

It's just giving me the ID's and not the full array with ID, Name, Size, & Price...

 

Any thoughts? Thx

Well, without your code, its pretty difficult to tell you whats wrong with it. However, i put this together which i think will do what you're after (and possibly more):

 

<?php
$array = array('Brand'=>array('Type'=>array(array('ID'=>1,'Name'=>'Plus','Size'=>'XL','Price'=>100),array('ID'=>3,'Name'=>'Shox','Size'=>'XL','Price'=>100))));
function find_items($array,$findwhat,$value,$found=array()){
foreach($array as $k=>$v){
		if(is_array($v)){
		 	$result = find_items($v,$findwhat,$value,$found);
			if($result === true){
				$found[] = $v; 	
			}else{
				$found = $result;
			}
		}else{
			if($k==$findwhat && $v==$value){
				return TRUE;
			}
		}
}
return $found;

}
$found = find_items($array,'ID',1);
echo '<pre>'.print_r($found,1).'</pre>';
?>

 

This will allow you to search for any product where the attribute matches what you're after. So, in the above example, we can search for a product where the ID is 1. Or, we could search for all products where the size is XL, using:

 

$found = find_items($array,'ID',1);
echo '<pre>'.print_r($found,1).'</pre>';

 

If this isn't what you want, please explain a bit more fully. And post up the code you've got currently.

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.