1

Let me describe in detail, I have a json as below :

{
    "client_id": 1234,
    "rawdata": [{
        "task_id1": {
            "apikey1": "1234",
            "flow": "ssp",
            "total_chocolate_request": 43235,
            "start_time": "2017 - 05 - 09 12: 29: 00 UTC",
            "end_time": "2017 - 05 - 09 12: 29: 00 UTC"
        },
        "task_id2": {
            "apikey2": "1235",
            "flow": "mobileweb",
            "total_chocolate_request": 43235,
            "start_time": "2017 - 05 - 09 12: 29: 00 UTC",
            "end_time": "2017 - 05 - 09 12: 29: 00 UTC"
        }
    }]

}

What I want is separate rawdata in json only . There can be n numbers of rawdata data . I need to iterate & segregate based on task id .

I am stuck cannot proceed till now what I wrote :

<?php

require_once 'execute_query.php';    
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $rawdata = file_get_contents("php://input");
    $array=json_decode($rawdata, true);    
}    
var_dump ($array);    
$clientId= $array[client_id];    
echo $clientId;
//print_r(array_keys($array));
?>

So to brief :

Input is :

{
    "client_id": 1234,
    "rawdata": [{
        "task_id1": {
            "apikey1": "1234",
            "flow": "ssp",
            "total_chocolate_request": 43235,
            "start_time": "2017 - 05 - 09 12: 29: 00 UTC",
            "end_time": "2017 - 05 - 09 12: 29: 00 UTC"
        },
        "task_id2": {
            "apikey2": "1235",
            "flow": "mobileweb",
            "total_chocolate_request": 43235,
            "start_time": "2017 - 05 - 09 12: 29: 00 UTC",
            "end_time": "2017 - 05 - 09 12: 29: 00 UTC"
        }
    }]

}

Output : There are two taskids & there respective json are

{
"task_id1": {
    "apikey1": "1234",
    "flow": "ssp",
    "total_chocolate_request": 43235,
    "start_time": "2017 - 05 - 09 12: 29: 00 UTC",
    "end_time": "2017 - 05 - 09 12: 29: 00 UTC"
}

and

{

"task_id2": {
                "apikey2": "1235",
                "flow": "mobileweb",
                "total_chocolate_request": 43235,
                "start_time": "2017 - 05 - 09 12: 29: 00 UTC",
                "end_time": "2017 - 05 - 09 12: 29: 00 UTC"
            }
4
  • Can you show what your input looks like and what your output should look like? Commented Jun 8, 2017 at 11:36
  • Put it your question, you can't format stuff properly in comments. Commented Jun 8, 2017 at 11:38
  • It's kinda hard to understand what you want. https://3v4l.org/A9juq. Each task in part of an array, so $array['rawdata'][0]['task_id1']. Commented Jun 8, 2017 at 11:40
  • I just decribed my Input & output Commented Jun 8, 2017 at 11:43

1 Answer 1

1

This is real easy. JSON decode into an array. Grab what you need. You can json_encode() back if you need to.

$array = json_decode($json, true);

$rawDatas = $array['rawdata'][0];

foreach($rawDatas as $key =>$data) {
    echo $key."\n......\n";
    var_dump($data);
    echo "............\n";
}

See it in action here: https://3v4l.org/bF9mt

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

1 Comment

nice one, glad i could help! Can you mark this answer as the correct one? :-D

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.