0

I have to post a form element inventory like following structure

[inventory] => Array
(
    [0] => Array
        (
            [inventory_id] => Array
                (
                    [0] => 1
                )

            [inventory_name] => Array
                (
                    [0] => Bed 90*200
                )

            [inventory_photo] => Array
                (
                    [0] => 1_bed_90x200.jpg
                )

        )

    [1] => Array
        (
            [inventory_id] => Array
                (
                    [0] => 15
                )

            [inventory_name] => Array
                (
                    [0] => Bed 90*200
                )

            [inventory_photo] => Array
                (
                    [0] => 1_bed_90x200.jpg
                )

        )

    [2] => Array
        (
            [inventory_id] => Array
                (
                    [0] => 15
                )

            [inventory_name] => Array
                (
                    [0] => Bed 90*200
                )

            [inventory_photo] => Array
                (
                    [0] => 1_bed_90x200.jpg
                )

        )

)

When I tried to assign this array to inventory in $client->setParameterPost(), I received POST values like this

[inventory] => Array
        (
            [0] => Array
                (
                    [inventory_id] => Array
                        (
                            [0] => 1
                        )

                )

            [1] => Array
                (
                    [inventory_name] => Array
                        (
                            [0] => Bed 90*200
                        )

                )

            [2] => Array
                (
                    [inventory_photo] => Array
                        (
                            [0] => 1_bed_90x200.jpg
                        )

                )

            [3] => Array
                (
                    [inventory_id] => Array
                        (
                            [0] => 15
                        )

                )

            [4] => Array
                (
                    [inventory_name] => Array
                        (
                            [0] => Bed 90*200
                        )

                )

            [5] => Array
                (
                    [inventory_photo] => Array
                        (
                            [0] => 1_bed_90x200.jpg
                        )

                )

            [6] => Array
                (
                    [inventory_id] => Array
                        (
                            [0] => 15
                        )

                )

            [7] => Array
                (
                    [inventory_name] => Array
                        (
                            [0] => Bed 90*200
                        )

                )

            [8] => Array
                (
                    [inventory_photo] => Array
                        (
                            [0] => 1_bed_90x200.jpg
                        )

                )

        )

)

I have verified my array structure that is fine. I also checked in setParameter method in Client.php (Zend library), no issues. Just receiving this post. How can I achieve this?

1
  • The problem is with Zend_Http_Client::_flattenParametersArray(). It discards the keys of the array while flattening them. Haven't figured the solution yet Commented Feb 13, 2014 at 9:42

1 Answer 1

1

Zend_Http_Client ignores integer keys of multi dimensional parameters. So in your case,

[inventory] => Array
(
    [0] => Array
    (
        [inventory_id] => Array
        (
            [0] => 1
        )

        [inventory_name] => Array
        (
            [0] => Bed 90*200
        )

        [inventory_photo] => Array
        (
            [0] => 1_bed_90x200.jpg
        )

    )

will be translated to

inventory[][inventory_id][] => 1
inventory[][inventory_name][] => Bed 90*200
inventory[][inventory_photo][] => 1_bed_90x200.jpg

The solution to this problem is

  • either to use a class extended from Zend_Http_Client and override its method _flattenParametersArray().
  • or convert the params arrays to strings yourself, such as:

    $client->setParameterPost('inventory[0][inventory_id][]', 1);
    $client->setParameterPost('inventory[0][inventory_name][]', 'Bed 90*200');
    $client->setParameterPost('inventory[0][inventory_photo][]', '1_bed_90x200.jpg');
    
Sign up to request clarification or add additional context in comments.

Comments

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.