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

Merge array into established array at random indexes in PHP

I'm building an image gallery and want to throw some promo banners in at random points to promote certain offers to users. Given the following two arrays have been filtered from a database query: Media images array: Array ( [0] => Array …
James
  • 81
  • 1
  • 10
0
votes
1 answer

Filter and Sort a multidimensional array in PHP

I'm building an image gallery and want to throw some promo banners in at random points to promote certain offers to users. Given the following array comes from a database query: Array ( [0] => Array ( …
0
votes
0 answers

Splice array on click - javascript

I have a type of newsfeed wall like Facebook were you can create posts. I want when I click on the glyphicon in my post to remove that certain post. The code is: function loadPosts(){ $('#allPosts').empty(); for (var i = 0; i <…
SmalliSax
  • 342
  • 3
  • 6
  • 24
0
votes
1 answer

Can't remove element from scope array AngularJS

I've got a couple views via ui-router and have a scope that I'm storing some input values in. The user can either enter information in one view and go to the next step or they can skip that step (and remove any entered data in the scope) and go to…
0
votes
2 answers

How to splice a string at second position and add it to an array?

In the following code, displayChar() gets the value as a,b,c,d,e,f. But I want it as ab,cd,ef. How can I do that? void displayString(char* s) { for (int i = 0; i<=strlen(s); i++) { displayChar(s[i]); } } void loop() { …
0
votes
1 answer

Remove element of array without splice()

I'm developing a JavaScript game and I want to keep my memory usage as low as possible. Therefore I set some objects to null again, so they can get garbage collected. I read an article which recommends avoiding functions like Array.splice(), because…
0
votes
1 answer

TypeError: Error #1010: A term is undefined and has no properties. Problems with successfully removing objects

I am trying to make a game involving a dragon burning little knights, fairly simple but I am new to AS3 and can't seem to solve this problem, sometimes when I kill a knight it returns an output error saying: TypeError: Error #1010: A term is…
0
votes
2 answers

PHP deleting $i from an array using unset. I have a hole in my array and can't fix it

This piece of my code deletes the item from the array using the value of $i. $i=0; when it starts, and when $i=2 is deleted, the value that replaces it can't be deleted again. I know it's because I have a hole in my array using the unset…
user3150191
  • 345
  • 3
  • 6
  • 17
0
votes
1 answer

php multidimensional array - value minus 1

I am having trouble with my multidimensional array. I have various items in the array. The name and quantity of each item are displayed on the screen, with a - and + button to alter the quantity of each item by 1. Each button is a form posting back…
metaBaron
  • 39
  • 9
0
votes
1 answer

splice deletes from an array and also from its parent array - strange issue

Hi I have this simple script in my code, var c = [{a: 'apple'},{b: 'ball'},{c: 'ball'},{d: 'ball'}]; console.log(c); //prints 3 objects var d = c; d.splice(0, 3); console.log(c); //prints 1 object console.log(d); //prints 1 object…
Luckyy
  • 1,021
  • 4
  • 15
  • 29
0
votes
1 answer

Remove an object from array with objects and functions

I have an object array: [ { name: 'one', value: '1' }, { name: 'two', value: '22' }, { name: 'three', value: '333' }, add: [Function], delete: [Function] ] How can I delete an object with name: 'two'? [ { name:…
Edward Ruchevits
  • 6,411
  • 12
  • 51
  • 86
0
votes
8 answers

Algorithm to search through and delete repeats in array

I have an array with some elements being "repeats", and I want to delete repeats in the array. So for example, the list (array) on the left turns into the array on the right: Ingredients: Ingredients: Apples …
LazerSharks
  • 3,089
  • 4
  • 42
  • 67
0
votes
2 answers

PHP - get_file_contents not working as an array?

I'm using a $file_contents = file_get_contents($file_name) then using $file_contents = array_splice($file_contents, 30, 7, 'changedText') to update something in the file code. However, this keeps resulting in: Warning: array_splice(): The first…
golmschenk
  • 11,736
  • 20
  • 78
  • 137
0
votes
1 answer

get removed and original array using splice and for loop

I'm trying to create multiple arrays from one array and then add html strings. I use .splice() and for. My main problem is that .splice() only shows the removed arrays and not the first. Here's what I have so far: array = ["a1", "a2", "a3", "b1",…
koen
  • 1
  • 2
0
votes
1 answer

Non well formed numeric value formed when using array_splice

I am using array_slice in PHP like so: if (isset($_GET['page']) && !isset($no_pagination)) { $page_num = mysql_real_escape_string($_GET['page']); $limit_value_from = $page_num * 10; $limit_value_to = $limit_value_from + 10; …
Arun
  • 626
  • 10
  • 29