我有以下两个数据:
$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?
答案 0 :(得分:2)
下面的数组(由你给出)
@url, @type, @expression, @width, @height
有5个索引为$temp = Array(
array(
[@url] => url
[@type] => image/jpeg
[@expression] => full
[@width] => 644
[@height] => 429
)
);
的值。所以它总会给你5分。
如果你想得到计数1.你必须这样做
count($temp)
此处 $temp = Array
(
[0] => Array
(
[@url] => url1
[@type] => image/jpeg
[@expression] => full
[@width] => 800
[@height] => 621
)
)
$count_total = count($temp);
会为您提供输出1
答案 1 :(得分:1)
这将给你计数1
{{1}}
答案 2 :(得分:0)
$temp[] = Array
(
[@url] => url
[@type] => image/jpeg
[@expression] => full
[@width] => 644
[@height] => 429
)
$count_total = count($temp);
如果你的数组在上面,你可以算作1;
答案 3 :(得分:0)
如果您获得单个结果,请执行以下操作:
$temp = Array
(
[@url] => url
[@type] => image/jpeg
[@expression] => full
[@width] => 644
[@height] => 429
);
单一结果:
$new_temp[0] = $temp;
现在你得到:
count($new_temp); // get 1
答案 4 :(得分:0)
$temp = Array
(
[0] => Array
(
[@url] => url
[@type] => image/jpeg
[@expression] => full
[@width] => 644
[@height] => 429
)
);
$count_total = count($temp); //gives you 1
问题是,在你的第一个代码块中,你试图计算数组中元素的数量,在第二个块中是另一个数组的内部数据。
答案 5 :(得分:0)
它正在计算数组中的元素数量。第一个给你5因为数组有5个元素。第二个是给你2,因为它是一个包含2个元素(数组)的多数组。
要使它返回一个,你可以把数组放在一个数组中,所以像这样:
Array([0] => Array())
我希望这能回答你的问题。