【问题标题】:How can i check particular element in a multidimensional array php [duplicate]我如何检查多维数组php中的特定元素[重复]
【发布时间】:2019-03-14 01:29:58
【问题描述】:

我想在这个数组中搜索50075285

$xyz=Array
(
    [typeA] => Array
        (
            [details_typeA] => Array
                (
                    [id] => 50075285
                    [action_code] => PDF_ONLINE   
                )

        )

    [typeB] => Array
        (
            [details_typeB] => Array
                (
                   [id] => 50075287
                   [action_code] => offline
                )

        )

)

【问题讨论】:

  • 请写一个适当的问题,而不仅仅是转储
  • 如果我没记错的话,它是一个 3 维数组
  • 您好,欢迎来到 SO!不幸的是,目前还不清楚你在问什么。您是要对所有元素还是仅对特定键进行值搜索?到目前为止,您尝试过什么?
  • 另外,阅读 steemit.com/php/@crell/…steemit.com/php/@crell/php-never-type-hint-on-arrays,了解使用关联数组时的一些思考。
  • 是的,我想做一个值搜索,例如,如果我想检查 50075285 是否存在

标签: php


【解决方案1】:

试试这个功能:

$key 数组键的值应该与之关联

你在数组中搜索的$value值

function arr_search($array, $key, $value)
{
    $results = array();

    if (is_object($array)){ $array = (array)$array; }

    if (is_array($array))
    {
        if (isset($array[$key]) && $array[$key] == $value)
            $results[] = $array;

        foreach ($array as $subarray)
            $results = array_merge($results, arr_search($subarray, $key, $value));
    }

    return $results;
}

【讨论】:

  • 非常感谢您的回复。我真的很高兴@Gobbin 真的有帮助..
  • 非常感谢@Mehdi..
  • @user10477731 欢迎您,如果有帮助请标记为已回答
  • $array1= 数组 ( [0] => 50059685 [1] => 50070059 [2] => 50070086 [3] => 50070087 [4] => 50070084 ) array2=数组 ( [0 ] => 1 [1] => 50059685 [2] => 2 [3] => 50070059 [4] => 3 [5] => 50070084 [6] => 4)
【解决方案2】:
//this gives the exact value in a specific array
//in this case this is the value you want. 
$value = $xyz['typeA']['details_typeA']['id'];

//this gives every id in the `details_typeX` array
foreach($xyz as $type){
    foreach($type as $details){
        $value = $details['id']
    }
}

PHP Sandbox proof

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    • 2011-10-09
    • 2011-06-24
    • 1970-01-01
    • 2018-06-28
    • 2016-08-11
    • 1970-01-01
    相关资源
    最近更新 更多