Ⅰ php如何將SQL查詢結果轉為多維數組,並按查詢行輸出
因為是循環取的,你在循環外聲明一個空數組,在循環內,將結果集交給這個數橋慎組就行了,比如空數組時$arr=array,給數據的時候$arr[ ]=一氏皮行結果。php的數組下標是自動增長的
樓下也是個糊塗蛋,明明自殲消差己遍歷的是二維數組,還叫你用一維數組
Ⅱ php多維數組foreach取值
可以使用count函數來獲取第一維的元素個數
也可以使用count函數的遞歸形式獲取總元素個數
看以下示例:
<?php
$food = array('fruits' => array('orange', 'banana', 'apple'),
'veggie' => array('carrot', 'collard','pea'));
// recursive count
echo count($food, COUNT_RECURSIVE); // output 8
// normal count
echo count($food); // output 2
?>
foreach取出的應該是每個數組元素 你可以使用嵌套的foreach獲取每個元素
看以下示例:
$a = array();
$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";
foreach ($a as $v1) {
foreach ($v1 as $v2) {
echo "$v2\n";
}
}
Ⅲ PHP判斷多維數組值的大小
$arr = [
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5, 6, 7, 8],
[1, 2, 3, 4, 5, 6, 7]
];
echo count($arr); // 3 二維數組長度
echo count($arr[0]);// 5 二維數組子數組長度
echo count($arr[1]);// 8 二維數組子數組長度
你可以去後盾人平台看看,裡面的東西不錯
Ⅳ php多維數組如何使用
$User[0][0]的值就是'張三'、$User[0][1]的值就是88;
$User[1][0]的值就是'lisi'、$User[1][1]的值就是90;
....
更高級的用法見下面的例子程序:
<?php
$User =array(
'張三' => 88,
'lisi' => 90,
'王五' => 99
);
print_r($User);
?>
這樣$User['張三']的值就是88,$User['lisi']的值就是90,是一維數組,而且更加方便。
Ⅳ php取出多維數組重復的值
$arr = array(
array(
array(
"stocknotifyId" => 1,
"sku" => '13809_BL_37',
"email" => '[email protected]'
),
array(
"stocknotifyId" => 3,
"sku" => '13809_BL_37',
"email" => '[email protected]'
),
array(
"stocknotifyId" => 5,
"sku" => '13809_BL_37',
"email" => '[email protected]'
)
),
array(
array(
"stocknotifyId" => 2,
"sku" => '13809_B_39',
"email" => '[email protected]'
)
),
array(
array(
"stocknotifyId" => 4,
"sku" => '13809_BL_39',
"email" =>'[email protected]'
)
)
);
$re = array();
foreach($arr as $a) {
foreach ($a as $v) {
$tmp_v = $v;
unset($tmp_v['email']);
if(isset($re[$v['email']])) {
$re[$v['email']][] = $tmp_v;
}else{
$re[$v['email']] = array($tmp_v);
}
}
}
print_r($re);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<!-- output -->
Array
(
[[email protected]] => Array
(
[0] => Array
(
[stocknotifyId] => 1
[sku] => 13809_BL_37
)
[1] => Array
(
[stocknotifyId] => 2
[sku] => 13809_B_39
)
)
[[email protected]] => Array
(
[0] => Array
(
[stocknotifyId] => 3
[sku] => 13809_BL_37
)
[1] => Array
(
[stocknotifyId] => 4
[sku] => 13809_BL_39
)
)
[[email protected]] => Array
(
[0] => Array
(
[stocknotifyId] => 5
[sku] => 13809_BL_37
)
)
)