0

I am trying to set the values of an empty array with another array (based on rows returned from a database).

I have tried a few things like array_merge but I just end up adding to the first array.

Just curious if there is a way to do this or would I need to iterate thought each array and merge them?

The second array can have between 1 and 3 arrays in it, while the first (the "empty") array has 3 elements always.

Empty array

(
    [0] => Array
        (
            [id] => 
            [quote_id] => 
            ...
        )

    [1] => Array
        (
            [id] => 
            [quote_id] => 
            ...
        )

    [2] => Array
        (
            [id] => 
            [quote_id] => 
            ...
        )

)

Array I want to copy data from

Array
(
    [0] => Array
        (
            [id] => 1
            [quote_id] => 1
            ...
        )

    [1] => Array
        (
            [id] => 2
            [quote_id] => 1
            ...
        )

) 

What I want to achieve

Array
(
    [0] => Array
        (
            [id] => 1
            [quote_id] => 1
            ...
        )

    [1] => Array
        (
            [id] => 2
            [quote_id] => 1
            ...
        )

      [2] => Array
        (
            [id] => 
            [quote_id] => 
            ...
        )

) 
0

4 Answers 4

2

You can make use of the array union operatorDocs:

$combined = $rows + $empty;

The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for explaining something I was curious about.
0

You could use array_replace(), but you'll have to be very careful with the keys, see the example in the PHP manual.

Comments

0

You can try

$empty = array(
"0" => Array("id" => null,"quote_id" => null,"position" => null,"type" => null,"number" => null,"cost" => null,"total" => null),
"1" => Array("id" => null,"quote_id" => null,"position" => null,"type" => null,"number" => null,"cost" => null,"total" => null),
"2" => Array("id" => null,"quote_id" => null,"position" => null,"type" => null,"number" => null,"cost" => null,"total" => null))

;

$content = Array(
"0" => Array("id" => 1,"quote_id" => 1,"position" => 1,"type" => "dwdwdw","number" => 22,"cost" => 33.00,"total" => 726.00),
"1" => Array("id" => 2,"quote_id" => 1,"position" => 2,"type" => "dwdw","number" => 22,"cost" => 22.00,"total" => 484.00));



var_dump(array_merge($content,array_unique($empty)));

Output

array
  0 => 
    array
      'id' => int 1
      'quote_id' => int 1
      'position' => int 1
      'type' => string 'dwdwdw' (length=6)
      'number' => int 22
      'cost' => float 33
      'total' => float 726
  1 => 
    array
      'id' => int 2
      'quote_id' => int 1
      'position' => int 2
      'type' => string 'dwdw' (length=4)
      'number' => int 22
      'cost' => float 22
      'total' => float 484
  2 => 
    array
      'id' => null
      'quote_id' => null
      'position' => null
      'type' => null
      'number' => null
      'cost' => null
      'total' => null

Comments

0

Try this,

<?php
 $result=mysql_query($query);
 $i=0;
 while($row=mysql_fetch_assoc($result)){
 $youArray[$i]['id']=$row['id'];
 $youArray[$i]['quote_id']=$row['quote_id'];
 //remaining values
 $i++;
 }
?>

1 Comment

Thanks for this, I had done somethign similar using a foreach loop

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.