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
-1
votes
1 answer

Understanding splice or rather not understanding it?

let fish = [ "piranha", "barracuda", "koi", "eel" ]; // Remove two items, starting at index position 1 fish.splice(1, 2); fish; Since the first digit 1 represents position "barracuda" within the array and 2 represents the items to be removed, Why…
-1
votes
1 answer

Is this splice definition Correct?

Hello just asking is this AngularJS splice definition is correct, if not, what is the correct form of use $scope.user=[]; $scope.userfakemodi={}; $scope.user.splice(indexuser,0,$scope.userfakemodi); Imagine that I have any data in the…
-1
votes
1 answer

How would i create n no of chunks from an array of dynamic length javascript

I have an array of n length(dynamic) say 830... I want this array to be split in sub-array such in the manner [0,100],[100,200].....[800,829] i.e in equal no of chunks How would I execute this?? I have tried below code so far with reference of this…
p u
  • 1,395
  • 1
  • 17
  • 30
-1
votes
1 answer

How can I compare spliced multidimensional arrays (with huge amount of data)?

I have a huge array $properties with about 500.000 items: array(470000) { ["12345"]=> array(5) { ["dateTime"]=> string(19) "2016-10-12 19:46:25" ["fileName"]=> string(46) "monkey.jpg" ["path"]=> …
-1
votes
1 answer

splice function doesn't work on array of integer as expected?

I was trying to use the splice() function to manipulate an array of integers. But all I am getting is strange results. Here is my code var g = [0,1]; //Expected result [0, 1, 1] //Actual result [0] console.log(g.splice(0, 1, 0, 1)); //Expected…
-1
votes
4 answers

How to use array.splice() after .join()?

I'm having trouble figuring out how to remove items from an array after using the join() function. As you'll see, the first array.splice() works, but the second does not... var recordsArray1 = ['a','b','c','d','e','f']; …
Tim Samoff
  • 94
  • 11
-1
votes
3 answers

extract information in between the * symbol of the text and save each data into a separate array

I have a text file as "a.txt", which has a data like the image. I want to read this text file line by line and save each entry between the * symbol and save into a separate array. Note: each information I want to save into a separate array. I…
-1
votes
3 answers

Insert elements at an array position even if the array is empty or position doesn't exist

I tried to use the array_splice strategy as described here Insert new item in array on any position in PHP But it doesn't work since the array is empty or the key doesn't exist. So I tried checking if the key is set first and then create it. But…
Victor Ferreira
  • 6,151
  • 13
  • 64
  • 120
-1
votes
1 answer

Spliced array in Javascript is irregular

I'm trying to splice an array when an element is clicked. When I console.log the new array it's irregular and sometimes removes the wrong array element and the last index will never be removed. Any one here with a good explanation to this? var…
NinjaGrisen
  • 29
  • 1
  • 6
-1
votes
2 answers

Javascript Array splice removes data but does not reset index

I have an array of objects. It had 3 objects I then deleted them one by one using arr.splice(0,1) which I repeated 3 times. I then added a new object to the (what is supposed to be an empty array) and printed out the ENTIRE array in console. As…
Michael Seltenreich
  • 3,013
  • 2
  • 29
  • 55
-1
votes
4 answers

When array length = 0, it stills shows as 1

I am dealing with an array that I want to delete an object from and return the new length of the array. For all other numbers, it works - but for one item, it does not. Not sure how to fix it so that the array length is 0 after the only object is…
itamar
  • 3,837
  • 5
  • 35
  • 60
-2
votes
4 answers

Filter an Array of strings using Filtered String and Remove from Original Array

I want to remove some elements from the array containing the word Evil (filterString). let guests = ["Partner", "Evil Nice Relative 1", "Nice Relative 2", "Evil One", "another evil", "another one", "another evil is here", "strange Evil is here",…
Tahir
  • 73
  • 2
  • 11
-2
votes
3 answers

List Slicing and Indexes

I'm trying to figure out why the output grabs the first letter of each item in the list instead of just grabbing the first element (buffalo wings)? Here's what my code looks like. faveFoods = ['buffalo wings' , 'menudo' , 'mashed potatoes' , 'man…
-2
votes
2 answers

Drop items from the beginning of the array inside a loop without mutating original array

so i am having a problem using javascript array splice where it changes the orginal array like let arr1 = [1,2,3,4,5] for(i = 0; i < 5; i++){arr1.splice(i,1);} in this above example , the arr1 gets changed after every iteration like :-…
Prastut Dahal
  • 27
  • 1
  • 6
-2
votes
5 answers

Given string how do I replace certain words with multiple words in an array?

Let's say I have a sentence and an array of words: let sentence = 'My * is * years old.'; let words = ['dog', 3]; //expected result => My dog is 9 years old. How would I replace each asterisk (*) with the appropriate word in the given array? But…
Chris
  • 21
1 2 3
17
18