0pt*_*1z3 0 php arrays multidimensional-array
以下是从XML文档转换的数组的print_r:
Array
(
[layer1] => Array
(
[item] => Array
(
[item-id] => 1886731
[item-name] => Bad Dog
[category] => pets
[link] = http://www.baddog.com/
)
[total-matched] => 1
)
)
Run Code Online (Sandbox Code Playgroud)
对于上面的数组,sizeof($ myarray [layer1] [item])应该返回1,但它返回4.如果有多个"item"项,我会得到正确数量的"item"项.无论我使用"sizeof"还是"count",都会发生同样的错误.当只有一个项目时,如何让PHP返回"1"?
因此,如果有一个项目,我无法使用数组[layer1] [item] [0] [item-name]访问item-name,我必须使用array [layer1] [item] [item-name].
两者之间存在着天壤之别:
$array1 = array(
'layer1' => array(
'item' => array(
'0' => array(
'item-id' => 123123,
'item-name' => 'Bad Dog',
'category' => 'pets',
'link' => 'http://www.baddog.com/',
),
)
)
);
Run Code Online (Sandbox Code Playgroud)
和:
$array2 = array(
'layer1' => array(
'item' => array(
'item-id' => 123123,
'item-name' => 'Bad Dog',
'category' => 'pets',
'link' => 'http://www.baddog.com/',
)
)
);
Run Code Online (Sandbox Code Playgroud)
基本上它们是不同的结构,PHP返回以下内容是正确的:
count($array1['layer1']['item']);
/// = 1 (count of items in the array at that point)
count($array2['layer1']['item']);
/// = 4 (count of items in the array at that point)
Run Code Online (Sandbox Code Playgroud)
如果您希望得到一个['layer1']['item']对您的应用程序有意义的计数,您将始终需要['layer1']['item']是一个包含多个数组结构的数组...即$array1如上所述.这就是为什么我问什么是生成你的数组结构,因为 - 无论它是什么 - 它基本上是智能地堆叠你的数组数据取决于项目的数量,这是你不想要的.
可能导致数组以这种方式堆栈的代码通常类似于以下内容:
/// $array = array(); /// this is implied (should be set elsewhere)
$key = 'test';
$newval = 'value';
/// if we are an array, add a new item to the array
if ( is_array($array[$key]) ) {
$array[$key][] = $newval;
}
/// if we have a previous non-array value, convert to an array
/// containing the previous and new value
else if ( isset($array[$key]) ) {
$array[$key] = array($array[$key],$newval);
}
/// if nothing is set, set the $newval
else {
$array[$key] = $newval;
}
Run Code Online (Sandbox Code Playgroud)
基本上,如果您继续调用上面的代码,并在每次运行后进行跟踪,您将看到以下结构构建:
$array == 'value';
Run Code Online (Sandbox Code Playgroud)
然后
$array == array(0 => 'value', 1 => 'value');
Run Code Online (Sandbox Code Playgroud)
然后
$array == array(0 => 'value', 1 => 'value', 2 => 'value');
Run Code Online (Sandbox Code Playgroud)
这是导致问题的第一步$array = 'value';.如果您稍微修改代码,您可以摆脱这个:
/// $array = array(); /// this is implied (should be set elsewhere)
$key = 'test';
$newval = 'value';
/// if we are an array, add a new item to the array
if ( is_array($array[$key]) ) {
$array[$key][] = $newval;
}
/// if nothing is set, set the $newval as part of an subarray
else {
$array[$key] = array($newval);
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我所做的就是删除itermediate if statement,并确保当我们发现没有设置初始值时,我们总是创建一个数组.上面将创建一个您可以随时计算的结构,并知道您推送到阵列的项目数.
$array == array(0 => 'value');
Run Code Online (Sandbox Code Playgroud)
然后
$array == array(0 => 'value', 1 => 'value');
Run Code Online (Sandbox Code Playgroud)
然后
$array == array(0 => 'value', 1 => 'value', 2 => 'value');
Run Code Online (Sandbox Code Playgroud)
更新
啊,我是这么认为的.所以数组是从XML生成的.在这种情况下,我收集到您正在使用预定义的库来执行此操作,因此修改代码是不可能的.正如其他人已经说过的那样,最好的办法是使用PHP可用的众多XML解析库中的一个:
http://www.uk.php.net/simplexml
使用这些系统时,您可以保留更多的对象结构,这应该更容易计算.以上两者都支持xpath表示法,它可以让你计算项目,甚至不必抓住任何数据.
更新2
在你给出的函数之外,这是导致数组以导致问题的方式堆栈的部分:
$children = array();
$first = true;
foreach($xml->children() as $elementName => $child){
$value = simpleXMLToArray($child,$attributesKey, $childrenKey,$valueKey);
if(isset($children[$elementName])){
if(is_array($children[$elementName])){
if($first){
$temp = $children[$elementName];
unset($children[$elementName]);
$children[$elementName][] = $temp;
$first=false;
}
$children[$elementName][] = $value;
}else{
$children[$elementName] = array($children[$elementName],$value);
}
}
else{
$children[$elementName] = $value;
}
}
Run Code Online (Sandbox Code Playgroud)
修改将是:
$children = array();
foreach($xml->children() as $elementName => $child){
$value = simpleXMLToArray($child,$attributesKey, $childrenKey,$valueKey);
if(isset($children[$elementName])){
if(is_array($children[$elementName])){
$children[$elementName][] = $value;
}
}
else{
$children[$elementName] = array($value);
}
}
Run Code Online (Sandbox Code Playgroud)
这应该会阻止您的数组堆叠...但是如果您的代码的任何其他部分依赖于以前的结构,则此更改可能会破坏该代码.