Sparkx
Sparkx

Reputation: 59

Append all values (except the first) from a flat array as a new column of a 2d array

These are my two arrays:

$test = array(
    "0" => array(
        "mem_id" => "299", 
        "profilenam" => "Guys&Dolls", 
        "photo_b_thumb" => "photos/935a89f58ef2f3c7aaaf294cb1461d64bth.jpeg"
    ), 
    "1" => array(
        "mem_id" => "344", 
        "profilenam" => "Dmitry", 
        "photo_b_thumb" => "no")
    );

$distance = array(
    "0" => "0", 
    "1" => "3.362", 
    "2" => "0.23"
);

I want to combine them as:

Array
(
    [0] => Array
        (
            [mem_id] => 299
            [profilenam] => Guys&Dolls
            [photo_b_thumb] => photos/935a89f58ef2f3c7aaaf294cb1461d64bth.jpeg
            [distance] => 3.362
         )

    [1] => Array
        (
            [mem_id] => 344
            [profilenam] => Dmitry
            [photo_b_thumb] => no
            [distance] => 0.23
        )

)

I tried the code below but it did not work:

foreach ($test as $key => $value) {
    $merged = array_merge((array) $value, $distance);
}
print_r($merged);

Upvotes: 0

Views: 133

Answers (6)

mickmackusa
mickmackusa

Reputation: 48069

All previous answers seem to have overlooked the fact that the first distance value is to be ignored while mapping the two arrays together. Use array_slice() to omit the first element, then call array_map() to synchronously traverse the two payloads and append the distance column to each row of the test array. Demo

var_export(
    array_map(
        fn($row, $d) => $row + ['distance' => $d],
        $test,
        array_slice($distance, 1)
    )
);

Output:

array (
  0 => 
  array (
    'mem_id' => '299',
    'profilenam' => 'Guys&Dolls',
    'photo_b_thumb' => 'photos/935a89f58ef2f3c7aaaf294cb1461d64bth.jpeg',
    'distance' => '3.362',
  ),
  1 => 
  array (
    'mem_id' => '344',
    'profilenam' => 'Dmitry',
    'photo_b_thumb' => 'no',
    'distance' => '0.23',
  ),
)

Upvotes: 0

n00dle
n00dle

Reputation: 6053

I think array_merge_recursive does what you need.

EDIT: It does not. :) However, a derivate of it, posted in the array_map_recursive man page does seem to, see this codepad. I'd be interested to know which is faster over a large dataset.

Upvotes: 0

roselan
roselan

Reputation: 3775

foreach ($test as &$value)
{
   $value['distance'] = array_shift($distance);
}

Upvotes: -1

Kasheftin
Kasheftin

Reputation: 9533

foreach ($test as $key => &$value) {
  $value["distance"] = $distance[$key];
}

Upvotes: 1

Wesley
Wesley

Reputation: 2200

$test = array("0" => array("mem_id" => "299", "profilenam" => "Guys&Dolls", "photo_b_thumb" => "photos/935a89f58ef2f3c7aaaf294cb1461d64bth.jpeg"
    ), "1" => array("mem_id" => "344", "profilenam" => "Dmitry", "photo_b_thumb" => "no"));

$distance = array("0" => "0", "1" => "3.362", "2" => "0.23");

foreach( $test as $id => $data ) {
    $test[$id]['distance'] = $distance[$id];
}

Something like this should work!

Upvotes: 1

Brendan Bullen
Brendan Bullen

Reputation: 11819

<?php

    foreach($test as $index=>$array)
    {
         $test[$index]['distance'] = $distance[$index]
    }

    print_r($test);
?>

Upvotes: 2

Related Questions