Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在多维数组中执行数组搜索?_Php_Arrays - Fatal编程技术网

Php 如何在多维数组中执行数组搜索?

Php 如何在多维数组中执行数组搜索?,php,arrays,Php,Arrays,我有以下代码: $order_list = array ( array ("tangible", 1, 8, 33, 19000), array ("tangible", 1, 9, 8, 19000), array ("tangible", 6, 3, 24, 19000), array ("tangible", 6, 2, 10, NULL),

我有以下代码:

$order_list = array ( array ("tangible", 1, 8, 33, 19000),
                      array ("tangible", 1, 9, 8, 19000),
                      array ("tangible", 6, 3, 24, 19000),
                      array ("tangible", 6, 2, 10, NULL),
                      array ("tangible", 1, 17, 11, 28000));

$key = array_search(3, array_column($order_list, $order_list[2]));
我想根据
$order\u list[$I][2]
得到
$order\u list[$I][3]
的值

例如,如果我把:

3
作为回报,我将得到
24

9
作为回报,我将获得
8

等等

我尝试使用数组搜索:

$key = array_search(3, array_column($order_list, $order_list[2]));
但我有一个错误:

Warning: array_column(): The column key should be either a string or an integer in /home/***/public_html/***.php on line 8

Warning: array_search() expects parameter 2 to be array, boolean given in /home/***/public_html/***.php on line 8
在这种情况下如何执行数组_serach?之前谢谢。


<?php

$search = 9;

$order_list = array ( array ("tangible", 1, 8, 33, 19000),
                  array ("tangible", 1, 9, 8, 19000),
                  array ("tangible", 6, 3, 24, 19000),
                  array ("tangible", 6, 2, 10, NULL),
                  array ("tangible", 1, 17, 11, 28000));




foreach ($order_list as $string){

    if (in_array($search,$string)){
       //repsonse here
    }
}
?>

我创建了一个通用函数来获取二维数组中当前值的下一个值。看看下面的函数。还要查看函数的变量描述,以了解函数中的输入:

/***
 * @param array $array input array
 * @param $search_value value that need to be searched
 * @param $search_index index of inner array where current value exists
 * @return next value of current value
 */
function getNextSequence(array $array, $search_value, $search_index)
{
    $result = null;
    $key = array_search($search_value, array_column($array, $search_index));
    if ($key !== false) {
        $result = (isset($array[$key][$search_index + 1])) ? $array[$key][$search_index + 1] : null;
    }

    return $result;
}

$order_list = array(
    array("tangible", 1, 8, 33, 19000),
    array("tangible", 1, 9, 8, 19000),
    array("tangible", 6, 3, 24, 19000),
    array("tangible", 6, 2, 10, NULL),
    array("tangible", 1, 17, 11, 28000)
);

var_dump(getNextSequence($order_list, 3, 2)); //output: int(24)
var_dump(getNextSequence($order_list, 9, 2)); //output: int(8)
var_dump(getNextSequence($order_list, 10, 2)); //output: Null
var_dump(getNextSequence($order_list, 2, 2)); //output: int(10)
另一种方式

$search = 9;

$order_list = array ( array ("tangible", 1, 8, 33, 19000),
                  array ("tangible", 1, 9, 8, 19000),
                  array ("tangible", 6, 3, 24, 19000),
                  array ("tangible", 6, 2, 10, NULL),
                  array ("tangible", 1, 17, 11, 28000));




foreach ($order_list as $string){
    if ($string[2] == $search){
    print_r( $string);  
    }  
}

6的输出是什么?6不是执行搜索的键,兄弟。6实际上是
供应商id
。3和2是
产品id
。我们不基于
供应商id
进行搜索,而是基于
产品id
。结果是24或10。看到我的答案了吗?希望这对你有帮助,兄弟,我试过了:var_dump(getNextSequence($order_list,8,2));这应该给我33作为结果。但是为什么你的代码给我空值?你的代码似乎跳过了$order的第一行_list@RobertHanson它的原因是,
if($key)
也将其转义为0。更正了我的答案。请看一看