Questions tagged [array-push]

array-push refers to the javascript Array.prototype.push() method. The push() method adds one or more elements to the end of an array and returns the new length of the array.

refers to the javascript Array.prototype.push() method.

The push() method adds one or more elements to the end of an array and returns the new length of that array. developer

Example

var a = []; //or new Array()
var i = a.push('foo', 'bar');
console.log(i, a); 

the console will display the values

2 ["foo", "bar"]

The counterpart to push() is pop(), which removes the last item from an array.

Documentation

382 questions
2
votes
1 answer

How to add an ARRAY to an associative array as key value pair

This is my associative array. $family = array("Mike"=>array("Ben","klatty","Paul"),"Johnson"=>array("Shena", "Glenn")); $family['shrinidhi']=array("akshata","pandu"); this works fine. I would like to push an array into $family…
2
votes
1 answer

push into every element of an multidimentional array

Sorry for being novice. i have a multidimensional array as under array(3){ [0] = array(2){ [type]=>car, [model]=> mazda } [1]= array(2){ [type]=>car, [model]=> lexus } [3]= array(2){ …
2
votes
3 answers

php Post arrays in Session array

Im trying to put multiple posts into one session. Heres the line of code that i use to do it. $_SESSION['answers'][] = array_push($_SESSION['answers'], $_POST); As i do so, the following array comes out once i try to add the second array to the…
SliQz
  • 298
  • 1
  • 4
  • 17
2
votes
2 answers

How to prevent duplicate keys from array_push

I have 2 2D arrays, how can i get the unique keys and only push those? For example: $array = json_decode('[{"7654321":1368356071},{"1234567":1368356071}]',true); $array2 = array(array(1234567 => time()), array(7654321 => time()), array(2345678 =>…
GameDevGuru
  • 1,095
  • 2
  • 12
  • 27
2
votes
3 answers

Jquery push function

I'm working on samsung SDK and modifying a javascript app for smart tv. I stumbled upon this piece of code and I don't understand what if($('.menuButton').push()){... does. The native push function was not overwrited but this still works. I thought…
Mircea Voivod
  • 721
  • 1
  • 10
  • 20
2
votes
3 answers

array_push creates new arrays instead of adding

I would like to add a key=>value pair to an existing array depending on an if statement. But it keeps adding the key=>value pair as a new index. Here is my code: foreach ($messageRepo as $oneMessage) { $calculatedTime =…
Pascal Cloverfield
  • 561
  • 1
  • 6
  • 20
2
votes
2 answers

jquery array push not working properly inside ajax success

I am trying to push a div id into an array.Array push is working good bofore ajax call.But when i use push inside ajax success first push is taken place when i click on the second element.that is array operation when with below code( array push…
jack
  • 473
  • 5
  • 21
2
votes
2 answers

Array replacing last value, not adding [PHP]

I have a PHP array, set up using $this->cart = array(); Which is all ind and dandy except the array will not, no matter what i do, take new values, but only replaces the existing values with the new ones. I've tried array_merge, array_push and…
alpha1
  • 157
  • 8
2
votes
5 answers

php magic method to catch array_push

I am looking for a way to intercept the action in array_push, because when it will be retrieve it each value of the array has another info like: class ClassName { var $test = array(); function __set($attr, $value) { $this->$attr =…
user115561
  • 3,631
  • 2
  • 14
  • 8
1
vote
3 answers

Insert element into array with specific position in loop

I want to create list of years in array like this: array ( '2011' => '2011', '2010' => '2010', '2009' => '2009', ... ... '1905' => '1905' ) I try to create this array with loop method like this: $years = array(); for($i=2011;$i>1904;$i--){ …
user1276509
  • 69
  • 1
  • 1
  • 4
1
vote
1 answer

PHP Array_Push multi dimensional array

$m = new mysql(); $players=$m->get('pugs','Players','PID=6'); $players = unserialize($players); $players = array($players); array_push($players[0]['T1'],'test'); This code above works but i want to to work like this ( because i dont want an…
twizzle
  • 11
  • 2
1
vote
2 answers

PHP array_push index retention

I am looping through an array which itself contains array to find indexes of values 5 & 6. Upon finding these indexes, I push the matched array, using array_push, into a another array. My application depends on maintaining the array indexes but…
sisko
  • 9,604
  • 20
  • 67
  • 139
1
vote
2 answers

array_push add value to an existing key

I have the following array called $zoo Array ( [0] => &dog& [1] => *1* [2] => one [3] => &cat& [4] => *2* [5] => two [6] => &mouse& [7] => *3* [8] => three [9] => &dog& [10] => *4* [11] => four ) and i need the following result: Array ( [&dog&] =>…
Preys
  • 103
  • 7
1
vote
6 answers

Separating odd numbers in a string with '-'

I am attempting to take a string of numbers and iterate through that number. When there are two odd numbers adjacent to one another, I want to separate them with '-'. For example, 999463 would become 9-9-9463. const str = "999463" const…
Richard B.
  • 13
  • 3
1
vote
1 answer

JS - How to populate an array with objects from another greater object

I am traying to populate an array "array1" with objects with format {a:'value', b:'value'} that come from a greater objet "objPractice", (could be a json). I don't know what I am doing wrong but I get an array with objects all equals corresponding…