Php In_数组不工作
我有一个数组,我在_数组函数中应用来查找该数组中的特定数字,但它没有显示结果,数据在数组中,但没有响应..:( 数组:Php In_数组不工作,php,arrays,xml,simplexml,Php,Arrays,Xml,Simplexml,我有一个数组,我在_数组函数中应用来查找该数组中的特定数字,但它没有显示结果,数据在数组中,但没有响应..:( 数组: Array ( [0] => SimpleXMLElement Object ( [0] => 572140 ) [1] => SimpleXMLElement Object ( [0] => 533167 ) [2] => SimpleXMLElement Object
Array
(
[0] => SimpleXMLElement Object
(
[0] => 572140
)
[1] => SimpleXMLElement Object
(
[0] => 533167
)
[2] => SimpleXMLElement Object
(
[0] => 572070
)
[3] => SimpleXMLElement Object
(
[0] => 572383
)
[4] => SimpleXMLElement Object
(
[0] => 285078
)
[5] => SimpleXMLElement Object
(
[0] => 430634
)
}
if(in_array('285078',$arr))
{
echo 'yes';
}
else
{
echo "No";
}
我正在使用的代码:
Array
(
[0] => SimpleXMLElement Object
(
[0] => 572140
)
[1] => SimpleXMLElement Object
(
[0] => 533167
)
[2] => SimpleXMLElement Object
(
[0] => 572070
)
[3] => SimpleXMLElement Object
(
[0] => 572383
)
[4] => SimpleXMLElement Object
(
[0] => 285078
)
[5] => SimpleXMLElement Object
(
[0] => 430634
)
}
if(in_array('285078',$arr))
{
echo 'yes';
}
else
{
echo "No";
}
这是我从xml文件创建的数组
$arr = array();
foreach($xmlInjury as $data)
{
array_push($arr,$data->player_id);
}
它只显示“否”…请在这方面帮助我…尝试键入您的数组:-
$array = (array) $yourarray;
if(in_array('285078',$arr))
{
echo 'yes';
}
else
{
echo "No";
}
in_数组不是递归的,它只在第一级搜索。
数组的第一级成员是SimpleXMLElement对象,而不是数字。您需要先将它们全部强制转换,然后进行搜索。如下所示:
$new_arr = array_map(function($piece){
return (string) $piece;
}, $arr);
// then use in array
if(in_array('285078', $new_arr)) {
echo 'exists';
} else {
echo 'does not exists';
}
首先,数组不是字符串数组,而是对象数组。 如果无法更改数组的结构,请尝试以下操作:
foreach ($your_array as $item) {
if (strval($item) == '25478') {
echo 'found!';
break;
}
}
如果可以更改阵列,请按如下方式向其中添加项:
$your_array[] = strval($appended_value);
然后你可以在数组中使用
我想这是因为“对象数组”介于两者之间。可能是因为它不起作用。@Khushboo我如何从数组中删除该对象,有什么方法吗?您有一个对象数组。循环遍历它们并检查您的值是否在其中。您在数组中找不到'285078'
,因为它不在数组中。您拥有的是一个SimpleXMLE数组元素
对象。“…获取数据,然后将其保存在数组中…”这样做时,不要将simplexmlement
添加到数组中,而是添加它所呈现的值。将数组转换为数组?hmmmi认为仍然不搜索它不会得到类型转换吗?是的,将对象转换为数组?是的,这样他只得到数组,而不是一部分对象