note 42101 added to function.array-search
| From: | softexpert at rn2 dot php dot net | Date: | Mon, 03 May 2004 15:51:09 +0000 |
| Subject: | note 42101 added to function.array-search | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-69151@lists.php.net to get a copy of this message | ||
I use this for searching for a value in a bidimensional array .
<?php
function SearchBiDimArray(&$theArray, $dimNo, $searchValue, $returnIndex = true){
if(is_array($theArray)){
$keys = array_keys($theArray[0]);
$key = $keys[$dimNo];
$elcount = count($theArray);
for($i=0; $i < $elcount; $i++){
if($theArray[$i][$key] === $searchValue){
if ($returnIndex){
return $i;
}
else{
return $theArray[$i];
}
}
}
}
else{
return array_search($searchValue, $theArray);
}
}
$theArray = array();
$theArray[0]['firstproperty'] = 'avalue1';
$theArray[0]['secondproperty'] = 'anothervalue1';
$theArray[1]['firstproperty'] = 'avalue2';
$theArray[1]['secondproperty'] = 'anothervalue2';
$theArray[2]['firstproperty'] = 'avalue3';
$theArray[2]['secondproperty'] = 'anothervalue3';
print SearchBiDimArray($theArray, 1, 'anothervalue2', true);
// result is 1
print SearchBiDimArray($theArray, 1, 'anothervalue2', true);
// result is
//Array
//(
// [firstproperty] => avalue2
// [secondproperty] => anothervalue2
//)
?>
----
Manual Page -- http://www.php.net/manual/en/function.array-search.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+42101
Delete -- http://master.php.net/manage/user-notes.php?action=delete+42101&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+42101&report=yes
Search -- http://master.php.net/manage/user-notes.php