0

I want to be able to join by commas values that have the same key.

I have this code:

Array
    (
        [0] => Array
            (
                [user123] => 50877214
            )
    
        [1] => Array
            (
                [user123] => 7776057
            )
    
        [2] => Array
            (
                [user456] => 53445145
            )
    
        [3] => Array
            (
                [user456] => 19487054
            )
    )

I would like to convert it to:

Array
    (
        [0] => Array
            (
                [user123] => "50877214,7776057",
            )

        [2] => Array
            (
                [user456] => "53445145,19487054",
            )
    )

My code:

$arr  = array_map(null, ...$arr);
$temp = array_map(function ($item) {
    return implode(",", $item);
}, $arr);
print_r($temp);die;

Result:

Array ( [0] => 50877214,7776057,53445145,19487054 )

apparently it is not possible to group by the key...

spawng
  • 11
  • 5

4 Answers4

1

Also array_reduce can be used here:

$res = array_reduce($arr, function ($acc, $el) {
    $key = array_key_first($el);
    $acc[$key] .= ($acc[$key] ? ',':'') . $el[$key];
    return $acc;
}, $acc);
    
var_export($res);

Execute PHP online

Result:

array (
  'user123' => '50877214,7776057',
  'user456' => '53445145,19487054',
)
Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39
0

Start by creating an associative array whose values are arrays. Then you can convert that into the desired result by creating nested arrays and using implode() to join with commas.

$assoc = [];
foreach ($array as $nested) {
    foreach ($nested as $key => $value) {
        if (isset($assoc[$key])) {
            $assoc[$key][] = $value;
        } else {
            $assoc[$key] = [$value];
        }
    }
}

$result = [];
foreach ($assoc as $key => $value) {
    $result[] = [$key => implode(',', $value)];
}

I actually recommend stopping at the first step. An array of arrays with different keys is generally difficult to work with (as you've already discovered).

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

A bit late, though I thought this could still be helpful.

$data = [
    ["user123" => 50877214],
    ["user123" => 7776057],
    ["user456" => 53445145],
    ["user456" => 19487054]
];

$keys = [];
$result = [];

array_walk_recursive($data, function ($value, $key) use (&$keys, &$result) {
    if (!in_array($key, $keys)) {
        $keys[] = $key;
        $result[array_search($key, $keys)] = [$key => ""];
    }
    $result[array_search($key, $keys)][$key] .= $value. ",";
});

print_r($result);

/*
Array
(
    [0] => Array
        (
            [user123] => 50877214,7776057,
        )

    [1] => Array
        (
            [user456] => 53445145,19487054,
        )

)
 */

Addendum

Otherwise, if you just want the result as a "flattened" array, the solution below could do the trick.

$data = [
    ["user123" => 50877214],
    ["user123" => 7776057],
    ["user456" => 53445145],
    ["user456" => 19487054]
]; 

$keys = [];
$result = [];

array_walk_recursive($data, function ($value, $key) use (&$keys, &$result) {
    if (!in_array($key, $keys)) {
        $keys[] = $key;
        $result[$key] = "";
    }
    $result[$key] .= $value. ",";
});

print_r($result);

/*
Array
(
    [user123] => 50877214,7776057,
    [user456] => 53445145,19487054,
)
 */
steven7mwesigwa
  • 5,701
  • 3
  • 20
  • 34
0

Loop through inner-arrays with foreach and another loop to iterate over key-value pairs, $key which is user id (e.g. user123). check if the $key in $arr which first time will not be there, So store its value, Second time will be there So accumulate its values by separator commas,...

$array = [
    ["user123" => 50877214],
    ["user123" => 7776057],
    ["user456" => 53445145],
    ["user456" => 19487054]
];

$arr = [];
foreach ($array as $inner) {
    foreach ($inner as $key => $value) {
        if(array_key_exists($key, $arr)){
            $arr[$key] .= ','.$value;
        }else{
            $arr[$key] = $value;
        }
    }
}
echo '<pre>';
print_r($arr);

Prints:

Array
(
    [user123] => 50877214,7776057
    [user456] => 53445145,19487054
)

Nevertheless, If you want the array be indexed, Just do another variable $res, loop through $arr and store its key-value pairs to $res.

$res = [];
foreach ($arr as $key => $value) {
    $res[] = [$key => $value];
}
print_r($res);

Output:

Array
(
    [0] => Array
        (
            [user123] => 50877214,7776057
        )

    [1] => Array
        (
            [user456] => 53445145,19487054
        )

)
XMehdi01
  • 5,538
  • 2
  • 10
  • 34