Questions tagged [array-splice]

PHP's array_splice() function helps to replace or extract array elements for particular offset. It accepts the array, offset, optionally length and replacement array if to be replaced. It returns the array consisting of the extracted elements.

PHP's array_splice() function helps to replace or extract array elements for particular offset. It accepts the array, offset, optionally length and replacement array if to be replaced. It returns the array consisting of the extracted elements.

array array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = FALSE ]] )

Example:

$input = array("a", "b", "c", "d", "e");
var_dump(array_slice($input, 2));
var_dump(array_slice($input, -2, 1)); 
var_dump(array_slice($input, 0, 3)); 

Result:

Array
(
    [0] => c
    [1] => d
    [2] => e
)

Array
(
    [0] => d
)

Array
(
    [0] => a
    [1] => b
    [2] => c
)
265 questions
1447
votes
29 answers

Deleting array elements in JavaScript - delete vs splice

What is the difference between using the delete operator on the array element as opposed to using the Array.splice method? For example: myArray = ['a', 'b', 'c', 'd']; delete myArray[1]; // or myArray.splice (1, 1); Why even have the splice…
lYriCAlsSH
  • 57,436
  • 10
  • 26
  • 20
38
votes
9 answers

How to remove all element from array except the first one in javascript

I want to remove all element from array except the element of array at 0th index ["a", "b", "c", "d", "e", "f"] Output should be a
shadowhunter_077
  • 454
  • 1
  • 6
  • 13
25
votes
4 answers

Javascript "shift" versus "splice" - are these statements equal?

I just want to confirm if the following two Javascript statements produces the same results, as it seems to me: First: var element = my_array.splice(0,1)[0]; Second: var element = my_array.shift(); I want to substitute the first by the second, in…
J. Bruni
  • 20,322
  • 12
  • 75
  • 92
24
votes
7 answers

Is there a way to use Array.splice in javascript with the third parameter as an array?

I'm attempting the following: var a1 = ['a', 'e', 'f']; // [a, e, f] var a2 = ['b', 'c', 'd']; // [b, c, d] a1.splice(1, 0, a2); // expected [a, b, c, d, e, f] // actual (a, [b, c, d], e, f] I am confined in my…
CoryG
  • 2,429
  • 3
  • 25
  • 60
23
votes
5 answers

angularjs forEach and splice

I have an array like this: $scope.emails = [ {"key":"Work","value":"user@domine.com"}, {"key":"","value":""}, {"key":"Work","value":"user2@domine.com"} {"key":"","value":""}]; So, I want to remove empty emails but angular forEach method…
chandu
  • 2,276
  • 3
  • 20
  • 35
22
votes
4 answers

Confusion with javascript array.splice()

I'm really confused about this. My understanding was that array.splice(startIndex, deleteLength, insertThing) would insert insertThing into the result of splice() at startIndex and delete deleteLength's worth of entries? ... so: var a =…
Trolleymusic
  • 2,162
  • 3
  • 19
  • 26
21
votes
9 answers

Comparing and Filtering two arrays

I've been trying to implement a function where given with two arrays, array1's elements is used as conditions to filter out elements in array2. For instance: array1= [apple, grapes, oranges] array2= [potato, pears, grapes, berries, apples,…
Alejandro
  • 2,266
  • 6
  • 35
  • 63
20
votes
5 answers

Best way to delete "column" from multidimensional array

I have a multidimensional php array that represents a table like this ------------- | A | 0 | A | |---|---|---| | 0 | 0 | 0 | |---|---|---| | A | 0 | A | ------------- so the array looks like this: array (size=3) 0 => array (size=3) 0…
Horen
  • 11,184
  • 11
  • 71
  • 113
12
votes
3 answers

array_splice preserving keys

I faced the situation that splicing arrays with preserved-keys, so I made the following function. I reached the solution that wrapping each items with array, but there seems to be some memory-inefficient statements. Have you any ideas? Thank…
mpyw
  • 5,526
  • 4
  • 30
  • 36
11
votes
1 answer

How to splice an array to insert array at specific position?

$custom = Array( Array( 'name' => $name1, 'url' => $url1 ), Array( 'name' => $name_a, 'url' => $url_a ) ); I am…
crmpicco
  • 16,605
  • 26
  • 134
  • 210
9
votes
5 answers

How can I remove a single array member using array_splice in php?

I think I may not understand correctly how array_splice is supposed to work. My understanding is that the first param is your initial array, the second param is the element to start at, and the third param is the length, or number of elements to…
EmmyS
  • 11,892
  • 48
  • 101
  • 156
7
votes
1 answer

Funny behaviour of Array.splice()

I was experimenting with the splice() method in jconsole a = [1,2,3,4,5,6,7,8,9,10] 1,2,3,4,5,6,7,8,9,10 Here, a is a simple array from 1 to 10. b = ['a','b','c'] a,b,c And this is b a.splice(0, 2, b) 1,2 a a,b,c,3,4,5,6,7,8,9,10 When I pass the…
janesconference
  • 6,333
  • 8
  • 55
  • 73
7
votes
2 answers

Insert object into array

i have an array in php full of "Eventos Calendario" objects, at some point in my script i need to introduce a new object of the same type at position x of this array. this is the code i am using $EventoLimite = new EventosCalendario(null,$Timestamp,…
user1181589
  • 153
  • 1
  • 10
6
votes
2 answers

PHP - Need help inserting arrays into Associative arrays at given keys

Ok, so I have an array like so: $buttons = array( 'home' => array( 'title' => $txt['home'], 'href' => $scripturl, 'show' => true, 'sub_buttons' => array( ), 'is_last' => $context['right_to_left'], …
SoLoGHoST
  • 2,673
  • 7
  • 30
  • 51
6
votes
5 answers

Insert elements from one array (one-at-a-time) after every second element of another array (un-even zippering)

What would be an elegant way to merge two arrays, such that the resulting array has two items from the first array followed by a single item from the second array, repeating in this fashion? $array1 = ['A1', 'A2', 'A3', 'A4', 'A5']; // potentially…
wes
  • 734
  • 6
  • 14
1
2 3
17 18