0

I have below array,

Array ( [0] => Array ( [user_name] => Jack1 [amount] => 100.00 [category_name] => Trial1 ) [1] => Array ( [user_name] => Jack1 [amount] => 150.00 [category_name] => Trial2 ) [2] => Array ( [user_name] => Jack1 [amount] => 200.00 [category_name] => Trial1 ) [3] => Array ( [user_name] => Jack2 [amount] => 200.00 [category_name] => Trial2 ) [4] => Array ( [user_name] => Jack2 [amount] => 200.00 [category_name] => Trial1 ) [5] => Array ( [user_name] => Jack2 [amount] => 200.00 [category_name] => Trial2 ) 

What i want to send to have JSON with below format

It will get some of Equal category name and then send it as json.

[{'user_name': Jack1, 'trial1':"300", 'trial2':150"" }, {'user_name': Jack2, 'trial1':"200", 'trial2':400"" }]

In summary, i want to username as unique and then put all category with name and sum of each category for that user,

Tried below,

$new_array = array();

        foreach ($expense_array['x'] as $a)
        {
            if (!isset($new_array[$a['user_name']]['amount']))
            {
                $new_array[$a['user_name']]['amount'] = 0;
            }

            $new_array[$a['user_name']] = array(
                    'user_name' => $a['user_name'],
                    'category_name' => $a['category_name'],
                    'amount' => $new_array[$a['user_name']]['amount'] + $a['amount']);

        }
echo json_encode(array_values($new_array));

This only output trai1 category, not as required JSON

How can i achieve this? Was thinking to get foreach loop and then make compare of category_name and use .=+ to get sum? but i lost there,

Thanks,

6
  • please format this . and put some valid json data Commented Nov 26, 2016 at 5:32
  • Are you getting this data from the database? Commented Nov 26, 2016 at 5:34
  • @peterm, yes i am getting array output from DB, and want to create PHP loop to output JSON Commented Nov 26, 2016 at 5:34
  • Then it's better / more natural do grouping on the db side. Commented Nov 26, 2016 at 5:37
  • What you have tried so far. Post your attempts too. SO is not free code writing service Commented Nov 26, 2016 at 5:40

1 Answer 1

1

If you pass the array into this function it will return the json string that you want:

function create_json($data)
{
    $output = [];

    foreach ( $data as $stats )
    {
        $key      = $stats['user_name'];
        $category = strtolower($stats['category_name']);
        $amount = $stats['amount'];

        if ( isset($output[$key]) )
        {
            if ( isset($output[$key][$category]) )
            {
                $output[$key][$category] += $amount;
            }
            else
            {
                $output[$key][$category] = $amount;
            }
        }
        else
        {
            $output[$key] = [
                'user_name' => $key,
                $category   => $amount,
            ];
        }
    }

    return json_encode(array_values($output));
}

Output:

[
{"user_name":"Jack1","trial1":300,"trial2":150},
{"user_name":"Jack2","trial2":400,"trial1":200}
]
Sign up to request clarification or add additional context in comments.

5 Comments

Sane, almost there, but how can i get sum of all category amount for Jack1 , 2?
@rjcode, So you want it to output the sum of each category and the total sum of all categories? if so then change you example JSON to reflect that and I'll make it match.
@rjcode, if you look at the example output from my post you will see that it matches you example, so I'm still confused on what is missing? The category's totals are small because I used the dataset array you provided
i don't want total categories, but amount total where each Trial 1 and 2 for Jack1 and 2
@rjcode, I understand now, I've changed the function to reflect what I think you want.

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.