0

我正在使用array_push()方法从我的数据库中插入 id。

例子:

print_r($arr_usr_ids);
Array ( [0] => 34 [1] => 35 [2] => 34 [3] => 37 [4] => 38 [5] => 30 ... ) 

$arr_usrs = array();

foreach($arr_usr_ids as $key => $value) {
if(isset($group_id[1]) && '1'=== $group_id[0]) {
 $numbers = $arr_usr_ids[$key]; // 30
 $arr_usrs[] = $numbers;
} else

if(isset($group_id[1]) && '2'=== $group_id[0]) {
 $numbers = $arr_usr_ids[$key]; // 34,33
 $arr_usrs[] = $numbers;
} 
...
}

等等...

但 array_push 正在订购数字

print_r($arr_usrs);
Array ( [0] => 30 [1] => 33 [2] => 34 [3] => 37 [4] => 38 ) 

正确的结果是 30,34,33,38,37

编辑这个作品:

$arr_usrs_one = array();
$arr_usrs_two = array();
$arr_usrs_three = array();

foreach($arr_usr_ids as $key => $value) {
if(isset($group_id[1]) && '1'=== $group_id[0]) {
 $numbers = $arr_usr_ids[$key]; // 30
 $arr_usrs_one[] = $numbers;
} else

if(isset($group_id[1]) && '2'=== $group_id[0]) {
 $numbers = $arr_usr_ids[$key]; // 34,33
 $arr_usrs_two[] = $numbers;
}
... 
}

$result_usrs = array_merge($arr_usrs_one,$arr_usrs_two,$arr_usrs_three);
4

3 回答 3

1

只需从更改array_push[]

$arr_usrs = array();

if(isset($group_id[1]) && '1'=== $group_id[0]) {
 $numbers = $arr_usr_ids; // 30
 $arr_usrs[] = $numbers;
}

if(isset($group_id[1]) && '2'=== $group_id[0]) {
 $numbers = $arr_usr_ids; // 34,33
 $arr_usrs[] = $numbers;
}
print_r($arr_usrs);
于 2013-05-10T08:40:15.157 回答
0

$array_push指一个变量。

你正确的意思是array_push

array_push($array, $value); 

女巫等于:

$array[] = $value;

文件:

array_push()array视为堆栈,并将传递的变量推送到array的末尾。数组的长度会随着推送的变量数量而增加。与以下效果相同:

<?php
$array[] = $var;
?>

对每个 var 重复。

于 2013-05-10T08:40:27.017 回答
0

array_push是一个函数。PF手册

$arr_usrs = array();

if(isset($group_id[1]) && '1'=== $group_id[0]) {
 $numbers = $arr_usr_ids; // 30
 array_push($arr_usrs,$numbers);
}

if(isset($group_id[1]) && '2'=== $group_id[0]) {
 $numbers = $arr_usr_ids; // 34,33
 array_push($arr_usrs,$numbers);
}
于 2013-05-10T08:41:55.317 回答