多维数组在php

时间:2015-06-26 17:50:58

标签: php multidimensional-array

阵列

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 array.above数组中找到值是$ arraypush?

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

我该怎么做?

5 个答案:

答案 0 :(得分:3)

这适用于所有多维数组:

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

答案 1 :(得分:2)

我想你想要这样: -

<?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

答案 2 :(得分:2)

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

你不能将一个值与in_array()函数中的数组进行比较,在yor数组中,&#39; needle&#39;不是一个值,是一个数组,因此您必须创建一个数组,该数组以与您拥有的数组相当的格式获取值

$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("-");
 }

答案 3 :(得分:2)

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

<?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 -------包含 -

答案 4 :(得分:1)

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

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