0

I have 2 nested arrays like this

array:1 [
  0 => array:1 [
    "id" => 6
  ],
  1 => array:1 [
    "id" => 4
  ]
]

array:1 [
  0 => array:1 [
    "id" => 509
  ],
  1 => array:1 [
    "id" => 256
  ]
]

I'm trying to merge this to arrays to have something like this

array:1 [
  0 => array:1 [
    "ssh_id" => 6
    "d_id"   => 509
  ],
  1 => array:1 [
    "ssh_id" => 4
    "d_id"   => 256
  ]
]

I tried array_merge functions from PHP, but I don't get the result I want. Is this possible?

3
  • Both arrays always have equal data or it contains variable data Commented May 6, 2016 at 8:39
  • Both arrays have equal number of datas Commented May 6, 2016 at 8:40
  • 1
    Then the solution given by @Aju John will work Commented May 6, 2016 at 9:10

2 Answers 2

3

To make it simple, I tried with looping:

$arr1 = array(
            array("id"=>6),
            array("id"=>"4")
    );
$arr2 = array(
            array("id"=>509),
            array("id"=>256)
    );

$result = array();
foreach($arr1 as $k=>$a) {
    $result[$k] = array("ssh_id"=>$a['id'], "d_id" => $arr2[$k]['id']);
}
Sign up to request clarification or add additional context in comments.

Comments

2

You can do this by using for loop also.

$final = array();
for($i = 0; $i < count($arr1); $i++){
    $final[] = array("ssh_id" => $arr1[$i]['id'], "d_id" => $arr2[$i]['id']);
}

Result

Array
(
    [0] => Array
        (
            [ssh_id] => 6
            [d_id] => 509
        )

    [1] => Array
        (
            [ssh_id] => 4
            [d_id] => 256
        )

)

Comments

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.