PHP Pushing variables to array1 then push it to array2 in a loop

array push php
php array push with key
php add to associative array
php push array into array
php add array to array
php array add key value pair to existing array
php associative array push
php foreach

I am having trouble to get the target result of an array.

What I am trying to do is:

  • push every variables in $inner_data in every loop.
  • then push that $inner_data to $details_data to create an array inside an array

I hope I explain my idea and problem.

Code

$details_data = array();
$inner_data = array();

for($i=0; $i < $count_selected; $i++){
    $amount[$i] = $quantity[$i] * $price[$i];
    array_push($inner_data, $last_insert_id, $amount[$i], $quantity[$i], $products[$i]);
    array_push($details_data, $inner_data);
}
print_r($details_data); exit;

Target result

$data = array(
        array(
                'last_insert_id' => 10,
                'amount' => 20000,
                'quantity' => 1,
                'product_id' => 1
        ),
        array(
                'last_insert_id' => 10,
                'amount' => 1000,
                'quantity' => 1,
                'product_id' => 2
        )
);

Unexpected Result

Array
(
    [0] => Array
        (
            [0] => 10
            [1] => 20000
            [2] => 1
            [3] => 1
        )

    [1] => Array
        (
            [0] => 10
            [1] => 20000
            [2] => 1
            [3] => 1
            [4] => 10
            [5] => 10000
            [6] => 1
            [7] => 2
        )

)
for($i=0; $i < $count_selected; $i++){
    $amount[$i] = $quantity[$i] * $price[$i];

    $inner_data=array(
      'last_insert_id'=>$last_insert_id, 
      'amount'=>$amount[$i], 
      'quantity'=>$quantity[$i], 
      'product_id'=>$products[$i]
    );
    array_push($details_data, $inner_data);
}

PHP: array_push - Manual, array_push() treats array as a stack, and pushes the passed variables onto the If you're adding multiple values to an array in a loop, it's faster to use array_push than php -r '$a = array(1,2); $b = array(2=>3,3=>4);$c = $a + $b; print_r($c);' Php pushing values to a 2-dimensional array. i've a 2-dimensional array and i want to push values to it with a while loop like; then push it to the "outer"

Link for your reference

<?php

    $details_data = array();

    /* do not set it as a global vari OR clear it when you re-use it */
    //$inner_data = array();

    $last_insert_id = 10;
    $amount = [20000, 1000];
    $quantity = [110, 220];
    $price = [32, 64];
    $products = [001, 002];

    for($i=0; $i < 2; $i++){
        $amount[$i] = $quantity[$i] * $price[$i];

        /* set it here as a local vari, thus it won't accumulate the result */
        $inner_data = array();

        //array_push($inner_data, $last_insert_id, $amount[$i], $quantity[$i], $products[$i]);
        $inner_data['last_insert_id'] = $last_insert_id;
        $inner_data['amount'] = $amount[$i];
        $inner_data['quantity'] = $quantity[$i];
        $inner_data['product_id'] = $products[$i];
        array_push($details_data, $inner_data);
    }

    echo "<pre>";
    print_r($details_data); exit;

    echo "</pre>";

?>

array_fill_keys - Manual, $value ) : array. Fills an array with the value of the value parameter, using the values of the keys array as keys. print_r(array_fill_keys($array1, $array2)); ?> PHP: Push one or more elements onto the end of array. The array_push() function is used to add one or more elements onto the end of an array. The length of array increases by the number of variables pushed.

array_push($details_data, 
array('last_insert_id'=>$last_insert_id, 'amount'=>$amount[$i], 'quantity'=>$quantity[$i], 'product_id'=>$products[$i])
);

Try to push an array directly to details_data.

array_walk - Manual, The array parameter's value being the first, and the key/index second. We can make that with this simple FOREACH loop : This will cause passing modified parameter to next iteration of array_walk(). This is because array_walk() does not take the third parameter by reference.. so what if we pass the reference as the​  The array_push() function inserts one or more elements to the end of an array. Tip: You can add one value, or as many as you like. Note: Even if your array has string keys, your added elements will always have numeric keys (See example below).

Adding Data to Arrays, JavaScript Loops, Arrays and Objects · What is an Array? 4:51 · Create an Array Add items to the end of an array with .push(). var items = ['Hat', 'Ball', 'Shoes'];  For clarification: I want to be able to pass multiple variables into a function, do something, then return multiple variables back out while keeping them seperate. This was just an example I was trying to get working as a work around for not being able to return multiple variables from an array

PHP append one array to another, It contains well written, well thought and well explained computer science and How to take user input for two dimensional (2D) array in PHP ? Use for each loop to print all the array elements. Using array_push Method: This method pushes the second array element in the first array in-place. $arr1 = array (1, 2);. This returns an infinite loop however and I can't figure out a way to move the while loop onto the next step? EDIT I'm using a while loop because the function have_block will set-up a global variable for the current block id. This will then be used within a function called the_element. Such as:

The List of the 10 Most Common Mistakes That PHP Developers , Common Mistake #1: Leaving dangling array references after foreach loops $​array[1] (i.e., “2”) into $value (which is a reference to $array[2] ), so $array[2] now variables and arrays are passed by value; objects are passed by reference) and To demonstrate, let's take a look at a test box with limited resources (​512MB  I'm working with PHP 5.6 and I want to push values from an array in the end of another array so I tried the array_push function but It pushed the whole array like this: Array ( [0] =&gt; Array

Comments
  • damn you're fast but yeah @Emjey23 that's pretty much it.
  • forgot to type in a ctrl+k to format the code at first. would have beat the other guy haha
  • Yes, hes damn fast. Hes answer was correct. waiting a mins to accept. Thanks
  • Thanks for the tips and explanation. Really appreciated it.