1
Array
 (
[0] => Array
    (
        [id] => 1
        [influencer_user_id] => 4
        [content_data] => {"Reach":"300","Views":"320","Views through rate":"350","Shares":"350"}
    )

[1] => Array
    (
        [id] => 2
        [influencer_user_id] => 4
        [content_data] => {"Reach":"100","Likes":"100","Views":"100"}
    )

[2] => Array
    (
        [id] => 3
        [influencer_user_id] => 5
        [content_data] => {"Reach":"350"}
    )

)


foreach($influencer_contents as $row){
      $influencer_id = $row['influencer_user_id'];
}

if influencer_user_id is same inside the loop I need to sum the content_data. given example there are 3 array values influencer_user_id has to 4, I need to merge first two arrays( has same values) and need to make array count 2.

I tried like I created temporary array and I pushed the same influencer_id to temp_array like below but that is not working

if(!in_array($influencer_id, $array_temp)){
          $array_temp[] = $influencer_id;  }

i need output like this below,

Array
 (

[0] => Array
    (
        [id] => 2
        [influencer_user_id] => 4
        [content_data] => {"Reach":"400","Likes":"100","Views":"100"}
    )

[1] => Array
    (
        [id] => 3
        [influencer_user_id] => 5
        [content_data] => {"Reach":"350"}
    )

  )

2 Answers 2

3

Is this the solution you're looking?

<?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);

The output to this is

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}
        )

)

Here is the link to my solution: https://3v4l.org/1aETf

Sign up to request clarification or add additional context in comments.

1 Comment

seriously awesome!. Jim.B thank you so much it's working fine.
0

Data:

<?php

$data = 
array
(
    '0' => array
        (
            'id' => 1,
            'influencer_user_id' => 4,
            'content_data' => '{"Reach":"300","Views":"320","Views through rate":"350","Shares":"350"}'
        ),
    '1' => array
        (
            'id' => 2,
            'influencer_user_id' => 4,
            'content_data' => '{"Reach":"100","Likes":"100","Views":"100"}'
        ),
    '2' => array
        (
            'id' => 3,
            'influencer_user_id' => 5,
            'content_data' => '{"Reach":"350"}'
        )
);

Method:

// Group influencer data
foreach($data as $item) {
    $item['content_data'] = json_decode($item['content_data'], true);
    $result[$item['influencer_user_id']][] = $item;
}

// Merge influencer data, and sum respective content_data
foreach($result as &$item) {
    $item = array_merge_recursive(...$item);
    // Use last occuring ids.
    foreach(['id', 'influencer_user_id'] as $key)
        if(is_array($item[$key]))
            $item[$key] = array_pop($item[$key]);
    // Sum content_data keys
    foreach($item['content_data'] as &$v)
        if(is_array($v))
            $v = (string) array_sum($v);
    unset($v);
    $item['content_data'] = json_encode($item['content_data']);
}
unset($item);

var_export($result);

Output:

array (
    4 => 
    array (
      'id' => 2,
      'influencer_user_id' => 4,
      'content_data' => '{"Reach":"400","Views":"420","Views through rate":"350","Shares":"350","Likes":"100"}',
    ),
    5 => 
    array (
      'id' => 3,
      'influencer_user_id' => 5,
      'content_data' => '{"Reach":"350"}',
    ),
  )

This method leans on array_merge_recursive, example:

var_export(array_merge_recursive(
    ['bert' => ['paperclips' => 10]],
    ['bert' => ['paperclips' => 5, 'duckys' => 2]]
));

Output:

array (
    'bert' => 
    array (
      'paperclips' => 
      array (
        0 => 10,
        1 => 5,
      ),
      'duckys' => 2,
    ),
  )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.