Ichier » coding » ichier's coding corner
{ php }  
  array_extract
date 2004
update 2004
code <?php
function array_extract($array,$getkeys,$depth=0) {    //Ichier 2004
    
if($depth==0) {
        foreach(
$array as $key => $val) {
            if(
is_array($getkeys)) {
                if(
in_array($key,$getkeys)) { $res[$key] = $val; }
            } else {
                if(
$key==$getkeys) { $res $val; }
            }
        }
        return 
$res;
    }
    while (
$depth>0) {
        --
$depth;
        foreach(
$array as $val) {
            
$tmp[] = array_extract($val,$getkeys,$depth);
        }
        
$array $tmp;
        unset(
$tmp);
    }
    return 
$array;
}
?>
description extract parts from an array recoursiveley by keys only to a certain dpth. only the keys given will remain.
syntax <?php
array = array_extract(array haystack, array needleint maxdepth);
?>
explanation suppose you have the array:
<?php
    $array
["bands"]["abc"][0]["name"] = "Butthole Surfers";
    
$array["bands"]["abc"][0]["hobby"] = "Music";
    
$array["bands"]["abc"][1]["name"] = "Bj÷rk";
    
$array["bands"]["abc"][2]["name"] = "Brainless Wankers";
    
$array["bands"]["def"][0]["name"] = "Deftones";
    
$array["bands"]["mno"][0]["name"] = "Mr. Bungle";
    
$array["albums"]["abc"][0]["name"] = "Between Heaven and Hell";
    
$array["albums"]["def"][0]["name"] = "Dummy";
    
$array["albums"]["def"][1]["name"] = "Fantastic Spikes Through Balloons";
    
$array["genre"]["abc"][0]["name"] = "Alternative";
?>

and you were searchiong for every stuff of 'abc':
<?php
    $array 
array_extract($array,"abc",1);
?>

you would get:
<?php
    $array
[0][0]["name"] = "Butthole Surfers";
    
$array[0][0]["hobby"] = "Music";
    
$array[0][1]["name"] = "Bj÷rk";
    
$array[0][2]["name"] = "Brainless Wankers";
    
$array[1][0]["name"] = "Between Heaven and Hell";
    
$array[2][0]["name"] = "Alternative";
?>