简体   繁体   中英

Posting multidimensional array as a value in Zend_Http_Client in Zend Framework 2

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?

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'); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM