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
2
votes
2 answers

JS fn optional-parameter(s) given ‘undefined’ (to get default) fails for built-ins at least ‘new Date(yr,moi,d,hr,min,s,ms)’ +Array.prototype.splice

Official JS docs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters says ‘In JavaScript, function parameters default to [pseudo-value ]undefined [when the the parameters are omitted].’ (and indeed I've…
2
votes
1 answer

Splice removing the wrong index

so I'm trying to build a dynamic textfield, everything is working fine but I'm having a problem removing a data from state/array. splice is always removing the last index instead of a custom index. here's my whole code. I'm using reactjs as frontend…
Jay Rhick
  • 53
  • 5
2
votes
1 answer

How to push array elemnt to specific position in another array using php

I want to push new array to another array at specific position for this purpose I used array_splice followed some stackoverflow links but it didn't work for me I refered this links also but they mentioned only for single value not array. How to…
user_1234
  • 741
  • 1
  • 9
  • 22
2
votes
3 answers

Trying to create copies of an array using spread operator, but some how the array is being mutated

I'm trying to practice with the concept of immutability. I'm using the the spliceTest array as my main reference for creating copies of the array and mutating those. I'm coming to the problem when I declare removeOneItem variable, I somehow can't…
2
votes
3 answers

Remove array from inside array based on index in JS

I have an array which looks like:- [[0,1], [0,2], [0,3], [1,1], [1,2]...] I am looking to remove one of the arrays from this array based on the indexOf() but I keep getting a value of -1, which removes the last item from the array when I try the…
Web Nexus
  • 1,150
  • 9
  • 28
2
votes
2 answers

JavaScript find and remove an element from an array using splice not supported in IE9

I have an array in JavaScript and I need to find a specific element and remove it. I tried with splice() and findIndex() and it's not supported in JSEclipse nor in IE9. I used splice() and find() and it doesn't work in IE9. There are 2 points why…
Binyamin Regev
  • 914
  • 5
  • 19
  • 31
2
votes
1 answer

Remove an array item nested inside of an object

I'm trying to remove a specific item from an objects array based on the title attribute in the array. I keep running into a problem where I can view the array item, but I'm not able to splice the item out of the array based on the parameters entered…
JackJack
  • 181
  • 1
  • 1
  • 20
2
votes
2 answers

remove elements from front of Buffer node.js

I know there is a function called slice() but I am looking for splice() and that function doesn't exist how would I go about doing it some other way? var buffer = new Buffer("090001060001020304090000060001020304", "hex"); var packetLength =…
SSpoke
  • 5,656
  • 10
  • 72
  • 124
2
votes
1 answer

Splice is not working perfectly to remove all the array in angularjs

I have User angular.forEach to push all the data and I have to remove all the data which I was pushed, So I used splice but it's not working. What I have to do instead of splice or is any method to remove all array. Following is my…
Vishnu
  • 745
  • 12
  • 32
2
votes
1 answer

PHP Array Index/splice

I'm using PHP 5.4 (and no I can't upgrade to something newer). I'm trying to find the index of an element in an array, but I'm pretty sure I'm doing something wrong since when I call array_splice() I'm getting an illegal string offset error. I'm…
djd97
  • 77
  • 1
  • 1
  • 10
2
votes
2 answers

Cannot reorder an array of objects in JS

I have an array of objects which are presented to the user as blocks. They can drag and drop to change the order that that blocks appear, which I then want to change the position of the objects in the array. $scope.myArray = [{a:1,b:2,c:3},…
tester123
  • 1,033
  • 2
  • 12
  • 17
2
votes
2 answers

Why doesn't this for loop seem to execute although the condition is still true?

In the following SSCCE, why isn't the for loop being executed for $a greater than 3, although the condition should let it execute till $a becomes 5. And the output of the last statement is even more weird. What I am trying to achieve is that I want…
Solace
  • 8,612
  • 22
  • 95
  • 183
2
votes
1 answer

Why does this work in JS and not PHP?

Did a project in coding school a while back in JS where we were using the Sieve of Eratosthenes (I can't not read in that in some kind of booming, echoey voice) to output all the prime numbers up to a user inputted number. The code we came up with…
Steve
  • 21
  • 4
2
votes
3 answers

Array.Splice() does not remove element at zero index when the indexed variable is checked under if condition

Why below array is not being removed. var removeableIndex =-1; bookingStatus.bookings.filter(function(item, index) { if(item.id == id){ removeableIndex = index; return true; } …
Desmond
  • 1,308
  • 1
  • 19
  • 27
2
votes
4 answers

Splice() does not make the array empty

I have arrays x,y and z. While iterating through x, based on a condition I need to keep removing the elements from z. Here's what I am trying to do: var x = ["test0", "test1", "test2"]; var y = ["test0", "test1", "test2"]; var z = ["test0", "test1",…
Mustang
  • 604
  • 2
  • 13
  • 32