1

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"
            )
    )
7
  • 1
    your "Expected output" is not a Javascript array Commented Apr 19, 2016 at 14:20
  • He also taged the question with php, and I suppose that setup could work in php Commented Apr 19, 2016 at 14:23
  • Expexted output is php while doing form.serialize() i recieve some other data in that type of array Commented Apr 19, 2016 at 14:23
  • 1
    php.net/manual/en/function.json-decode.php ? is this what you are looking for? Commented Apr 19, 2016 at 14:25
  • I cant understand it can u please explain Commented Apr 19, 2016 at 14:29

2 Answers 2

1

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_dumping the result you'll get almost exactly your expected output.

Sign up to request clarification or add additional context in comments.

1 Comment

There's not much I can help you with, when I know nothing about how you implemented the json_decode function.
0

Try to use a .each() loop:

myNewArray = [];
indexOfNestedArray = 0;

PensionLimit.each(function(index1, mySingleObject) {
    mySingleObject.each(function(index2, mySingleObjectSingleValue) {
        myNewArray[index1][index2] = mySingleObjectSingleValue;
    });
    indexOfNestedArray++;
});

5 Comments

Better i expected it in lodash
here you are: lodash.com/docs#forEach synthax is different but way how it works the same. Just change the.each( to .forEach(
sorry it recieve as empty array
myArrayOfObject in this example is your PensionLimit array.. Did you replace it?
var myNewArray = []; indexOfNestedArray = 0; _.forEach(arr,function(index1, mySingleObject) { _.forEach(mySingleObject,function(index2, mySingleObjectSingleValue) { myNewArray[index1][index2] = mySingleObjectSingleValue; }); indexOfNestedArray++; }); console.log(myNewArray);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.