php数组合并有4种办法,分别为:1、利用加号“+”运算符,语法“数组1+数组2+..”;2、利用array_push()函数;3、利用array_merge()函数;4、利用array_merge_recursive()函数。
本教程操作环境:windows7系统、PHP7.1版,DELL G3电脑
1、利用加号“+”运算符:直接将两个数组的值合并成为一个数组;
$a = [1,2,3,'a'=>'a'];
$b = ['a'=>'b',4,5,'b'=>'c',6,7,'a'];
$c = $a + $b;
print_r($a);
print_r($b);
print_r($c);
//$a
Array
(
[0] => 1
[1] => 2
[2] => 3
[a] => a
)
//$b
Array
(
[a] => b
[0] => 4
[1] => 5
[b] => c
[2] => 6
[3] => 7
[4] => a
)
//$c
Array
(
[0] => 1
[1] => 2
[2] => 3
[a] => a
[b] => c
[3] => 7
[4] => a
)
采取"+"合并数组,如果键相同的情形下,前面的数组值会覆盖掉后面的数组值。
2、利用array_push()函数将某个变量放入到另一个数组当中
$a = [1,'a'=>'aa',2,'b'=>'bb'];
$b = [1,'a'=>'aa',2,'b'=>'bb'];
print_r($a);
print_r($b);
array_push($a, $b);
print_r($a);
//$a
Array
(
[0] => 1
[a] => aa
[1] => 2
[b] => bb
)
//$b
Array
(
[0] => 1
[a] => aa
[1] => 2
[b] => bb
)
// array_push过后的$a
Array
(
[0] => 1
[a] => aa
[1] => 2
[b] => bb
[2] => Array
(
[0] => 1
[a] => aa
[1] => 2
[b] => bb
)
)
通过打印的值可以看出,变量进入数组后,对应的数字索引一定是当前数组中最小的数字索引+1(如果当前数组不存在数字索引,新增元素对应的索引为0)
3、利用array_merge()函数直接将两个数组进行合并,两个数组键相同的情形,后面数组的值覆盖前面数组的值
$a = [1,'2'=>2,'a'=>'a','b'=>'b'];
$b = [1,'2'=>3,'a'=>'c','b'=>'d','c'=>'e'];
$c = array_merge($a,$b);
print_r($a);
print_r($b);
print_r($c);
//$a
Array
(
[0] => 1
[2] => 2
[a] => a
[b] => b
)
//$b
Array
(
[0] => 1
[2] => 3
[a] => c
[b] => d
[c] => e
)
//$c
Array
(
[0] => 1
[1] => 2
[a] => c
[b] => d
[2] => 1
[3] => 3
[c] => e
)
可以看出,两个数组键相同的情形,后面数组的值覆盖前面数组的值。但是对付数字索引或者数字字符串索引,会按照顺序进行重置(第一个数组的第一个数字索引元素从0开始依次添补)
4、利用array_merge_recursive()函数合并数组,碰着相同的键将该键中值合并为一个子数组
$a = [1,'2'=>2,'a'=>'a','b'=>'b'];
$b = [1,'2'=>3,'a'=>'a','b'=>'d','c'=>'e'];
$c = array_merge_recursive($a,$b);
print_r($a);
print_r($b);
print_r($c);
//$a
Array
(
[0] => 1
[2] => 2
[a] => a
[b] => b
)
//$b
Array
(
[0] => 1
[2] => 3
[a] => a
[b] => d
[c] => e
)
//$c
Array
(
[0] => 1
[1] => 2
[a] => Array
(
[0] => a
[1] => a
)
[b] => Array
(
[0] => b
[1] => d
)
[2] => 1
[3] => 3
[c] => e
)
采取"array_merge_recursive"方法时,会将相同键的元素,整合成为一个新的数组,保持键名不变,作为合并数组($a与$b组合后的数组)的一个子元素。但是对付数字索引或者数字字符串索引,会按照顺序进行重置(第一个数组的第一个数字索引元素从0开始依次添补)。纵然对应的数字索引相同,也不会将相同索引的元素整合成功一个子数组。
精确的sql语句:
1、SELECT own_id, dotime,note ,AES_DECRYPT(price,'66683821') AS jfprice,dNodeText FROM zb_yiduozhang2 WHERE left(`jSortID`,length('1001001'))= '1001001' AND own_id='66683821'