fteinz
fteinz

Reputation: 1095

search with in_array in multidimensional array

My English issnt so good sorry for that.

i have a array:

Array ( [name] => Array 
                      ( 
                      [0] => Sorry the name is missing! 
                      [1] => Sorry the name is to short! 
                      ) 
) 

Now I want to test with in_array for eg "name".

if (in_array("name", $false["name"])) {
echo "the array keys";
}

but it dsnt work. could annybody help me please? thanks a lot.

Upvotes: 3

Views: 24617

Answers (6)

Subail Sunny
Subail Sunny

Reputation: 287

in_array() does not work with multi-dimensional arrays, so it is not possible to use in_array() here. When you are searching "name" in in_array() it searches in the first array and finds a key of the array named "name".

Better to use array_key_exists function. An example is given below: (remember it is only a suggestion. code may vary)

if(array_key_exists('name', $false['name'])) {
    echo $false['name'][0]; // or [1] .. or whatever you want to echo
}
//$false['name'] array contains your searched data in different keys; 0,1,2,....

You can use foreach() to loop the first array then search using in_array() but that will not be a good method because it will take more time to find.
Best of luck :)

Upvotes: 2

iCezz
iCezz

Reputation: 634

If you are wanting to use in_array you just need to put the second parameter as the array you have defined example

if(in_array('name',$false){
//do your stuff
print_r($false['name']); //this will print the array of name inclusive of [0] and [1]
}

There are more example given in the in_array php manual. please check it out.

Upvotes: 0

Naveed
Naveed

Reputation: 42093

If you are searching for array key name in your main array:

$arr = array( "name" => array( 
                      "0" => "Sorry the name is missing!", 
                      "1" => "Sorry the name is to short!" 
               )); 

if(array_key_exists('name', $arr)) {
    print_r( $arr['name'] );
} else {
    echo "array key not found";
}

Demo

It will not find name if you will search in $arr['name'] because it contains only 0 and 1 array keys at this level.

Upvotes: 0

norman784
norman784

Reputation: 2221

Maybe you need to walk through the array first, then check it

function in_multiarray($str, $array)
{
    $exists = false;

    if (is_array($array)) {
       foreach ($array as $arr):
           $exists = in_multiarray($str, $arr);
       endforeach;
    } else {
        echo $array . ' = ' . $str . "\n";
        if (strpos($array, $str) !== false) $exists = true;
    }

    return $exists;
}

Upvotes: 1

Treffynnon
Treffynnon

Reputation: 21553

in_array() looks for the exact value. So you would need to specify "Sorry the name is missing!" instead of just "name".

For example:

if (in_array("Sorry the name is missing!", $false["name"])) {
    echo "the array keys";
}

Where:

$false = array( 'name' => array( 
    0 => 'Sorry the name is missing!',
    1 => 'Sorry the name is to short!'),
);

Upvotes: 0

stefandoorn
stefandoorn

Reputation: 1032

Try array_key_exists(): link

if(array_key_exists('name', $false['name'])) {
    echo $false['name'][0]; // or [1] .. or whatever you want to echo
}

Upvotes: 9

Related Questions