Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 如果in_array()返回true,则返回数组的值_Php_Arrays - Fatal编程技术网

Php 如果in_array()返回true,则返回数组的值

Php 如果in_array()返回true,则返回数组的值,php,arrays,Php,Arrays,我有一个多维数组 Array ( [0] => Array ( [item_no] => 1.01 [sor_id] => 2 [selected_qty] => 7 [price] => 3 [type] => S [form_name] => GSOR [par

我有一个多维数组

Array
(
    [0] => Array
        (
            [item_no] => 1.01
            [sor_id] => 2
            [selected_qty] => 7
            [price] => 3
            [type] => S
            [form_name] => GSOR
            [parent_id] => 89
        )

    [1] => Array
        (
            [item_no] => 1.03.03
            [sor_id] => 7
            [selected_qty] => 1
            [price] => 50
            [type] => S
            [form_name] => GSOR
            [parent_id] => 89
        )

    [2] => Array
        (
            [item_no] => 1.23
            [sor_id] => 28
            [selected_qty] => 6
            [price] => 60
            [type] => S
            [form_name] => GSOR
            [parent_id] => 89
        )

    [3] => Array
        (
            [item_no] => 6.03
            [sor_id] => 64
            [selected_qty] => 1
            [price] => 50
            [type] => S
            [form_name] => GSOR
            [parent_id] => 61
        )

    [4] => Array
        (
            [item_no] => 4.02
            [sor_id] => 42
            [selected_qty] => 1
            [price] => 39
            [type] => S
            [form_name] => GSOR
            [parent_id] => 40
        )

)
我有一个递归函数,如果值存在于多维数组中,则返回true

递归in_array()函数

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
            // return $haystack;
            // print_r($haystack);
        }
    }
    return false;
}
例如

 echo $item_2 = in_array_r("1.03.03", $selected_items_array) ? 'found' : 'not found';
所以,
item\u no
=>
1.03.03
存在于该多维数组中,因此它返回true,否则返回false, 但是我想得到第一位的
price
soru id
的值。
但它只返回
1
或“0”,因此如何返回整个数组或数组
索引
,以便使用该数组或索引可以获取值。或任何其他选择

你就快到了。返回匹配元素的id,而不是true:

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $key => $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return $key;
        }
    }
    return false;
}

$matching_item = in_array_r("1.03.03", $selected_items_array);
echo $matching_item===false ? "not found" : " found at item ".$matching_item;

你就快到了。返回匹配元素的id,而不是true:

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $key => $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return $key;
        }
    }
    return false;
}

$matching_item = in_array_r("1.03.03", $selected_items_array);
echo $matching_item===false ? "not found" : " found at item ".$matching_item;

数据来自哪里?如果它是一个数据库,那么你最好使用查询。数据来自哪里?如果它是一个数据库,那么你最好使用查询。非常感谢你。太好了,很好用。如果可能的话,我会给你100票。我从3小时起就被困在里面;)哦非常感谢你。太好了,很好用。如果可能的话,我会给你100票。我从3小时起就被困在里面;)