多维数组在php中的数组中查找给定值



阵列

Array ( 
  [0] => Array ( 
            [0] => Array ( [planoption_id] => 1 ) 
            [1] => Array ( [planoption_id] => 2 ) 
            [2] => Array ( [planoption_id] => 3 ) 
            [3] => Array ( [planoption_id] => 4 ) 
            [4] => Array ( [planoption_id] => 5 ) 
            [5] => Array ( [planoption_id] => 6 ) 
            [6] => Array ( [planoption_id] => 7 ) 
            [7] => Array ( [planoption_id] => 53 ) 
            [8] => Array ( [planoption_id] => 1 ) 
            [9] => Array ( [planoption_id] => 2 ) 
         ) 
     )

如何在givene数组中找到值。上面的数组是$arraypush?

if (in_array('2', $arraypush)) {
    echo "INCLUDED";
    } else {
    echo "-";
    }

我该怎么做?

这应该适用于所有多维数组:

function findInArray($array, $value){
  foreach($array as $k=>$v){
    if((is_array($v) && findInArray($v, $value)) || ($v == $value)){
      return "INCLUDED";
    }
  }
  return false;
}

我想你想要这样的:-

<?php
$arraypush = Array ( 0 => Array ( 0 => Array ( 'planoption_id' => 1 ), 1 => Array ( 'planoption_id' => 2 )));
echo "<pre/>";print_r($arraypush);
foreach($arraypush as $key=>$value){
    foreach($value as $key1=>$val){
        if(in_array('2', $val) == true){
            echo "value exist at the initial array [".$key.']['.$key1.'] index and is INCLUDED.';
        }
    }
}
?>

输出:-https://eval.in/388240

代码中的in_array()函数正在$arraypush中搜索数据,数据在$arraypush[0]中

在in_array()函数中,你不能将一个值与一个数组进行比较,在你的数组中,"针"不是一个值,而是一个数组,所以你必须制作一个以与你的数组相当的格式获得值的数组

$arraypush = array(
0 => array( 
   0 => array( 'planoption_id' => 1 ),
   1 => array( 'planoption_id' => 2 ),
   2 => array( 'planoption_id' => 3 ),
   3 => array( 'planoption_id' => 4 ),
   4 => array( 'planoption_id' => 5 ),
   5 => array( 'planoption_id' => 6 ),
   6 => array( 'planoption_id' => 7 ),
   7 => array( 'planoption_id' => 53 ),
   8 => array( 'planoption_id' => 1 ),
   9 => array( 'planoption_id' => 2 ) 
        ) 
    );
 //gettig the value for the search 
$foo = $_GET['foo'];
// making the 'needle' in a array format like your array definition
$foo_array = array('planoption_id' => $foo);
//use in_array() for search your array-needle 
//in the $arraypush[0] where is the data, not in $arraypush only
if(in_array($foo_array, $arraypush[0]))
 {
  die($foo);
}
else
{
  die("-");
 }

试试这个我认为对你有很大帮助。

<?php
$a = array ( 
  0 => array ( 
            0 => array ( 'planoption_id' => 1 ), 
            1 => array ( 'planoption_id' => 2 ), 
            2 => array ( 'planoption_id' => 3 ),
            3 => array ( 'planoption_id' => 4 ), 
            4 => array ( 'planoption_id' => 5 ), 
            5 => array ( 'planoption_id' => 6 ), 
            6 => array ( 'planoption_id' => 7 ), 
            7 => array ( 'planoption_id' => 53 ), 
            8 => array ( 'planoption_id' => 1 ), 
            9 => array ( 'planoption_id' => 2 ), 
         ) 
     );

    function multi_in_array($value, $array)
    {
        foreach ($array AS $item)
        {
            if (!is_array($item))
            {
                if ($item == $value)
                {
                    //return true;
                    echo "INCLUDED";
                }
                continue;
            }
            if (in_array($value, $item))
            {
                //return true;
                    echo "INCLUDED";
            }
            else if (multi_in_array($value, $item))
            {
                //return true;
                echo "-";
            }
        }
        //return false;
         echo "-";
    }
    echo  multi_in_array(2, $a);

输出:--INCLUDED--------INCLUDED-

以下甚至适用于任意维度的数组:

$found = false;
$needle = 2;
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
foreach($it as $v) {
  if ($v == $needle) {
    $found = true;
    break;
  }
}
echo $found ? 'INCLUDED' : '-';

相关内容

  • 没有找到相关文章

最新更新