KARTHI SRV KARTHI SRV - 2 months ago 8
Javascript Question

Convert the array of an object into an array

Convert the array of an object to an array.

My array which I created in JavaScript is based on the array.push function. There I got an array in an object, and then I converted it into an array by using JSON.stringify(myarray):

[PensionLimit] =>
[
{"member":1,"pension_name":"1A","min":"N/A","max":"N/A","actual":0,"pension_type":"4","result":"N/A"},
{"member":1,"pension_name":"1B","min":"N/A","max":"N/A","actual":0,"pension_type":"4","result":"N/A"},
{"member":1,"pension_name":"1C","min":"N/A","max":"N/A","actual":0,"pension_type":"4","result":"N/A"},
{"member":2,"pension_name":"2A","min":"N/A","max":"N/A","actual":1,"pension_type":"4","result":"N/A"},
{"member":2,"pension_name":"2B","min":"N/A","max":"N/A","actual":0,"pension_type":"4","result":"N/A"},
{"member":2,"pension_name":"2C","min":"N/A","max":"N/A","actual":2000,"pension_type":"4","result":"N/A"},
{"member":3,"pension_name":"3A","min":"N/A","max":"N/A","actual":0,"pension_type":"4","result":"N/A"},
{"member":4,"pension_name":"4A","min":"N/A","max":"N/A","actual":0,"pension_type":"4","result":"N/A"}
]


How do convert it?

My expected output is:

[PensionLimit] => Array
(
[1] => Array
(
[member] => 1
[pension_name] => "1A"
[min] => "N/A"
[max] => "N/A"
[actual] => 0
[pension_type] => "4"
[result] => "N/A"
)
[2] => Array
(
[member] => 1
[pension_name] => "1B"
[min] => "N/A"
[max] => "N/A"
[actual] => 0
[pension_type] => "4"
[result] => "N/A"
)
[3] => Array
(
[member] => 1
[pension_name] => "1C"
[min] => "N/A"
[max] => "N/A"
[actual] => 0
[pension_type] => "4"
[result] => "N/A"
)
[4] => Array
(
[member] => 1
[pension_name] => "2A"
[min] => "N/A"
[max] => "N/A"
[actual] => 0
[pension_type] => "4"
[result] => "N/A"
)
[5] => Array
(
[member] => 1
[pension_name] => "2B"
[min] => "N/A"
[max] => "N/A"
[actual] => 0
[pension_type] => "4"
[result] => "N/A"
)
[6] => Array
(
[member] => 1
[pension_name] => "3A"
[min] => "N/A"
[max] => "N/A"
[actual] => 0
[pension_type] => "4"
[result] => "N/A"
)
)

Answer

I guess your trying to convert your JSON to a php array, as that is not a valid JavaScript output.

In order to do this, php provides a function called json_decode.

json_decode($json, true);

When var_dumping the result you'll get almost exactly your expected output.