0

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'].$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.

2
  • You have a multidimensional array $users, which you search for a specific element and if this element is found, you want to have it removed from $users, right? Just trying to understand what you are asking. Commented Jan 25, 2010 at 12:16
  • Yes, Thats exactly what I want Commented Jan 25, 2010 at 12:20

2 Answers 2

2

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).'\']);');
Sign up to request clarification or add additional context in comments.

1 Comment

Warning: Illegal offset type in unset in C:\xampplite\htdocs\scripts\multi_arrays.php on line 65
0

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]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.