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

Recursive function to build array

I'm trying to use a recursive function to build an array with inheritance. Let's say I have an object "a" that looks like this (with a Parent ID of "b") a = 'Item 1', 'Item 2', Parent_ID, 'Item 3', 'Item 4' And I have an object "b" that looks like…
djbokka
  • 1
  • 3
0
votes
1 answer

PHP: array_splice() not giving me correct output

I'm trying to experiment with array_splice and I get an output like this (from $match) Array ( [Keep me Updated] => Array ( [winner] => winnerl.jpg [0] => value0.jpg ) [0] => valuel.jpg //this should…
hellomello
  • 8,219
  • 39
  • 151
  • 297
0
votes
4 answers

Get random item from array, within a for loop, then remove from array

I am writing a script that onload will add a class to a random 4 of 12 DIV's and then remove the ID of the DIV from the array. I have an array setup with all 12 DIV ID's in it. Sometimes when I reload the page, 4 DIV'S have that class and other…
Papa Wheelz
  • 180
  • 4
  • 16
0
votes
3 answers

Array_splice to swap an element with previous in a multidimensional associative array

Swap-up element in a multidimensional array with the sibling above it. I want the element with the selected index in the array to swap it's position with the one above him. The element from it's position(N) to go to position(N-1) I want the element…
0
votes
3 answers

How to empty an array-like object, cross-browser (IE8)?

I have an object which is array-like. This means that it has numeric properties (0, 1, 2...) and a length property. In its prototype, I declare a method to clear it, like so: 'clear': function() { Array.prototype.splice.call(this, 0,…
0
votes
1 answer

Questions about actionscript

I have a few different question and i have decided to put them in one. So the first question : If i have one and the same code(for example): buttonsA.buton1a.addEventListener(MouseEvent.MOUSE_DOWN ,…
Yoan Dinkov
  • 531
  • 1
  • 5
  • 17
0
votes
1 answer

php foreach goes into infinite loop, array stored in session

All works perfectly, but when there are more one items in the cart.. and the the quantity of any item (except the last item in list) is changed the code below goes into infinite loop, i have verified it by placing print_r statements in it. The part…
0
votes
4 answers

foreach count does not get updated after array_splice

I have two large arrays: $a = array('a1','a2','a3','a4'); // and so on $b = array('b1'=>'a1', 'b2'=>'a3', 'b3'=>'a1'); // I would like to get the following result: $a = array('a1'=>array('b1', 'b3'), 'a2'=>array(), 'a3'=>array('b2') ); I could…
mgPePe
  • 5,677
  • 12
  • 52
  • 85
-1
votes
1 answer

How to split url parameters with ampersand as array

I want to split the parameters sent through URL. Here is my text. "mytext=A & B Company&anothertext=20&texttwo=SampleText&array[1]=10&array[2]=20" Expected output: ['mytext'=>'A & B Company', 'anothertext'=> 20, 'texttwo' => 'SampleText', …
Saravanan DS
  • 278
  • 3
  • 14
-1
votes
1 answer

JavaScript inline split(',').splice()

I'm trying to perform an inline split() then splice(), and it's not working. var newValue = "61471acddbbfef00961374b5ae961943,fafd1e39db3fa20084cc74b5ae961914"; var test =…
Zyre
  • 149
  • 1
  • 11
-1
votes
2 answers

I want remove an object from array by splice() but not working properly

I have an array that I want remove an object from this, I try write this by splice(), But when I found it and splice that, my code remove all objects Except object that found. It's my JavaScript code: On jsfiddle var MyArr = [{ "id": "139", …
-1
votes
2 answers

Separate camelCase string

My function is supposed to separate a camel-cased string (e.g. "camelCase" into "camel Case"). My logic is basically that I split the string into an array, identify uppercase characters, and then splice in a space character before the uppercase…
TOOTCODER
  • 25
  • 1
  • 1
  • 2
-1
votes
3 answers

2 random values from array project

Okey ive got to make a program that counts togheter 2 random values. In the program there is supposed to be a list (1-9) in a function. From this list im supposed to get 2 random values (im recommended to use array.splice()). After the 2 random…
-1
votes
2 answers

I want to add an value to an array, trying using splice, but it want work?

Im having problem with getting splice to work for my /home. Im clearly doing something wrong but I cant figure out what, please help? const logArray = [ "|timestamp |url |userid|", "|2019-03-01 09:00:00UTC |/contact.html…
Anna Almestal
  • 39
  • 3
  • 11
-1
votes
1 answer

compare 2 array and remove items

in vue i have an array with this data(TempId): [{"id":47},{"id":45},{"id":48}] and second array with this: multiprices: [ { id: 45, name: "", price: "2600000", }, { id: 46, name: "", …