0

I am using the array_push() method to insert ids from my database.

Example:

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;
} 
...
}

and so on...

but array_push is ordering the numbers

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

the correct result would be 30,34,33,38,37

EDIT this works:

$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);
Jim
  • 923
  • 4
  • 17
  • 30
  • 3
    $array_push is not a function.. do you mean array_push? – Jayyrus May 10 '13 at 08:39
  • If $array_push is a custom function or closure, then show us the code for that – Mark Baker May 10 '13 at 08:40
  • no its the standard "PHP Array Function -> array_push" http://www.php.net/manual/en/function.array-push.php – Jim May 10 '13 at 08:42
  • If it's the standard function, then what is the datatype for $arr_usr_ids, and how are you getting what looks like a comma-separated string value returned when you show your print_r result. What else are you doing to $arr_usrs? – Mark Baker May 10 '13 at 08:43
  • this would be hard to explain the code comes from my typo3 extension and would be out of the focus of this question...i try another way with array_merge. I put in every "if" a unique array name and use the array_merge at the end to put all together, so i get my right order. I know this is not the best way but it works. – Jim May 10 '13 at 09:58
  • @MarkBaker datatype for $arr_usr_ids is tinytext and with the result of the numbers i select users from my database with this order... – Jim May 10 '13 at 10:04
  • Again, I see your edit still has `__$__array_push()` rather than `array_push()`.... is it really `$array_push` in your code? – Mark Baker May 10 '13 at 10:09
  • @MarkBaker updated with more details – Jim May 10 '13 at 10:18
  • Unable to replicate `$arr_usrs = array(); $arr_usr_ids = array('30', '34,33', '38,37'); foreach($arr_usr_ids as $key => $value) { $numbers = $arr_usr_ids[$key]; array_push($arr_usrs,$numbers); } $newnumbers = implode(',', $arr_usrs); var_dump($newnumbers);` – Mark Baker May 10 '13 at 10:29

3 Answers3

1

Just change from array_push to []:

$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);
Jayyrus
  • 12,961
  • 41
  • 132
  • 214
  • has the same effect as with array_push...instead of this 30,34,33,37,38,35 i get this 30,33,34,35,37,38 – Jim May 10 '13 at 09:00
  • @Jim - array_push does __not__ order.... what are you doing to $arr_usrs to get a comma separated string? – Mark Baker May 10 '13 at 09:16
  • @MarkBaker simply this $newnumbers = implode(',', $arr_usrs); but this is not important because print_r($arr_usrs); shows me the wrong order :/ – Jim May 10 '13 at 09:23
  • 1
    @Jim - can you please edit your original question with the real code that you're using; because it also still has $array_push() rather than array_push(), and no indication of the loop that you're using, or where $arr_usr_ids comes from.... just enough of the code to allow us to recreate the problem – Mark Baker May 10 '13 at 09:26
0

$array_push refers to a variable.

You properly mean array_push:

array_push($array, $value); 

Witch is equal to:

$array[] = $value;

Docs:

array_push() treats array as a stack, and pushes the passed variables onto the end of array. The length of array increases by the number of variables pushed. Has the same effect as:

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

repeated for each var.

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
0

array_push is a function. PF the Manual

$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);
}
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126