0

Currently i have an array $newArr with some elements as shown in picture below. How do I know the last digit of the array index (highlighted in yellow)?

enter image description here

This is important because, if later I wanted to insert a new record into this $newArr array, I could just

$newArr[$the_variable_that_holds_the_last_digit + 1] = ['foo', 'bar']; 

otherwise the whole array overwrite if

$newArr = ['foo', 'bar']; 
3
  • 2
    php.net/end Commented Aug 10, 2017 at 13:23
  • you can use end method Commented Aug 10, 2017 at 13:27
  • 1
    Many answers had been given. I just want to add that if you want to add a value at the end of your array, you can just do $newArr[] = ['foo', 'bar']; or array_push($newArr, ['foo', 'bar']); Commented Aug 10, 2017 at 13:28

4 Answers 4

3

I think you are looking for end pointer

$array = array( 'a' => 1, 'b' => 2, 'c' => 3, ); end($array); // it will point to last key $key = key($array); // get the last key using `key` 
Sign up to request clarification or add additional context in comments.

2 Comments

why does key($newArr) returns 0? It suppose to be 2, no?
yes, it should return 2 if everything going proper, if you paste my example it will return $key = c.
2

Assuming you have the numerically indexed array, the last index on your array is :

$last_index = count($newArr) -1; 

if However your keys are not sequential, you can do this:

end($newArr); $last_key = key($newArr); 

6 Comments

Not good if your array looks like this: ['0' => ..., '1' => ..., '3' => ...].
Thats why I said Assuming you have the numerically indexed array. the solution is for numerical keys only.
0,1,3,7 are numeric, they are just NOT consecutive. count = 4 but Highest index value = 7
OK I edited the answer. Check it out and let me know if that works.
why does key($newArr) returns 0? It suppose to be 2, no?
|
2

I think you can try this

$array = end($newArr); $last_index = key($array);//Its display last key of array 

For more details, please follow this link.

4 Comments

key will only provide the current key, i.e first, you need to move pointer to last then access the key
No its not the answer, because the OP wants a key of the last element, NOT the last element. end() returns the last element of the array.
why does key($newArr) returns 0? It suppose to be 2, no?
@begineeeerrrr When you should only use key($newArr) then this function return current key only but you should use as per my answer then return last key of array.
1

If the only reason is to not overwrite the values you can use [] which means add new value.

$arr = [1,2,3,4]; var_dump($arr); // incorrect way: $arr = [1,2]; var_dump($arr); //correct way $arr = [1,2,3,4]; $arr[] = [1,2]; var_dump($arr); 

See here for output: https://3v4l.org/ZTg28

The "correct way" will in the example above input a new array in the array.
If you want to add only the values you need to insert them one at the time.

$arr[] = 1; $arr[] = 2; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.