Diego Rangel
Diego Rangel

Reputation: 13

Merge arrays made in a loop PHP

I have a array that, 3 repeats from the same week, but 3 differents users that have your totals separately:

array
  0 => 
    array
      'week' => '1'
      'user' => 'Oswaldo Aranha'
      'totals' => 'value'
  1 => 
    array
      'week' => '1'
      'user' => 'Protogenes'
      'totals' => 'value'
  2 => 
    array
      'week' => '1'
      'user' => 'Rego Barros'
      'totals' => 'value'
  3 => 
    array
      'week' => '2'
      'user' => 'Oswaldo Aranha'
      'totals' => 'value'
  4 => 
    array
      'week' => '2'
      'user' => 'Protogenes'
      'totals' => 'value'
  5 => 
    array
      'week' => '2'
      'user' => 'Rego Barros'
      'totals' => 'value'
  ...    

I wanna to unify the week in one array, with the 3 users and your totals. Like this:

array 
  0 => 
  array 
    'week' => '1'
    'Oswaldo Aranha' => 'value'
    'Protogenes' => 'value'
    'Rego Barros' => 'value'
  1 => 
  array 
    'week' => '2'
    'Oswaldo Aranha' => 'value'
    'Protogenes' => 'value'
    'Rego Barros' => 'value'

  2 => 
  array 
    'week' => '3'
    'Oswaldo Aranha' => 'value'
    'Protogenes' => 'value'
    'Rego Barros' => 'value'
  ...

I'm trying using array_merge(), array_combine(), array_whatever() but doesn' work. How i do that?

Upvotes: 1

Views: 64

Answers (3)

Matthias Leuffen
Matthias Leuffen

Reputation: 144

I think there is no php-function doing it for you. Just do it your own:

$in = $array;
$out = [];
foreach ($in as $curIn) {
    if ( ! isset ($out[$curIn["week"]])) {
        $out[$curIn["week"]] = [];
    }
    $out[$curIn["week"]][$curIn["user"]] = $curIn["totals"];
}

That's it.

Upvotes: 0

Evadecaptcha
Evadecaptcha

Reputation: 1441

$array = [
    [
        'week' => '1',
        'user' => 'Oswaldo Aranha',
        'totals' => 'value'
    ],
    [
        'week' => '1',
        'user' => 'Protogenes',
        'totals' => 'value'
    ],
    [
        'week' => '1',
        'user' => 'Rego Barros',
        'totals' => 'value'
    ],
    [
        'week' => '2',
        'user' => 'Oswaldo Aranha',
        'totals' => 'value',
    ],
    [
        'week' => '2',
        'user' => 'Protogenes',
        'totals' => 'value'
    ]
];
function combineWeeks($array) {
    $results = [];
    for($i = 0;$i < count($array);$i++) {
        $results[$array[$i]['week']][$array[$i]['user']] = $array[$i]['totals'];
        $results[$array[$i]['week']]['week'] = $array[$i]['week'];
    }
    $combined = [];
    foreach($results as $key => $value) {
        $combined[] = $results[$key];
    }
    return $combined;
}

Usage:

$combinedArray = combineWeeks($array);
print_r($combinedArray);

output:

Array
(
    [0] => Array
        (
            [Oswaldo Aranha] => value
            [week] => 1
            [Protogenes] => value
            [Rego Barros] => value
        )

    [1] => Array
        (
            [Oswaldo Aranha] => value
            [week] => 2
            [Protogenes] => value
        )

)

Upvotes: 0

Mohammad Salem
Mohammad Salem

Reputation: 1143

you can use array_merge();

$arr=array_merge ( $array[0],$array[1] ,$array[3]);

i hope this will help you :)

for extra info this link will help you :

http://php.net/manual/en/function.array-merge.php

Upvotes: 1

Related Questions