PHP Loop If same key then merge value

How to merge value on same key while looping, my array :

Array
(
    [0] => Array
        (
            [key] => company-group
            [val] => index
        )

    [1] => Array
        (
            [key] => company-group
            [val] => view
        )

    [2] => Array
        (
            [key] => company-group
            [val] => create
        )

    [3] => Array
        (
            [key] => company-group
            [val] => update
        )

    [4] => Array
        (
            [key] => company-group
            [val] => delete
        )

    [5] => Array
        (
            [key] => company-industry
            [val] => index
        )

    [6] => Array
        (
            [key] => company-industry
            [val] => view
        )

    [7] => Array
        (
            [key] => company-industry
            [val] => create
        )

    [8] => Array
        (
            [key] => company-industry
            [val] => update
        )

    [9] => Array
        (
            [key] => company-industry
            [val] => delete
        )
)

My desire result

Array
(
    [0] => Array
        (
            [key] => company-group
            [val] => Array (
                        [0] => index,
                        [1] => create,
                        [2] => update,
                        [3] => delete,
            )
        )

    [1] => Array
        (
            [key] => company-industry
            [val] => Array (
                        [0] => index,
                        [1] => create,
                        [2] => update,
                        [3] => delete,
            )
        )
)

Thanks for advice !

I’d think a foreach() through the main loop to build into the new loop would be the easiest way, though there may be an array_something function to do it.

$newarr = array();

foreach($oldarr as $ar => $el) {
	$found = -1;
        // does the key already exist? Thought I could use array_search(), but no
	for ($count = 0; $count < count($newarr); $count++) { 
	    if ($newarr[$count]['key'] == $el['key']) { 
		    $found = $count;
		    break;
		}
	}
	if ($found != -1) { 
          // already have a main entry, so just add to the val array
	  $newarr[$found]['val'][] = $el['val'];
	}
	else {
		// there *must* be a better way to do this bit in one line
		$newarr[]['key'] = $el['key'];
		$latest = count($newarr)-1;
		$newarr[$latest]['val'][] = $el['val'];
		}
}

var_dump($newarr);

I can’t quite see what the syntax would be to create the new sub-array with a single ‘key’ and an array of ‘val’ elements, there must be a nicer way than the way I’ve done it there. Likewise I thought I could use array_search() to see if the key already exists, but it seems to not work, possibly because it’s an array within an array. Maybe someone with more array knowledge will comment.

$new = [];
foreach ($original as $elem) {
  $new[$elem['key']]['key'] = $elem['key'];
  $new[$elem['key']]['val'][] = $elem['val'];
}

$new = array_values($new);

See https://repl.it/repls/NoxiousBitterNumber :slight_smile:

2 Likes

See, I thought there might be a quicker way. :slight_smile:

http://php.net/array-merge-recursive

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.