3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = [ [ "id" => 1, "influencer_user_id" => 4, "content_data" => '{"Reach":"300","Views":"320","Views through rate":"350","Shares":"350"}', ], [ "id" => 2, "influencer_user_id" => 4, "content_data" => '{"Reach":"100","Likes":"100","Views":"100"}', ], [ "id" => 3, "influencer_user_id" => 5, "content_data" => '{"Reach":"350"}', ], ]; $result = array(); // first, group the influencer_user_id foreach ($data as $element) { $result[$element['influencer_user_id']][] = $element; } // second, calculate content_data $result = array_map(function($element){ $element = array_reduce($element, function($carry, $item){ // convert to array $content_data = json_decode($item['content_data'], true); // convert content_data to integer values $content_data = array_map('intval', $content_data); $item['content_data'] = $content_data; if(empty($carry)) return $item; // compute content_data sum array_walk($item['content_data'], function(&$value, $key) use ($carry){ if(isset($carry['content_data'][$key])){ $value += $carry['content_data'][$key]; } }); return $item; }); // convert content_data back to json $cdata = $element['content_data']; $element['content_data'] = json_encode($cdata); return $element; }, $result); $result = array_values($result); print_r($result);
Output for git.master, git.master_jit, rfc.property-hooks
Array ( [0] => Array ( [id] => 2 [influencer_user_id] => 4 [content_data] => {"Reach":400,"Likes":100,"Views":420} ) [1] => Array ( [id] => 3 [influencer_user_id] => 5 [content_data] => {"Reach":350} ) )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
33.98 ms | 402 KiB | 8 Q