【问题标题】:search multidimensional array with mixed data types, echo back result搜索具有混合数据类型的多维数组,回显结果
【发布时间】:2015-04-01 01:23:19
【问题描述】:

我有一个混合数据类型(数组、整数、字符串)的 php 数组。我想在数组中搜索包含在混合数据类型数组中的匹配项,如下所示。

我的测试数组

$arrActors =[0 => [ 
    'actorName' => "heath ledger",
    'actorAlias' => [],
    'actorGender' => 1,
    'actorNoms' => ["angel", "john constantine"]
],
1 => [ 
    'actorName' => "Michael pare",
    'actorAlias' => ["mikey", "that guy"],
    'actorGender' => 1,
    'actorNoms' => ["cyclops", "slim", "eric the red"]
    ]
];

如果将 needle 设置为一个元素并且发现该元素存在于 actorNoms 中,我想回显关联的 actor 的名称 (actorName)。在下面的示例中,我试图找到 cyclops (actorNoms) 返回与他相关联的演员 Michael Pare (actorName) 的姓名。

我尝试查找 actorNoms 并返回演员姓名

$needle = 'cyclops';
foreach($arrActors as $haystack) 
{   
    if(in_array($needle, $haystack)) { 
        echo $haystack['actorNoms'] . '<br />' ;
    }else{
        echo 'nothing found<br />';//echo something so i know it ran
    }
}

我的尝试返回失败,因为它回显了“未找到任何内容”。在搜索独眼巨人时,如何回显演员 Michael Pare 的名字。

感谢您提供的任何帮助。我试图正确格式化我的代码以便于使用。我已经搜索了 Stack、Google 和其他资源几个小时,试图找到一个我能理解的解决方案。我不是很熟练,但我保证我正在学习并且感谢所有帮助。

【问题讨论】:

  • in_array() 只搜索顶层,它不会像现在这样做你想要的。您需要运行一个循环 (foreach / for)。 (因为actorNoms 是一个数组)。尝试将其更改为if(in_array($needle, $haystack['actorNoms'])) { ....
  • 如果您只是要在actorNoms 中搜索,那么只需明确地将其指向in_array 中的那个,然后回显演员名称。
  • 感谢您的建议 - 在大多数情况下,我实际上会明确搜索(这意味着我会搜索“actorName”,因此专门称之为是吗?)。但我想了解如何做更多事情才能成长。

标签: php search multidimensional-array echo


【解决方案1】:

而不是使用

if(in_array($needle, $haystack)) { 
    echo $haystack['actorNoms'] . '<br />' ;
}

试试这个:

if(in_array($needle, $haystack['actorNoms'])) { 
    echo $haystack['actorName'] . '<br />' ;
}

您所做的是搜索$haystack,这是演员的主要数组。

in_array 不会自动在嵌套数组中搜索多维数组,因此您需要指定要搜索的区域:in_array($haystack['actorNoms'])

【讨论】:

  • 感谢您的建议,它奏效了,非常感谢。因为是第一次收到,所以我选择了它作为答案。
【解决方案2】:
$needle = 'cyclops';
foreach($arrActors as $haystack) 
{   
    if(in_array($needle, $haystack['actorNoms'])) { 
        echo $haystack['actorName'] . '<br />' ;
    }
}

in_array,仅适用于一级数组。所以每次它通过一级数组,其中'actorNoms'是一级数组下的子数组。

【讨论】:

    【解决方案3】:

    试试这个给你父索引使用这个索引你会得到数据

    $arrActors = array( array( 
        'actorName' => "heath ledger",
        'actorAlias' => array(),
        'actorGender' => 1,
        'actorNoms' => array("angel", "john constantine")
    ),array(
    
        'actorName' => "Michael pare",
        'actorAlias' => array("mikey", "that guy"),
        'actorGender' => 1,
        'actorNoms' => array("cyclops", "slim", "eric the red")
        )
    );
    
    
    print_r( getParent_id("cyclops",$arrActors));
    function getParent_id($child, $stack) {
            foreach ($stack as $k => $v) {
                if (is_array($v)) {
                    // If the current element of the array is an array, recurse it and capture the return
                    $return = getParent_id($child, $v);
    
                    // If the return is an array, stack it and return it
                    if (is_array($return)) {
                        return array($k => $return);
                    }
                } else {
                    // Since we are not on an array, compare directly
                    if (preg_match("/$child/",$v)) {
                        // And if we match, stack it and return it
                        return array($k => $child);
                    }
                }
            }
    
            // Return false since there was nothing found
            return false;
        }
    

    【讨论】:

    • 我尝试了这个,但它返回了一个致命错误:致命错误:调用未定义的函数 getParentStack()。可以分享一下这个功能吗?
    • 现在我已经修改函数尝试使用更新的代码或“getParentStack”替换为“getParent_id”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-01
    • 2016-07-21
    • 2019-06-26
    • 2013-04-08
    相关资源
    最近更新 更多