-2

how to convert such an array to JSON ? the array is obtained after parsing excel table , and some fields are empty ,

      0 => array:10 [▼
    0 => "number"
    1 => null
    2 => "274"
    3 => null
    4 => null
    5 => null
    6 => null
    7 => null
    8 => null
    9 => null
  ]



 1 => array:10 [▼
    0 => "step"
    1 => null
    2 => "4"
    3 => null
    4 => null
    5 => null
    6 => null
    7 => null
    8 => null
    9 => null
  ]

  19 => array:10 [▼
0 => "details"
1 => null
2 => null
3 => null
4 => null
5 => null
6 => null
7 => null
8 => null
9 => null

]

to become like this : i tried to remove all null values but that made it worse and harder to deal with .

{"number":"274","step":"4" , "details": null }

1 Answers1

1

In your original array, all the keys are in element 0 and the values are in element 2. You can combine them with:

$new_array = array_combine(array_column($array, 0), array_column($array, 2));
echo json_encode($new_array);

DEMO

Barmar
  • 741,623
  • 53
  • 500
  • 612