0

I need to create the following array:

$data = array(
   "invoice_info" => array(
       ...
    ),
    "product_info => array(
        array(
           "name" => "product 1",
           ...
        ),
        array(
           "name => "product 2",
            ...
        )
    )
);

I am currently struggling to get the products into my "product_info" array. For each product I want to add I use the following line:

$product_info[] = array("name" => "product 1", ...);

This works fine for the first one. Adding another one, however, overwrites it. The second problem is, that keys are added, that should not be there:

[product_info] => Array
    (
        [0] => Array
            (
                [name] => product 2
                [qty] => 293
                [price] => 44
                [sum] => 12892
            )

    )

This is the output after adding 2 products. Just the second one is included. What am I doing wrong?

3
  • The value "name" is the a key (always the same key ) so without a proper index you can't append the same array .. each time you add name=> array your overwrite the previous Commented Jun 1, 2019 at 16:27
  • Where does $product_info come from? It's $data['product_info']. Commented Jun 1, 2019 at 16:31
  • Please show your actual code, it's impossible to tell what you're doing wrong from that one line. Commented Jun 1, 2019 at 16:32

1 Answer 1

1

This will generate you an array of arrays

$data = array(
    "invoice_info" => array(),
    "product_info" => array(),
    );

$data['product_info'][] = ['name' => 'prod1','qty'=>1, 'price'=> 44, 'sum'=>100];
$data['product_info'][] = ['name' => 'prod2','qty'=>22, 'price'=> 55, 'sum'=>200];

print_r($data);

RESULT

Array
(
    [invoice_info] => Array
        (
        )

    [product_info] => Array
        (
            [0] => Array
                (
                    [name] => prod1
                    [qty] => 1
                    [price] => 44
                    [sum] => 100
                )

            [1] => Array
                (
                    [name] => prod2
                    [qty] => 22
                    [price] => 55
                    [sum] => 200
                )

        )

)

Now if you had done something like this

$product_info =& $data['product_info'];

to create your $product_info variable then note that you must use the =& or when you attempt to allocate a new array it will not work as you expect. So, adding that to the previous code would look like this

$data = array(
    "invoice_info" => array(),
    "product_info" => array(),
    );

$data['product_info'][] = ['name' => 'prod1','qty'=>1, 'price'=> 44, 'sum'=>100];
$data['product_info'][] = ['name' => 'prod2','qty'=>22, 'price'=> 55, 'sum'=>200];


$product_info =& $data['product_info'];

$product_info[] = ['name' => 'prod3','qty'=>99, 'price'=> 155, 'sum'=>300];

print_r($data);

Will produce the result

Array
(
    [invoice_info] => Array
        (
        )
    [product_info] => Array
        (
            [0] => Array
                (
                    [name] => prod1
                    [qty] => 1
                    [price] => 44
                    [sum] => 100
                )
            [1] => Array
                (
                    [name] => prod2
                    [qty] => 22
                    [price] => 55
                    [sum] => 200
                )
            [2] => Array
                (
                    [name] => prod3
                    [qty] => 99
                    [price] => 155
                    [sum] => 300
                )
        )
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.