0

Say I got the following JSON:

{
users: [
{
  qnt: "3",
  post: "j8v2g5",
  contact: "[email protected]",
  id: "1"
 },
 {
  qnt: "10",
  post: "xxxyyy",
  contact: "[email protected]",
  id: "6"
 },
 {
  qnt: "3",
  post: "xxxyyy",
  contact: "[email protected]",
  id: "4"
 }
]
}

I want all children of users with the same post to have their qnt added together and remove contact and id from the final result.

I want the result to look like this:

{
users: [
 {
  qnt: "3",
  post: "j8v2g5"
 },
 {
  qnt: "13",
  post: "xxxyyy"
 }
]
}
6
  • What have you tried? If you are stuck somewhere we'll be happy to help, but you can't expect us to do all the work for you! Unless I can send you an invoice that is ;-) Commented Apr 1, 2015 at 22:52
  • Convert it to array by json_decode() and then something like array_walk to build up an array. If post is in_array, then add qnt to the qnt. Then encode the result via json_encode(). Commented Apr 1, 2015 at 22:55
  • @Pevara Thank you for your help, I don't know how I would do it without you :) Seriously now, if you can't and don't even consider helping - don't bother commenting. Commented Apr 1, 2015 at 22:56
  • Excuse me? I was just trying to tell you in a friendly way that you should at least show some effort. SO is for solving concrete programming problems, not a free 'write my code' service! Perhaps you should read the terms first stackoverflow.com/help/on-topic Commented Apr 1, 2015 at 23:01
  • @Pevara I don't know how to do this, okay? For a very long time I avoided doing this because I didn't know how to do this but now, doing a workaround would make it extremely hard to work with. Commented Apr 1, 2015 at 23:03

1 Answer 1

2

You will probabely be recieveing $myJson from somewhere and then you can return $rsultsJson. I suggest putting this whole thing in a function or a class method.

$myJson = '{"users": [{  "qnt": "3",  "post": "j8v2g5",  "contact": "[email protected]",  "id": "1" }, {  "qnt": "10",  "post": "xxxyyy",  "contact": "[email protected]",  "id": "6" }, {  "qnt": "3",  "post": "xxxyyy",  "contact": "[email protected]",  "id": "4" }]}';
$usersObject=json_decode($myJson);
$usersArray = $usersObject->users;
$postCount = array();

foreach ($usersArray as $user) {
    if (array_key_exists($user->post, $postCount)) {
        $postCount[$user->post] += $user->qnt;
    } else {
        $postCount[$user->post] = $user->qnt;
    }   

}

$results = new stdClass();
$results->users = array();
foreach ($postCount as $post => $count) {
    $user = new stdClass();
    $user->qnt = $count;
    $user->post = $post;    
    $results->users[] = $user;
}

$rsultsJson = json_encode($results);
Sign up to request clarification or add additional context in comments.

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.