Ⅰ 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
)
)
)