dongluoqiu0255
dongluoqiu0255
2015-05-09 21:02
浏览 94
已采纳

在多维数组中查找并删除$ key的所有元素

Im trying to search and delete multiple values in a multidimensional array. I have tried to kind of mix it with a multiDim Search. I pass the array &$haystack by reference.

This should probably go in a do while loop, but as it stands it will go in a endless loop.

But nothing happens

$b = array(0 => array("patient" => 123, "condition" => "abc"), 
           1 => array("patient" => 987, "condition" => "xyz"),
           2 => array("patient" => 123, "condition" => "zzz"));



function in_array_r($needle, &$haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
          unset($haystack["patient"]);            
          return true;
        }
    }

    return false;
}


echo in_array_r(123, $b) ? 'found' : 'not found';

Print_r($b);

Expected Result

Array
(
    [1] => Array
        (
            [patient] => 987
            [condition] => xyz
        )

)

图片转代码服务由CSDN问答提供 功能建议

我试图在多维数组中搜索和删除多个值。 我试图将它与一个混合 multiDim Search。 我通过引用传递数组& $ haystack

这可能应该在 do while 循环中进行, 但是现在它会进入无限循环。

但没有任何反应

  $ b = array(0 => array)  (“patient”=> 123,“condition”=>“abc”),
 1 => array(“patient”=> 987,“condition”=>“xyz”),
 2  => array(“patient”=> 123,“condition”=>“zzz”)); 
 
 
 
函数in_array_r($ needle,& $ haystack,$ strict = false){\  n foreach($ haystack as $ item){
 if(($ strict?$ item === $ needle:$ item == $ needle)||(is_array($ item)&& in_array_r($ needle,  $ item,$ strict))){
 unset($ haystack [“patient”]);  
返回true; 
} 
} 
 
返回false; 
} 
 
 
echo in_array_r(123,$ b)?  'found':'not found'; 
 
Print_r($ b); 
   
 
 

预期结果 <前> 数组 ( [1] =&gt;数组 ( [病人] =&gt; 987 [条件] =&gt; xyz ) )

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doutuoji8418
    doutuoji8418 2015-05-09 21:13
    已采纳

    You don't need to pass by reference since you're not trying to change a value in an array. But you're on the right track! Here's a working example:

    $patients = array(0 => array("patient" => 123, "condition" => "abc"), 
               1 => array("patient" => 987, "condition" => "xyz"),
               2 => array("patient" => 123, "condition" => "zzz"));
    
    function remove_patient($patients, $number) {
        foreach ($patients as $key => $patient) {
            if ($patient['patient'] == $number) {
                unset($patients[$key]);
            }
        }
        return $patients;
    }
    

    And the example results:

    var_dump(remove_patient($patients, 123));
    
    array(1) {
      [1]=>
      array(2) {
        ["patient"]=>
        int(987)
        ["condition"]=>
        string(3) "xyz"
      }
    }
    
    点赞 评论
  • dswfyq6201
    dswfyq6201 2015-05-09 21:16
    function in_array_r($needle, &$haystack, &$count = 0)
    {
        foreach($haystack as $index => $data)
        {
            if(is_array($data))
            {
                foreach($data as $key => $value)
                {
                    if(is_array($value))
                        in_array_r($needle, $value, $count);
                    else
                    {
                        if($value === $needle)
                        {
                            unset($haystack[$key]);
                            $count++;
                        }
                    }
                }
            }
        }
    
        return $count > 0;
    }
    
    $count = 0;
    

    123:

    echo (in_array_r(123, $b, $count) ? "found (".$count ." times)" : "not found") . "
    ";
    

    Output:

    found (2 times)
    

    1123:

    echo (in_array_r(1123, $b, $count) ? "found (".$count ." times)" : "not found") . "
    ";
    

    Output:

    not found
    
    点赞 评论

相关推荐