removing an element from multidimentional array php
I am trying to remove an element from a multidimentional array in PHP. Here is the code :
<?php
$tset = "ST3";
$gettc = "gettingtc1";
$getbid = "gettingbid1";
$getresultsid = "gettingresid1";
$users[$tset] = array();
$users[$tset][] = array( "testcase"=>"$gettc",
"buildid"=>"$getbid",
"resultsid"=>"$getresultsid"
);
$tset = "TEJ";
$gettc = "ggettingtc2";
$getbid = "gettingbid2";
$getresultsid = "gettingresid2";
$users[$tset][] = array( "testcase"=>"$gettc",
"buildid"=>"$getbid",
"resultsid"=>"$getresultsid"
);
$tset = "ST3";
$gettc = "ggettingtc12";
$getbid = "gettingbid13";
$getresultsid = "gettigresid14";
$users[$tset][] = array( "testcase"=>"$gettc",
"buildid"=>"$getbid",
"resultsid"=>"$getresultsid"
);
foreach ($users as $val => $yy)
{
echo "For $val the array is :";
foreach($yy as $uy)
{
echo $uy['testcase'开发者_StackOverflow社区].$uy['buildid'].$uy['resultsid'];
}
echo '<br>';
}
$ser = "gettingresid1";
$to = array_searchRecursive($ser,$users);
if($to <> 0)
{
print_r($to);
}
else
{
echo "not";
}
function array_searchRecursive( $needle, $haystack, $strict=true, $path=array() )
{
if( !is_array($haystack) ) {
return false;
}
foreach( $haystack as $key => $val ) {
if( is_array($val) && $subPath = array_searchRecursive($needle, $val, $strict, $path) ) {
$path = array_merge($path, array($key), $subPath);
return $path;
} elseif( (!$strict && $val == $needle) || ($strict && $val === $needle) ) {
$path[] = $key;
return $path;
}
}
return false;
}
?>
Where I am stuck is : $to
holds the array that has my search element. But the results $to
is holding should be removed from the original array $users
.
Any help.
Thanks.
I think you want to use unset()
//assuming $to contains the key in $users that needs to be removed
//from the array
unset($users[$to]);
But as $to
contains an array of keys to the element rather than a single key to the element, you would need to write your own functions to do what you want.
The function that does what you want is below, and will remove the element of the given array which has the address in $keys.
/**
* Removes the element with the keys in the array $keys
*
* @param haystack the array that contains the keys and values
* @param keys the array of keys that define the element to remove
* @return the new array
*/
function array_remove_bykeys( array $haystack, array $keys ){
//check if element couldn't be found
if ( empty($keys) ){
return $haystack;
}
$key = array_shift($keys);
if ( is_array($haystack[$key]) ){
$haystack[$key] = array_remove_bykeys($haystack[$key], $keys);
}else if ( isset($haystack[$key]) ){
unset($haystack[$key]);
}
return $haystack;
}
The other method would be to delete all the keys with the value you were looking for.
/**
* Removes all elements from that array that has the value $needle
* @param $haystack the origanal array
* @param $needle the value to search for
* @return a new array with the value removed
*/
function array_remove_recursive( array $haystack, $needle ){
foreach( $haystack as $key => $value ) {
if( is_array($value) ) {
$haystack[$key] = array_remove_recursive($value, $needle);
} else if ( $value === $needle ){
unset($haystack[$key]);
}
}
return $haystack;
}
For completeness (Although defiantly not recommended), here is a eval version:
$keys = array(...);//an array of keys
//$arr is the variable name that contains the value that you want to remove
eval ('unset($arr[\''.implode('\'][\'', $keys).'\']);');
Pass $users to array_searchRecursive by reference (add '&' to $haystack):
function array_searchRecursive( $needle, &$haystack, $strict=true, $path=array()
and then in array_searchRecursive, just before each return statement:
unset($haystack[$key]);
精彩评论