Reputation: 499
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"
)
)
Upvotes: 1
Views: 83
Reputation:
Try to use a .each() loop:
myNewArray = [];
indexOfNestedArray = 0;
PensionLimit.each(function(index1, mySingleObject) {
mySingleObject.each(function(index2, mySingleObjectSingleValue) {
myNewArray[index1][index2] = mySingleObjectSingleValue;
});
indexOfNestedArray++;
});
Upvotes: 0
Reputation: 655
I guess you're trying to convert your JSON to a PHP array, as that is not valid JavaScript output.
In order to do this, PHP provides a function called json_decode
:
json_decode($json, true);
When var_dump
ing the result you'll get almost exactly your expected output.
Upvotes: 1