0

I am having an issue with understanding why my array containing 3 elements must be sliced into 2 parts each. I wish to access a number I'm pushing into the array only however it seems to print out the index rather than the 'key' value I pushed into it ($number).

I have a 2d array I'm pushing an ID and an integer into, and then sort it :

$array = [[]];
array_push($array, $doc[_id], $number);
array_multisort($array);

I then filter any empty elements:

$array = array_filter($array); //remove null elements

This all works as id expect however the array looks like this by this point:

 unrated.array(5) 
 { 
 [2]=> object(MongoId)#32 (1) 
 { ["$id"]=> string(24) "57b99696ce2350100b000029" } 

 [3]=> object(MongoId)#31 (1) 
 { ["$id"]=> string(24) "57b998ccce2350181700002b" } 

 [4]=> object(MongoId)#33 (1) 
 { ["$id"]=> string(24) "57b99a84ce2350100b00002b" } 

 [5]=> int(2) [6]=> int(3) 

 }

Again, this is fine however it means when I loop over the array using the code below it appears to be longer than 3 elements, as I have to slice from 0-6 instead of 0-3:

$array = array_slice($array, 0, 6, true); //only get 3 elements from array
foreach ($array as $key => $value) {
echo $key; //prints out values from 1-5 weirdly.... should just print the $number value
$id = $value->{'$id'};
}

What I am trying to achieve is to find the element in the array with the lowest possible value that was pushed earlier (array_push($array, $doc[_id], $number);) however because I cannot understand why the array is split into 6 rather than 3 parts its even more confusing.

Question in short : How do I access the $number pushed into the array and why is my array 6 seemingly 6 in size when it contains only 3 elements.

Any help would be appreciated, thanks.

3
  • What is it that you expected array_push($array, $doc[_id], $number) to do, exactly? Because what that is actually doing is pushing two values onto the end of the array. The value of $doc[_id] and the value of $number. Which, if you're doing that 3 times, would explain why you're seeing 6 elements in the array. Commented Aug 21, 2016 at 13:10
  • Well i just need a way to access both the ID and the $number in my loop, so i expected pushing the $number to the array and the ID and sorting it would result in the array being sorted by the $number value. But ultimately the main issue i need solved is how to access the $number value from the loop as sorting etc i can solve with more research if there is an issue with it. Commented Aug 21, 2016 at 13:12
  • 1
    Well, your expectations are wrong then, because both $number and id would be values in the array. As such sorting the array would mean sorting both values. What you need to do is assign the $number directly to the id key instead. Commented Aug 21, 2016 at 13:17

1 Answer 1

2

To be clear, array_push simply pushes one or more values onto the end of an array. The first argument of array_push is the array you wish to push the value(s) to, and any subsequent argument is a list of values you wish to push. So what you're doing with array_push($array, $doc[_id], $number) is pushing two values ($doc[_id] and $number) to the end of the array $array. array_push will just use the next available index as the key when it adds those values to the array. It will not allow you to specify a key. This is the same thing as doing $array[] = $value.

To specify a key you must assign a value directly to the array key like so: $array[$key] = $value.

Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way to push the ID to the array and set the $number as the index of the array at the same time?
Yes, you just specify the $number as the key, which is the exact opposite of what I'm doing here $array[$id] = $number then becomes $array[$number] = $id. Keep in mind that an array key is unique in a PHP array. As such you cannot have more than one key of the same value. Please be sure to read the manual for more details on using arrays.
Awesome, thanks so much i'll look into this further! very helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.