Multidimensional array find given value in array in php

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

      

How do I find the value in the givene array.above: $ arraypush?

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

      

How can i do this?

+3


source to share


5 answers


I think you want like this: -

<?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.';
        }
    }
}
?>

      



Output: - https://eval.in/388240

+2


source


This should work for all multidimensional arrays:



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

      

+3


source


The in_array () function in your code looks for data in $ arraypush and the data is in $ arraypush [0]

And you cannot compare a value with an array in the in_array () function, in the yor array "needle" is not a value, it is an array, so you need to create an array that receives a value in a format comparable to your 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("-");
 }

      

+2


source


Try it, I think it will help you.

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

      

Output: - - ON ------- ON -

+2


source


The following functions work for arrays of arbitrary sizes:

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

      

+1


source







All Articles