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
1
vote
1 answer

unable to add element to array

Controller public function category_fetching(){ $result=$this->AdminModel->category_fetching(); $arraymaincat=array(); $ifarraymatch=""; $result_count=$result[1]; for($i=0;$i<$result_count;$i++){ …
Anvin
  • 49
  • 8
1
vote
3 answers

using array_push($var); with keys

$dizi = array( "tr" => "Turkey", "uk" => "United Kingdom", "us" => "United States" ); i want to add "br"=>"brasil" to end of the array. thanks.
jankli
  • 41
  • 2
1
vote
1 answer

Array push despite declaration: TS2345: Argument of type 'string' is not assignable to parameter of type 'never'

I have the error mentioned in the title for the line transactionIds: acc.transactionIds.push(currId). My code looks like this: const resultObject: { amountAccumulated: number; amountLeft: number; rate: number | undefined; …
Spurious
  • 1,903
  • 5
  • 27
  • 53
1
vote
1 answer

array_push does not add value in case of used inside of foreach > if

I'm currently working on a website for my project, but for some reason when everything is as it has to be ( session is started, session variable is defined ), then only array_push() function doesn't add the value to session variable, when I move…
Arty
  • 45
  • 6
1
vote
1 answer

Javascript update array value if exists otherwise push new array to object

I am trying to update a value in the array if it is found, if it isn't then add a new array to it. Here's some code I have been trying: var cartItems = {}; var items = [] cartItems.items = items; $('.proAdd').click(function(){ var name =…
Abu Nooh
  • 846
  • 4
  • 12
  • 42
1
vote
1 answer

Unable to use JSON data after pushing into array with Javascript .push Method

I am not able to retrieve the values from an array after storing it in a variable outside of getJSON. My JSON response is: [ { "Book_ID": "1", "Book_Name": "Computer Architecture", "Category": "Computers", …
1
vote
1 answer

How many parameters does array_push() actually expect?

http://php.net/manual/en/function.array-push.php clearly shows the signature of the function with the variadic being completely optional: int array_push ( array &$array [, mixed $... ] ) I think that, when using array_push with splat operator, it…
user719662
1
vote
2 answers

How to convert string into an array

I have an array something like this $arr1 = array( '0' => '674534856|213123213|232313123', '1' => '349578449|782374879|232313123' ); I loop through the arr1 array, for ($x=0; $x < $count; $x++) { $check = explode("|", $arr1[$x]); …
Ivanka
  • 183
  • 1
  • 1
  • 5
1
vote
1 answer

How to merge arrays of different lengths with unique keys and duplicate data

UPDATE: One array is an actual array, the other is an object array. I'm wanting to combine two arrays repeating data where keys match. $arr1 = array( object(stdClass){"id" => 1, "key" => 1, "content" => "blah", "extra" => "extra data"}, …
mezzomix
  • 305
  • 2
  • 12
1
vote
2 answers

How to merge two multidimensional arrays

I have two array: $arr1 = array( 'attributes' => array( 'fruit' => 'banana', ), ); $arr2 = array( 'attributes' => array( 'color' => 'red', ), ); $result = array_merge($arr1, $arr2); The result is: Array (…
Tikky
  • 1,253
  • 2
  • 17
  • 36
1
vote
1 answer

array_push for stdClass with numeric keys

I'm trying to use a function similar to array_push but for objects (stdClass) in PHP. See what I expect: a = 'foo'; $obj->{'0'} = 'far'; var_dump($obj); // > object(stdClass)#1 (2) { ["a"]=> string(3) "foo"…
1
vote
0 answers

PHP array_push putting the same value into the array?

I'm trying to make my own "Database" class for MySQLi, it's the classic class that coomunicate with database, so into my web application I can do (for example) $user = $database->select($connection, "SELECT * FROM user"); So I wrote the function…
Matt Chad
  • 75
  • 6
1
vote
1 answer

PHP 5.2 array_push inserts a null value

Hi I'm encountering a weird problem iterating and pushing a value in an array(for better viewing i use json data). Below is the data i'm using in my foreach for the data below. I actually don't know what the problem is but i'm using php 5.2…
vlad awtsu
  • 185
  • 1
  • 2
  • 14
1
vote
2 answers

How to push JSON Objects and create a nested one in AngularJs

I end up with a frontend task on my desk yesterday and since our frontend dev is on annual leave I need some help. I have this piece of AngularJs script which is part of my angular controller. I am trying to post some data to the server which I…
user2342259
  • 345
  • 2
  • 9
  • 27
1
vote
1 answer

Insert array at desired index into an associate array of arrays

I have a array like below $arr=array( array( 'id'=> 342, 'name' =>'srikanth', 'age' => 32 ), array( 'id'=> 409, 'name' =>'Ashok', 'age' => 24 ), array( 'id'=> 314, …
saikiran
  • 2,247
  • 5
  • 27
  • 42