1

I have 2 array I would like add new row in the first with informtion from the second. My 2 arrray

    $arrayItems = array(
    array(
    "item" => "firstname",
    "value"=>"John"
    ),
    array(
    "item" => "lastname",
    "value"=>"Doe"
    ),
    array(
    "item" => "adress",
    "value"=>"3 garden street"
    ),
     array(
    "item" => "Tel",
    "value"=>"123456"
    )
);

$arrayValue = array(

    array(
    "isAlias" => "false"  
    ),
    array(
    "isAlias" => "false"
    ),
    array(
    "offical" => "true"
    ),
     array(
    "active" => "true"
    )
);

I would like add new row in my fist array with data from the second Like this :

    array (size=4)
  0 => 
    array (size=3)
      'item' => string 'firstname' (length=9)
      'value' => string 'John' (length=4)
      'isAlias' => string 'false' (length=5)
  1 => 
    array (size=3)
      'item' => string 'lastname' (length=8)
      'value' => string 'Doe' (length=3)
      'isAlias' => string 'false' (length=5)
  2 => 
    array (size=3)
      'item' => string 'adress' (length=6)
      'value' => string '3 garden street' (length=15)
      'offical' => string 'true' (length=4)
  3 => 
    array (size=3)
      'item' => string 'Tel' (length=3)
      'value' => string '123456' (length=6)
      'active' => string 'true' (length=4)

Thanks you for your help :-) Thanks you for your help :-)

user3279494
  • 451
  • 1
  • 6
  • 14

5 Answers5

3

If both arrays always have the same length and there are not issues with the order in both arrays or keys existing in arrayItems and arrayValues it's simple:

$result = array();
foreach ($arrayItems as $key => $value) {
  $result[] = $arrayItems[$key] + $arrayValues[$key];
}

If you run into problems (values overwritten, etc.), please edit your question with specifics.

Edit: I fixed an important error in my answer it needs to be $result[].

There are a few small differences between my answer and another answer, and I feel like explaining them:

  • array() and [] are the same. They create an empty array.
  • foreach and for behave the same if the arrays are properly sorted and indexed. It's up to you what you prefer.
  • $result[] and array_push are very similar. One difference is that array_push requires an initialized array, while [] will create it if necessary. Another is that you can user more than 2 parameters for array_push.
  • the + operator and array_merge mainly differ, if there is a key in both arrays. + will keep the first one; merge the last. There a other subtle differences.

Edit 2 Or just use array_map as suggested in another answer. But I'm not deleting this wall of text.

jh1711
  • 2,288
  • 1
  • 12
  • 20
  • +1 for standing up for your wall of text. :) But really, it is a very well explained answer. Just one suggestion - the unofficial site policy is to not label your edits with **Edit**, etc.. You can see what was changed easily enough in the edit history of the post. Relevant meta post: https://meta.stackoverflow.com/questions/255644/should-edit-in-edits-be-discouraged – Don't Panic Dec 01 '17 at 15:49
1

You can map array_merge over the two arrays.

$result = array_map('array_merge', $arrayItems, $arrayValue);
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
0
$i = 0;
foreach($arrayItems as $arrayItem)
{
     array_push($arrayItem[$i]["isAlias"],$arrayValue[$i]["isAlias"]);
    $i++;
}
futureweb
  • 442
  • 4
  • 12
0

You can make use of array_merge() inside a for loop that merges both indexes:

$merge = [];
for ($i = 0; $i < count($arrayItems); $i++) {
  array_push($merge, array_merge($arrayItems[$i], $arrayValue[$i]));
}
var_dump($merge);

Which results in the following:

array (size=4)
  0 => 
    array (size=3)
      'item' => string 'firstname' (length=9)
      'value' => string 'John' (length=4)
      'isAlias' => string 'false' (length=5)
  1 => 
    array (size=3)
      'item' => string 'lastname' (length=8)
      'value' => string 'Doe' (length=3)
      'isAlias' => string 'false' (length=5)
  2 => 
    array (size=3)
      'item' => string 'adress' (length=6)
      'value' => string '3 garden street' (length=15)
      'offical' => string 'true' (length=4)
  3 => 
    array (size=3)
      'item' => string 'Tel' (length=3)
      'value' => string '123456' (length=6)
      'active' => string 'true' (length=4)

This can be seen working here.

Note that this assumes that order of both arrays will always be consistent.

Hope this helps! :)

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
0
reset($arrayItems);
reset($arrayValue);
foreach( $arrayValue as $value ){
 $aKey = key($value);
 //. no size limitation on arrays
 $arrayItems[key($arrayItems)][$aKey] = $value[$aKey];
 next($arrayItems);
}
print_r($arrayItems);
tdjprog
  • 706
  • 6
  • 11