Array (
[REF_DETAILS] = Array
(
[0] = Array
(
[ID] => 1231312
[USER] => USER
)
[1] = Array
(
[TID] => 2754042
[USER] = USER
)
[1] = Array
(
[TID] => 534535
[USER] = USER
)
)
[TOTAL_COUNT] = 31
)
I have a array output like this above and I want to remove one element from the array then again i want to reindex it from 0. I tried with array_value in php but after doing this it is removing [REF_DETAIL] with 0 and TOTAL_COUNT as 1 , please provide the solution in php
The re-index of an array can be done by using some inbuilt function together. These functions are: array_combine() Function: The array_combine() function is an inbuilt function in PHP which is used to combine two arrays and create a new array by using one array for keys and another array for values.
pop() function: This method is use to remove elements from the end of an array. shift() function: This method is use to remove elements from the start of an array. splice() function: This method is use to remove elements from the specific index of an array.
This is done in O(1) because it is pretty simple (constant number of math calculations) where the element is located given the index, the beginning of the array and the size of each element.
Most efficient way to remove an element from an array, then reduce the size of the array.
Use array_splice
(php docs), it automatically reindexes.
array_splice($array['REF_DETAILS'], 1, 1)
try
unset($array['REF_DETAILS'][1]);
$array['REF_DETAILS'] = array_values($array['REF_DETAILS']);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With