0

I'm new at PHP so forgive me if this question has been asked. How do you combine two PHP arrays into one? This is the code I'm using that needs to be combined:

<?php $duplicates[] = get_the_ID(); ?>
<?php $images = get_attached_media( 'image', $post->ID );?>

Thanks for the help.

Gregory Schultz
  • 864
  • 7
  • 24

3 Answers3

1

If all you need to do is merge two arrays into one, this should do the trick:

$new_array = array_merge($duplicates, $images);
Andrej
  • 74
  • 3
0

Array Combine Procedure

Code:

<?php
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));
?> 

Output:

Array ( [0] => red [1] => green [2] => blue [3] => yellow ) 
Naresh Kumar P
  • 4,127
  • 2
  • 16
  • 33
  • In as per your request you have to do like this: `$resultant_array = array_merge($duplicates,$images)`. This will result in an single array as per my example. :) – Naresh Kumar P Aug 25 '16 at 07:13
0

PHP function array_combine creates an array by using the values from the keys array as keys and the values from the values array as the corresponding values.

Mariella
  • 107
  • 1
  • 7