简体   繁体   English

在多维数组中查找特定值

[英]Find specific value in multidimensional array

I am trying to make a search of the digit one [1] in multidimensional array. 我试图在多维数组中搜索数字[1]。 It does work using a standard array [$array_1], but not when there are embedded arrays [$array_2] and [$array_3]. 它确实使用标准数组[$ array_1]起作用,但是当存在嵌入式数组[$ array_2]和[$ array_3]时不起作用。

At the very end of the script you find what I have tried. 在脚本的最后,您可以找到我尝试过的内容。

Wanted behaviour: 想要的行为:

The search to return a value that indicates if the value was found. 搜索以返回指示是否找到该值的值。 It is fine with either the index position, alternatively returning the amount of digits or repetetive numbers of found digits. 可以使用索引位置,也可以返回数字位数或找到的数字的重复数字,这很好。

My plan is to move the result into a variable and check if the results is null. 我的计划是将结果移动到变量中,并检查结果是否为空。 If null means it did not find any search result. 如果为null,则表示未找到任何搜索结果。 I am using null because zero [0] could refer to the index position. 我使用null是因为零[0]可以引用索引位置。

<pre>

<?php

$search_for_value = 1;

/**
 * ---------------------------------
 * Array.
 * ---------------------------------
 */

$array_1 = [3, 2, 1];

/**
 * ---------------------------------
 * Multidimensional arrays.
 * ---------------------------------
 */

/**
  *  Value 1 is exists in the array.
 */

$array_2 = [
        [2],
        [1]
];


echo ("-- array_2 ---\n\n");
print_r($array_2);

/**
 *  Value 1 is missing in the array.
 */

$array_3 = [
        [4],
        [5]
];

echo ("-- array_3 ---\n\n");
print_r($array_3);


/**
 * Functions
 */
 function find_value($search_for_value, $array_selected) {
     return(array_search($search_for_value, $array_selected));
 };


 /**
  * ---------------------------------
  * Searches
  * ---------------------------------
  */

 // Search for value in array_1

$array_selected = $array_1;

 print_r(
     find_value(
         $search_for_value, $array_selected
     )
 );

 // Search for value in array_2

 $array_selected = $array_2;

 print_r(                                               # <==== Not working.
     find_value(
         $search_for_value, $array_selected
     )
 );

?>

You can use in_array with splat operator, 您可以将in_array与splat运算符配合使用,
Below is for multidimensional arrays 下面是多维数组

$temp = array_merge(...$array_2);
var_dump(in_array(2, $temp));
$temp = array_merge(...$array_3);
var_dump(in_array(2, $temp));

for 1D arrays, 对于一维阵列,

you can directly check in_array($value_to_search, $array_1); 您可以直接检查in_array($value_to_search, $array_1);

I am exposing array to their value level so they flatten up. 我将数组暴露于它们的值水平,以便它们变平。
Now I just checked with in_array whether it exists in an array or not. 现在,我只是用in_array检查它是否存在于数组中。

Demo . 演示

Here's a recursive function that returns a boolean indicating if the value was found. 这是一个递归函数,它返回一个布尔值,指示是否找到该值。 The majority of this function was taken from Recursive array_search . 此函数的大部分来自递归array_search

/**
 * Returns a boolean indicating if the needle is found in the haystack.
 * @param $needle
 * @param $haystack
 * @return bool
 */
function find_value($needle, $haystack)
{
    foreach ($haystack as $key => $value) {

        if ($needle === $value || (is_array($value) && find_value($needle, $value) !== false)) {
            return true;
        }
    }

    return false;
}

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

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