0

I am trying to convert an array to nested array by its items. My array contains mainly 10 items and they have key and value.Keys have [] brackets..My primary array is:

$data = Array
(
  [4/3/20] => 61
  [4/4/20] => 70
  [4/5/20] => 88
  [4/6/20] => 123
  [4/7/20] => 164
  [4/8/20] => 218
  [4/9/20] => 330
  [4/10/20] => 424
  [4/11/20] => 482
  [4/12/20] => 621
)

My end result is:

Array
(
[0] => Array
    (
        [x] => 
        [y] => 61
    )

[1] => Array
    (
        [x] => 
        [y] => 70
    )
.....

[8] => Array
    (
        [x] => 
        [y] => 482
    )

[9] => Array
    (
        [x] => 
        [y] => 621
    )

)

But x is missing here.. I am using this function

foreach ($data as $key => $value) {
    $arr = array(
            'x' => $Key,
            'y' => $value,
        );
    array_push($dataset, $arr);
}
return $dataset;

I have selected keys from its array. Am i missing something here..??

Tick Twitch
  • 513
  • 10
  • 23

2 Answers2

1

Try this

foreach ($data as $key => $value) {
$arr = array(
        'x' => $key,
        'y' => $value,
    );
array_push($dataset, $arr);
}
 return $dataset;
Prabhash Rawat
  • 441
  • 1
  • 4
  • 15
1

I am not sure about your variable $data and its content, but in the code you are using $Key instead of $key (Capital k).

I hope this solves your issue.