-1

I have a simple Two array

    $ages[] = array("Peter"=>22, "Clark"=>32, "John"=>28);

    $ages1[] = array("demo"=>22);

When I print this arrays it should be like following:

Array
(
    [0] => Array
        (
            [Peter] => 22
            [Clark] => 32
            [John] => 28
        )

)
Array
(
    [0] => Array
        (
            [demo] => 22
        )

)

But I want to create third array which will be show demo kye value into first array like following:

Array
(
    [0] => Array
        (
            [Peter] => 22
            [Clark] => 32
            [John] => 28
            [demo] => 22
        )

)

Can we do two array into single array in PHP like Above

Shahzad Intersoft
  • 762
  • 2
  • 14
  • 35
  • What have you tried? Show us your attempts, we will help debug them as required. – Nic3500 Oct 12 '17 at 13:59
  • Duplicate: https://stackoverflow.com/questions/5394157/whats-the-difference-between-array-merge-and-array-array – Nic3500 Oct 12 '17 at 14:00

2 Answers2

2

Not sure what are you trying to achieve here...little more context would be helpful. But this is how you can do this,

$ages[] = array("Peter"=>22, "Clark"=>32, "John"=>28);

$ages1[] = array("demo"=>22);

$result[] = array_merge($ages[0],$ages1[0]);
Himan
  • 379
  • 1
  • 7
0

This would do the job.

<?php    
$ages[] = array("Peter"=>22, "Clark"=>32, "John"=>28);
$ages1[] = array("demo"=>22);
$output = prepend_array($ages,$ages1);
print_r($output);

// Function to prepend arrays
function prepend_array()
{
    $num_args = count(func_get_args());
    $new_array = array();
    foreach (func_get_args() as $params){
        foreach($params as $out_key => $param)
        {
            foreach($param as $key => $value)
            $new_array[$out_key][$key] = $value;
        }
    }

    return $new_array;

}
Waqas Yousaf
  • 64
  • 1
  • 3
  • 10