>我有以下 2 个数据:
$temp = Array
(
[@url] => url
[@type] => image/jpeg
[@expression] => full
[@width] => 644
[@height] => 429
)
$count_total = count($temp); // gives me 5, how can it give me total = 1?
Array
(
[0] => Array
(
[@url] => url1
[@type] => image/jpeg
[@expression] => full
[@width] => 800
[@height] => 621
)
[1] => Array
(
[@url] => url2
[@type] => application/x-shockwave-flash
)
)
this is total:2 // this is correct
如何将第一个数组计数为 1?
数组(由您给出)
$temp = Array
(
[@url] => url
[@type] => image/jpeg
[@expression] => full
[@width] => 644
[@height] => 429
)
有 5 个值,索引@url, @type, @expression, @width, @height
.所以它总是会给你计数 5。
如果您想获得计数 1。你必须像下面这样做
$temp = Array(
array(
[@url] => url
[@type] => image/jpeg
[@expression] => full
[@width] => 644
[@height] => 429
)
);
在这里count($temp)
将为您提供输出 1
这将为您提供计数 1
$temp[] = Array
(
[@url] => url
[@type] => image/jpeg
[@expression] => full
[@width] => 644
[@height] => 429
)
$count_total = count($temp);
$temp = Array
(
[0] => Array
(
[@url] => url1
[@type] => image/jpeg
[@expression] => full
[@width] => 800
[@height] => 621
)
)
$count_total = count($temp);
如果您的数组在上面,则可以计数为 1;
如果得到单个结果,请执行以下操作:
$temp = Array
(
[@url] => url
[@type] => image/jpeg
[@expression] => full
[@width] => 644
[@height] => 429
);
单一结果:
$new_temp[0] = $temp;
现在你得到:
count($new_temp); // get 1
$temp = Array
(
[0] => Array
(
[@url] => url
[@type] => image/jpeg
[@expression] => full
[@width] => 644
[@height] => 429
)
);
$count_total = count($temp); //gives you 1
问题是,在第一个代码块中,您正在尝试计算数组的元素数量,在第二个块中是另一个数组的 insido。
它正在计算数组中的元素数量。第一个是给你 5,因为数组有 5 个元素。第二个是给你 2,因为它是一个包含 2 个元素(数组)的多数组。
要使其返回一个,您可以将数组放在数组中,如下所示:
Array([0] => Array())
我希望这回答了你的问题。