Php 搜索具有混合数据类型的多维数组,回显结果
我有一个混合数据类型(数组、整数、字符串)的php数组。我想在数组中搜索混合数据类型数组中包含的匹配项,如下所示 我的测试阵列Php 搜索具有混合数据类型的多维数组,回显结果,php,search,multidimensional-array,echo,Php,Search,Multidimensional Array,Echo,我有一个混合数据类型(数组、整数、字符串)的php数组。我想在数组中搜索混合数据类型数组中包含的匹配项,如下所示 我的测试阵列 $arrActors =[0 => [ 'actorName' => "heath ledger", 'actorAlias' => [], 'actorGender' => 1, 'actorNoms' => ["angel", "john constantine"] ], 1 => [
$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"]
]
];
如果指针被设置为一个元素,并且发现该元素存在于ActorNames中,那么我想回显关联的参与者(actorName)的名称。在下面的示例中,我试图找到cyclops(actorNoms)返回与他相关联的演员Michael Pare(actorName)的名字
我尝试查找actorNoms并返回actors名称
$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
}
}
$pinder='cyclops';
foreach($haystack中的arrActors)
{
if(在数组中($needle,$haystack)){
echo$haystack['actorNoms']。
;
}否则{
echo“未找到任何内容”
;//echo某个内容,以便我知道它正在运行
}
}
我的尝试返回失败,因为它“未找到”。在搜索独眼巨人时,我如何回想起演员迈克尔·帕雷的名字
谢谢你的帮助。我已尝试正确格式化代码,以便于使用。我已经搜索了Stack、Google和其他资源好几个小时了,现在我正试图找到一个我能理解的解决方案。我不是很熟练,但我保证我正在学习,感谢所有帮助。不要使用
if(in_array($needle, $haystack)) {
echo $haystack['actorNoms'] . '<br />' ;
}
if(在数组中($needle,$haystack)){
echo$haystack['actorNoms']。
;
}
试试这个:
if(in_array($needle, $haystack['actorNoms'])) {
echo $haystack['actorName'] . '<br />' ;
}
if(在数组($needle,$haystack['actorNoms']){
echo$haystack['actorName']。
;
}
您所做的是搜索$haystack
,这是演员的主要数组
in_array
不会自动搜索嵌套数组中的多维数组,因此您需要指定要搜索的区域:in_array($haystack['actorNoms')
$needle='cyclops';
foreach($haystack中的arrActors)
{
if(在数组($needle,$haystack['actorNoms']){
echo$haystack['actorName']。
;
}
}
,仅适用于一级阵列。因此,每次它通过第一级数组时,其中as'actorNoms'是第一级数组下的子数组。尝试此操作,使用此索引为您提供父索引,您将获得数据
$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;
}
in_array()
只搜索顶层,它不会像当前那样执行您想要的操作。您需要运行一个循环(foreach
/for
)。(因为actorNoms
是一个数组)。尝试将其更改为if(in_array($needle,$haystack['actorNoms')){……
如果您只是要在actorNoms
内搜索,那么只需显式地将其指向in_array内的,然后回显式地搜索参与者名称。谢谢您的建议-在大多数情况下,我实际上会显式地搜索(这意味着我将搜索'actorName',并因此特别称之为yes?)。但我想了解如何做得更多以实现增长。我尝试了此操作,但它返回了一个致命错误:致命错误:调用未定义的函数getParentStack()。您可以共享该函数吗?现在我已经修改了函数try,并使用更新的代码或“getParentStack”替换为“getParent_id”。感谢您的建议,它很有效,我们对此表示感谢。因为它是第一次收到,所以我选择它作为答案。
$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;
}