3

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 to find the value in givene array.above array is $arraypush ?

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

How can i do this ?

1
  • @phpdev try the given answers, and mark the most suitable answer for your problem. thanks. Commented Jun 26, 2015 at 21:41

5 Answers 5

3

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

Comments

2

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

Comments

2

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

And you can't compare a value with a array in in_array() function, in yor array the 'needle' isn't a value, is an array so you have to make an array who gets the value in a format comparable whit the array you have

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

Comments

2

Try this I think is help full to 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:- -INCLUDED-------INCLUDED--

Comments

1

The following even works for arrays of arbitrary dimensions:

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

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.