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
3
votes
5 answers

Splicing first object returns TypeError: Cannot read property of undefined

I have an array like this: var arrSession = [ { name: 'Product 01', groupID: '50303', delivery: 'mail'}, { name: 'Product 02', groupID: '50403', delivery: 'bike'} ] And this Loop to delete specific objects: for(var i=0, len=arrSession.length;…
Philipp M
  • 3,306
  • 5
  • 36
  • 90
3
votes
1 answer

Splice in function doesn't change a character as expected

I've been working for a while on this function but i cannot figure out why even if I'm using .splice() I don't get a modified array. I'm providing the index at which to start changing the array "i", the number of elements to remove "1" and the…
Mugg84
  • 585
  • 1
  • 7
  • 15
3
votes
3 answers

How to push object into an array? in Angular 7

I am pushing an object into an array but cannot do it? I'm doing it like this this.passData = this.tribeForm.value; var id = {"tribe_id": 1} this.passData.push(id) This is the value in the tribeForm I also tried var id = {tribe_id:…
Mark
  • 293
  • 1
  • 11
  • 25
3
votes
1 answer

Send index from child component to the parent one to remove array item in v-for - VUE.JS 2

I am a little newbie using Vue JS, so i started with Vue 2. I need to remove an array item but the button that trigger that method is inside a template and the v-for is inside a parent template. This is my HTML: MAIN
3
votes
6 answers

Base Algorithm Scripting by chopping the array with splice function in Javascript

Now I am working on a exercise in freecodecamp. Currently I got an logical error but do not why the failure happens. In the code,I have to build in a function, which chop the input array based on the parameter. The testing result should be as…
Pak Hang Leung
  • 389
  • 5
  • 15
3
votes
2 answers

Angular5 compare 2 array of (JSON) objects and splice when found match

If i have 2 arrays with (JSON) objects and i want to compare them and splice an object when there is a match what is the best way to do this. Example: Array 1: [{test: 1, test2: 2}, {test: 3, test2: 5}, {test: 6, test2: 8}] Array 2: [{test: 6,…
Hans
  • 287
  • 1
  • 5
  • 18
3
votes
3 answers

Array splice with a custom key

Say I have this code $test = array(); $test['zero'] = 'abc'; $test['two'] = 'ghi'; $test['three'] = 'jkl'; dump($test); array_splice($test, 1, 0, 'def'); dump($test); Which gives me the output Array ( [zero] => abc [two] => ghi …
TMH
  • 6,096
  • 7
  • 51
  • 88
3
votes
1 answer

Using MPI_Type_Create_Subarray in FORTRAN 90

This is related to the earlier thread on "Using MPI_Send/Recv to handle chunk of multi-dim array in Fortran 90". My array is real and 3-dim say 5 by 5 by 5, i.e x(1:5,1:5,1:5) If I want to send the following part of my array say x(2:3,2:5,4:5) to…
Madhurjya
  • 497
  • 5
  • 17
3
votes
1 answer

Getting this PHP error: fputcsv() expects parameter 2 to be array

I am trying to insert an array into an array of arrays using array_splice. array_splice($array, 0, 0, $fieldNames); The resulting array is then converted to csv format with fputcsv. I get the following error: fputcsv() expects parameter 2 to be…
Rynardt
  • 5,547
  • 7
  • 31
  • 43
3
votes
2 answers

PHP: Will using array_splice on an array that's the subject of a foreach() cause problems?

I am writing some PHP code that's kind of like this: foreach($filter[1] as $reject){ $reject_processed = preg_replace('~\s~','',strtolower($filter[1][$reject])); if(array_key_exists($reject_processed,$list_of_common_replacements)){ …
SoItBegins
  • 414
  • 1
  • 6
  • 22
3
votes
1 answer

using reference (&) in foreach add `&` in output array? is it a bug

I am using PHP 5.3.5, and I am stuck with an error. I have an array $input = array( 0=>array( 'a'=>'one0', 'b'=>'two0', 'c'=>'three0', …
xkeshav
  • 53,360
  • 44
  • 177
  • 245
2
votes
2 answers

Compare 2 arrays and remove matches

I have 2 arrays (@system,@reserve). Each contain a list of numbers and I'd like to compare and splice out (maybe?) the numbers in @reserve which match numbers in @system. I've tried some of the responses to find and splice questions out there, but…
Martin Sloan
  • 119
  • 10
2
votes
2 answers

as3: does array splice delete an object completely?

I have a lot of display objects in an array that I'm constantly adding and removing from stage. When removed they are not used anymore. Considering the displayObject is not on the display list, and has no event listeners... will it be garbage…
Pier
  • 10,298
  • 17
  • 67
  • 113
2
votes
3 answers

How to remove elements from object of arrays

I use an object of arrays and I want to delete a specific value in 1 of the arrays. let medici= ["Person1","Person2", "Person3", "Person4", "Person5", "Person6" ]; let giorni = ["Lun", "Mar", "Mer","Gio","Ven"]; let presenti ={}; for (let giorno…
Damiano
  • 25
  • 5
2
votes
3 answers

Vue JS - How to apply color to text of specific element from array then remove last comma

I am using date, the user selects the date, and then the selected date is displayed in the browser, the problem is that I want to change the color for the last two items except that remove the last comma I used splice to remove the first word, but I…
Synchro
  • 1,105
  • 3
  • 14
  • 44
1 2
3
17 18