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

PHP Arrays: How to add 'key & value' to an existing array

I do not know how to add a key and value to the existing array. My array goes like this. Initially I have tried adding using array_push() but it added not as I needed it. I have given my output after I gave the 'var_dump'. array (size=5) 0 => …
Deepak Keynes
  • 2,291
  • 5
  • 27
  • 56
6
votes
4 answers

How to count unique value from object of array in javascript

I want to calculate number of unique value and put that in new array. I have below array: [ { CategoryId: "b5c3f43f941c", CategoryName: "Category 1", CategoryColor: "cgreen" } { CategoryId: "9872cce5af92", CategoryName: "Category 2",…
rohit13807
  • 605
  • 8
  • 19
5
votes
3 answers

push() method not working properly in JavaScript

I'm trying to write a quite simple program that divides an array in another array of defined size smaller arrays, however the push() method is not working. Could someone please help me with it? function chunk(array, size) { var newArray = []; …
5
votes
3 answers

how to use array_push for json_encode

I am iOS developer and I am making Webservices in PHP for getting JSON Response. Code which I wrote is: $result = mysqli_query($con,"SELECT * FROM wp_marketcatagories"); $data =array(); while($row = mysqli_fetch_array($result)) …
Usama Sadiq
  • 587
  • 1
  • 4
  • 21
5
votes
2 answers

If else shorthand inside array .push()

Why I can do if else shorthand inside .push() function ? like var arr = []; arr.push(test||null); // nothing But var arr = []; var test = test||null; arr.push(test); // [null] I need to insert null if variable is undefined. Why I cant use…
l2aelba
  • 21,591
  • 22
  • 102
  • 138
4
votes
2 answers

dynamically inserting value to array with index

Pushing values to an array caused the index to start with 0 if the index were any other values except starting from 0. $a=array("a"=>"Dog","b"=>"Cat"); array_push($a,"Horse","Bird"); this will insert Horse and Bird with index 0 and 1. Can I insert…
bunkdeath
  • 2,348
  • 5
  • 21
  • 23
4
votes
4 answers

Array made by array.push() has elements but couldn't use angular

I have 2 Array. One of them has initial elements and another one has its elements added by array.push() in ngOnInit. In the end, both have the elements in output but html doesn't render the elements which were pushed with .push //the result…
Hama Bohel
  • 95
  • 2
  • 10
4
votes
2 answers

Php : Laravel - Array push after using eloquent pluck method

here is my Query $RecipientList = Employees::select(DB::Raw('CONCAT(first_name," ",last_name) as employee_name'),'email')->pluck('employee_name','email'); which give me the proper result as i want, but after i execute query i have 1 more key =>…
Punit Gajjar
  • 4,937
  • 7
  • 35
  • 70
4
votes
2 answers

Array push not working in IE5 using javascript?

When I try to load the select options into the optStored array using the array push, the array always remains empty. In chrome, firefox, and safari the code is working correctly but not in Internet Explorer. Is there a work around for this or…
ur345
  • 41
  • 4
4
votes
2 answers

PHP Adding new key to the array used in foreach

How can I add to the array that I'm using foreach on? for instance: $t =array('item'); $c = 1; foreach ($t as $item) { echo '--> '.$item.$c; if ($c < 10) { array_push($t,'anotheritem'); } } this seems to produce only one value…
NEW2WEB
  • 503
  • 2
  • 8
  • 22
4
votes
2 answers

PHP array_push error

My code is as below, $products = array(); for($i=0; $i < sizeof($sales); $i++){ if(!in_array($sales[$i]['Product']['product'], (array)$products)){ $products = array_push((array)$products, $sales[$i]['Product']['product']); } …
Irawana
  • 269
  • 3
  • 6
  • 14
4
votes
3 answers

add key and value to an existing array

I need to add key and value to an existing array and don't seem to be able to get it together. My existing array when printed looks like this: Array ( [0] => stdClass Object ( [id] => 787 [name] => Steve …
Cybercampbell
  • 2,486
  • 11
  • 48
  • 75
4
votes
3 answers

How to declare an array as global in PHP?

I need to write the contents of an array to a file, each time the page is loaded... I've created the array in index.php and pushing contents to the array in another ajax page.. But i couldn't access the array globally.. its showing an error as…
Deepzz
  • 4,573
  • 1
  • 28
  • 52
3
votes
3 answers

PHP arrays. inserting "$key" => "$value" pair into array with array_push();

Why won't this work? $slidetotal=1; $slideids = array(); while ($rowcs = mysql_fetch_array($orig_slides_result)) { $key = $slidetotal; array_push($slideids[$key], $rowcs['id']); $slidetotal++; } I get this error: [phpBB…
Patrick
  • 241
  • 2
  • 8
  • 18
3
votes
3 answers

PHP array_push() is overwriting existing array elements

I'm trying to push objects into an array to end up with an object array like this: [ {"recipient_name":"John D", "phone_number":"123456"}, {"recipient_name":"Doe J", "phone_number":"654321"}, {"recipient_name":"Jon Do",…
Clint_A
  • 518
  • 2
  • 11
  • 35
1
2
3
25 26