0

Here is my PHP json output

[ {      
        "campaign_id": "4",   
        "date": "2017-04-01",   
        "name": "Dealoyal",   
        "iteration": "5149"   
    },   
....   
]

I want to make it to look like this:

[{   
    "date": "2017-04-01",   
    "Dealoyal": "5149"   
},...]  

My code:

foreach ($result as $row) {
    $name[] = $row['name'];
    $iteration[] = $row['iteration'];
}
$mine[]=array_combine($name, $iteration);
echo json_encode($mine,JSON_PRETTY_PRINT);

But this only prints the last day names and iterations , when I need all days that I choose at date range. Do I need to use for loop or maybe there is another way?

1
  • Could you explain me what's wrong? Commented Dec 13, 2017 at 16:07

1 Answer 1

1

You don't need to use array_combine(), you can directly create $mine in the foreach:

$mine = array();
foreach ($result as $row) {
    $mine[] = array(
        'date' => $row['date'],
        $row['name'] => $row['iteration'],
    );
}
echo json_encode($mine, JSON_PRETTY_PRINT);
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.