How to json_encode associative php array to [{x: key1, y: value1}, {x: key2, y: value2}, …]?

0👍

You can use simple foreach loop with json_encode

$r  = [];
foreach($a1 as $k => $v){
  $r[] = ['x' => $a1[$k], 'y' => $a2[$k]];
}
echo json_encode($r);

https://3v4l.org/1pOhV

Leave a comment