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

Compare and remove array elements

i have a bug in this code that i cannot seem to solve. if there is only 1 instance of Act, it works as it should. But when there is more than 1 instance of Act, it breaks. Not sure what I am missing here. //Find all instances of italics var…
RW Hammond
  • 113
  • 7
-2
votes
4 answers

How to remove objects from array when splice() reorders

I have an Javascript array of objects. I'm looping through the array with a forEach and checking for a specific match, and if that match is true, splicing/removing it from the array at the current index. Am I mad? When I find a match and…
user2305363
  • 157
  • 2
  • 15
-2
votes
4 answers

Why arrays.splice() function does not remove the element?

I have a strange behaviour with the splice() function. Suppose that this is the array: var arrObj = [{"key1":"val1"},{"key2":"val2"}]; If I make from the browser console: arrObj.splice(0,1); The first element is removed as expected. But if I make…
Alessandro C
  • 3,310
  • 9
  • 46
  • 82
-2
votes
1 answer

array_splice() is not working as expected

Some help please with this thing that was confused me about the correct use of array_splice(); When i copy literally the following code from the php.net website, that appears as follow: $input = array("red", "green", "blue",…
-2
votes
1 answer

Node js remove sensitive data from object before sending it

I want to remove some sensitive data from the array before sending to the client (to render all the users), but for some reason I can't actually remove them! I need to remove some usernames, passwords, emails etc. Is there something with splice() ?…
-2
votes
4 answers

Why does adding [0] after the delete count in splice() return just a number?

First of all sorry for the long title, I really didn't know how to word it better. In the middle of solving _.shuffle in underscore, I encountered the use of splice. Here is my original code : shuffle = function(array) { var shuffledArray = []; …
Tinah
  • 7
  • 2
-2
votes
1 answer

Remove item from json array using its random ids

I have an array which contains values as follows: { "123456": { "name": "tom", "projects": { "987654": { "cli": "abcd", "org": "123456", "cli_e": "abcd", "pro": "abcd", "status": "6" } …
Jay
  • 3,353
  • 5
  • 25
  • 34
-3
votes
1 answer

randomly select value from list and remove that item from the list

I was in need of a script that would allow me to randomly select a value from a list and remove that value from the list - thereby, decrementing the total items in the list by one item each time the script was executed. I needed this script so I…
-3
votes
2 answers

Javascript splice method issue with this

I will be happy if someone could help me with my problem : I'm trying to delete the current max number form the nums Array and I couldn't get it done. function getNthLargestNum(nums, nth) { debugger; var maxNums = []; for (var i = 0; i <…
B.Arthur
  • 1
  • 3
-4
votes
2 answers

Split string into array with various delimiters in PHP

I want to know how should I use regex to split this into an array: input = "1254033577 2009-09-27 06:39:37 "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_4_11; en) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1" 44.12.96.2 …
Aakash Shah
  • 669
  • 1
  • 5
  • 12
1 2 3
17
18