How add new arrray Row in multidimensional Array PHP

318
December 01, 2017, at 12:11 PM

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 :-)

Answer 1

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.

Answer 2

You can map array_merge over the two arrays.

$result = array_map('array_merge', $arrayItems, $arrayValue);
Answer 3
$i = 0;
foreach($arrayItems as $arrayItem)
{
     array_push($arrayItem[$i]["isAlias"],$arrayValue[$i]["isAlias"]);
    $i++;
}
Answer 4

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! :)

Answer 5
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);
READ ALSO
How to move Wordpress admin files to wp-uploads to use as Email attachment?

How to move Wordpress admin files to wp-uploads to use as Email attachment?

I am working with WeForms to create an event registration formThe plugin automatically generates a PDF-File, which can be downloaded in the backend area (under /wp-admin/admin-post

191
javascript to connect to local RestfulApi fail

javascript to connect to local RestfulApi fail

So i'm try to create an social webSo in my html i have this scrip

224
having trouble with session_start()

having trouble with session_start()

I have a login page, home page and a nav page that I include in both of themThe nav shows an admin tab if the session variable admin is a 1

142
Remove the display of the customer invoice address from Prestashop 1.6

Remove the display of the customer invoice address from Prestashop 1.6

I need to remove the customer invoice address information from my Prestashop (16) site

187