简体   繁体   English

在多维数组中查找值和键

[英]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. 我需要找到我的数组中是否存在[position] => 22并保留数组路径以供进一步参考。 Thank you. 谢谢。

Example of code for the solution "Ancide" provide. 解决方案“Ancide”提供的代码示例。

$found = false;

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

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";
        }
    }
}

First check if they key exists using isset , then if the key exists, check that the value is equal to your compare value. 首先使用isset检查它们是否存在键,然后如果该键存在,请检查该值是否等于您的比较值。

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. 如果检查结果为肯定,您可以通过查看当前索引来了解它是哪个数组。

我认为没有其他解决方案比循环数组检查是否存在关键“位置”和值“22”

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";
}

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM