我正在获取一些XML数据并用PHP进行解析。
大多数结果来自多维数组,但偶尔我会在单个数组中得到结果,这会破坏我的脚本。
我正在尝试格式化单个结果以匹配多维数组中结果的格式,但我运气不好。
这是我得到的:
Array
(
[name] => Steve Jobs
[id] => 3
)
Array
(
[0] => Array
(
[name] => Steve Jobs
[id] => 6
)
[1] => Array
(
[name] => Bill Gates
[id] => 8
)
)
我正在尝试格式化单个结果以匹配多维格式,然后压平。。。
Array
(
[0] => Array
(
[name] => Steve Jobs
[id] => 3
)
[1] => Array
(
[name] => Steve Jobs
[id] => 6
)
[2] => Array
(
[name] => Bill Gates
[id] => 8
)
)
我试过这个:
$array_check = @array_keys($result[0]['name']);
if ($array_check[0] == "0") {
$result;
} elseif ($array_check[0] == "name") {
$ReWrite = array ([0] =>
array (['name']=>
array ($result[0]['name'])
));
$result = $ReWrite;
}
我以为这样就可以了,但它已经关闭了…
试试这个:
$array_check = @array_keys($result[0]['name']);
if (!isset($array_check[0])) {
$result[] = $array_check;
} else {
$result = $array_check;
}
var_dump($result);
如果您的第一个数组被分配给变量$singleArray,并且您的目标结果存储在$results中,请尝试此array_push($results, $singleArray);
与reset()
一起使用它会返回数组的第一个元素:
if(!is_array(reset($result))){
array_push($results, $result);
}
这将测试数组是否不包含作为元素的数组,是否不将整个数组推送到聚合数组。
编辑:尝试这个循环:
for($i = 0; $i <= count($multi); $i++){
$arr = $multi[$i];
if(!is_array($arr)){
$multi[$i] = array($arr);
}
} var_dump($multi);