1

is it possible to turn this data (retrieved from sql database) into this data

[{
"DATE": "2016-02-08",
"STORE": "1",
"SALES": "34"
}, {
"DATE": "2016-02-08",
"STORE": "2",
"SALES": "11"
}, {
"DATE": "2016-02-08",
"STORE": "3",
"SALES": "19"
}]

into this

[{
"DATE": "2016-02-08",
"STORE1": "34",
"STORE2": "11",
"STORE3":"19"
}]

either through the sql select or array manipulation?

EDIT

to extend this question further if the array contained multiple days how would i create a multidimensional array like below

Array (
[0] => array (
    "DATE": "2016-02-08",
    "STORE1": "34",
    "STORE2": "11",
    "STORE3":"19"
    )
[1] => array (
    "DATE": "2016-02-08",
    "STORE1": "34",
    "STORE2": "11",
    "STORE3":"19"
)
)
2
  • Yeah, it's possible either through sql depending on your structure or by just using plain PHP afterwards. Commented Mar 7, 2016 at 14:47
  • How is it possible through sql @purpleninja ? thanks Commented Mar 7, 2016 at 15:22

1 Answer 1

3

Array manipulation is simple:

$final = [];
foreach($results AS $result) {
    $final['DATE'] = $result['DATE'];
    $final['STORE' . $result['STORE']] = $result['SALES'];
}

The $final array will have what you want.

Working example: https://3v4l.org/XqWUT

Edit

If you have different dates and need the $final array to be multi-dimensional, here's a new working example link: https://3v4l.org/9ctpU

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

1 Comment

Instead of just one $final create a separate variable for each day, basically. Update your question with details if you want more info.

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.