4

I'm thinking how to group my array by objects with the same value.

I have this result from MySQL query:

   Date           StartTime           EndTime
2014-12-01          08:00              12:00
2014-12-01          10:00              16:00
2014-12-02          12:00              18:00
2014-12-03          10:00              20:00

I have this data in a PHP variable called $Data.

Is it possible to get the json array like this with php: ?

[
    {
        "2014-12-01": [
            {
                "StartTime": "08:00",
                "EndTime": "12:00"
            },
            {
                "StartTime": "10:00",
                "EndTime": "16:00"
            }
        ]
    },
    {
        "2014-12-02": [
            {
                "StartTime": "12:00",
                "EndTime": "18:00"
            }
        ]
    },
    {
        "2014-12-03": [
            {
                "StartTime": "10:00",
                "EndTime": "20:00"
            }
        ]
    }
]

If I use echo json_encode($Data), the result is:

[
    {
        "Date": "2014-12-01",
        "StartTime": "10:00",
        "EndTime": "16:00"
    },
    {
        "Date": "2014-12-02",
        "StartTime": "12:00",
        "EndTime": "18:00"
    },
    {
        "Date": "2014-12-03",
        "StartTime": "10:00",
        "EndTime": "20:00"
    }
]
6
  • Sure is possible. What MySQL API are you using in your PHP code? For most you would have to build the array while fetching, but PDO even offers a FETCH_GROUP option to fetchAll() which would produce this structure (more or less). Commented Dec 6, 2014 at 3:10
  • I have the result from MySQL in a php variable called $Data. Commented Dec 6, 2014 at 3:11
  • Okay but what are the actual PHP contents of $Data? Please show a limited sample of it via print_r($Data); and also post the PHP code you used to fetch rows from MySQL into $Data. Commented Dec 6, 2014 at 3:13
  • The grouped output as you have posted your desired structure is kind of strange - an outer array of objects, each containing one property (date) that is an array of objects. It would make more sense to have an outer object with keys (dates) each an array of objects. 2 fewer levels of nesting. Commented Dec 6, 2014 at 3:24
  • Like {"2014-12-01": [{"StartTime": "10:00", "EndTime":"16:00"},{"StartTime": "10:00","EndTime": "16:00"}],"2014-12-02":[{"StartTime":"12:00","EndTime":"18:00"}] Commented Dec 6, 2014 at 3:26

2 Answers 2

6

Based on the structure outlined in the question:

Requires php 5.4+ for the json pretty printing, if you are on a lower version just remove it and use long array format.

$in = <<<'JSON'
[
    {
        "Date": "2014-12-01",
        "StartTime": "10:00",
        "EndTime": "16:00"
    },
    {
        "Date": "2014-12-02",
        "StartTime": "12:00",
        "EndTime": "18:00"
    },
    {
        "Date": "2014-12-03",
        "StartTime": "10:00",
        "EndTime": "20:00"
    },
    {
        "Date": "2014-12-03",
        "StartTime": "12:00",
        "EndTime": "20:00"
    }
]
JSON;

$data = json_decode($in, true);
$out = [];

foreach($data as $element) {
        $out[$element['Date']][] = ['StartTime' => $element['StartTime'], 'EndTime' => $element['EndTime']];
}

var_dump(json_encode($out, JSON_PRETTY_PRINT));

To get the exact same output as in the question (the returned output wrapped in a single element json-array you'd need to wrap $out in an other array like this:

json_encode([$out], JSON_PRETTY_PRINT)

Results in:

{
  "2014-12-01": [
    {
      "StartTime": "10:00",
      "EndTime": "16:00"
    }
  ],
  "2014-12-02": [
    {
      "StartTime": "12:00",
      "EndTime": "18:00"
    }
  ],
  "2014-12-03": [
    {
      "StartTime": "10:00",
      "EndTime": "20:00"
    },
    {
      "StartTime": "12:00",
      "EndTime": "20:00"
    }
  ]
}
Sign up to request clarification or add additional context in comments.

Comments

1

EMPLOYER NAME DATA_PATH Alp 1. video_link 2. video_link Xyz 1. video_link 2. video_link 3. video_link

$stmt = $this->conn->prepare("SELECT a.employer_name,b.data_path FROM 
rec_experience a INNER JOIN rec_multi_upload_data b 
ON(a.rec_uniqueid=b.rec_id) WHERE b.data_type='Video'  ORDER BY 
a.c_date DESC ");
$stmt->execute();
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);

$out = [];

foreach($res as $element)
{
 $out[$element['employer_name']][] = ['video_link' => 
 $element['data_path']];
}
echo json_encode($out);

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.