Dragon Garda
Dragon Garda

Reputation: 15

Find value and key in multidimensional array

I have the following array:

Array ( 
    [0] => Array ( 
        [word] => 1 
        [question] => php 
        [position] => 11 
    )
    [1] => Array ( 
        [word] => sql 
        [question] => 1 
        [position] => 22 
    ) 
)

I need to find if [position] => 22 exists in my array and retain the array path for further reference. Thank you.

Upvotes: 1

Views: 4725

Answers (8)

Marco Pace
Marco Pace

Reputation: 3870

Try with this function:

function findKey($array, $mykey) {
    if(array_key_exists($mykey, $array))
        return true;

    foreach($array as $key => $value) {
        if(is_array($value))
            return findKey($value, $mykey);
    }

    return false;
}

if(findKey($search_array, 'theKey')) {
    echo "The element is in the array";
} else {
    echo "Not in array";
}

Upvotes: 0

Saic Siquot
Saic Siquot

Reputation: 6513

function findPath($array, $value) {
    foreach($array as $key => $subArray)  if(subArray['position'] === $value) return $key;
    return false;  // or whatever if not found
}


echo findPath($x, 22);  //  returns 1


$x= Array ( 
    [0] => Array ( 
        [word] => 1 
        [question] => php 
        [position] => 11 
    )
    [1] => Array ( 
        [word] => sql 
        [question] => 1 
        [position] => 22 
    ) 
)

Upvotes: 0

user669677
user669677

Reputation:

Try this:

function exists($array,$fkey,$fval)
{
foreach($array as $items)
{
   foreach($items as $key => $val)
     if($key == $fkey and $val == $fval)return true;
}
return false;
}

Example:

if(exists($your_array,"position",22))echo("found");

Upvotes: 0

Giorgio
Giorgio

Reputation: 1613

This will solve your problem:

<?php

foreach ($array as $k => $v) {
    if(isset($v['position']) && $v['position'] == 22) {
    $key = $k;  
    }
}
echo $key;

//$array[$key]['position'] = 22

?>

Upvotes: 0

AndVla
AndVla

Reputation: 713

Example of code for the solution "Ancide" provide.

$found = false;

foreach ($array as $array_item) {
    if (isset($array_item['position'] && $array_item['position'] == "22")) {
        $found = true;
        break;
    }
}

Upvotes: 3

hungneox
hungneox

Reputation: 9829

You can try this code:

$array = array 
( 
    array ( 
        "word" => 1,
        "question" => php,
        "position" => 11 
    ),
    array ( 
        "word" => sql,
        "question" => 1,
        "position" => 22 
    ) 
);

foreach($array as $item)
{
    foreach($item as $key=>$value)
    {
        if($key=="position" && $value=="22")
        {
           echo "found";
        }
    }
}

Upvotes: 1

domes
domes

Reputation: 66

I think there is no other solution than to loop through the array an check whether there is a key "position" and value "22"

Upvotes: 0

rzetterberg
rzetterberg

Reputation: 10268

First check if they key exists using isset, then if the key exists, check that the value is equal to your compare value.

Edit: I missed that there were two arrays. To solve this, iterate through each array and do the check in each cycle. If the check is positive you know which array it is by looking at the current index.

Upvotes: 0

Related Questions