James Simpson
James Simpson

Reputation: 197

PHP Unserialise Within an Array

I have an array that has the following values

[0] => array(4) {
["sku"] => string(12) "WMS-M-VN-MRN"
["name"] => string(62) "Maroon V-neck Jumper"
["qty_ordered"] => string(6) "1.0000"
["product_options"] => string(533) "a:2:{s:15:"info_buyRequest";a:6:{s:4:"uenc";s:64:"aHR0cDovL2VtYmxlbWF0aWMuY28udWsvd21zLW0tdm4tbXJuLz9fX19TSUQ9VQ,,";s:7:"product";s:3:"780";s:8:"form_key";s:16:"gDXvCEtQOlRWihqc";s:15:"related_product";s:0:"";s:7:"options";a:1:{i:1970;s:5:"17201";}s:3:"qty";s:1:"1";}s:7:"options";a:1:{i:0;a:7:{s:5:"label";s:27:"PLEASE SELECT SIZE REQUIRED";s:5:"value";s:12:"36 inch (13)";s:11:"print_value";s:12:"36 inch (13)";s:9:"option_id";s:4:"1970";s:11:"option_type";s:9:"drop_down";s:12:"option_value";s:5:"17201";s:11:"custom_view";b:0;}}}"
}

Now is there a way to unserialise the "Product_options" field in-line, without having to break the array apart and rebuild it again?

Upvotes: 0

Views: 81

Answers (1)

nanocv
nanocv

Reputation: 2229

This is the way. You just have to reassign the result of unserialize to the same array item.

<?php

$array = [
    [
        "sku" => "WMS-M-VN-MRN",
        "name" => "Maroon V-neck Jumper",
        "qty_ordered" => "1.0000",
        "product_options" => 'a:2:{s:15:"info_buyRequest";a:6:{s:4:"uenc";s:64:"aHR0cDovL2VtYmxlbWF0aWMuY28udWsvd21zLW0tdm4tbXJuLz9fX19TSUQ9VQ,,";s:7:"product";s:3:"780";s:8:"form_key";s:16:"gDXvCEtQOlRWihqc";s:15:"related_product";s:0:"";s:7:"options";a:1:{i:1970;s:5:"17201";}s:3:"qty";s:1:"1";}s:7:"options";a:1:{i:0;a:7:{s:5:"label";s:27:"PLEASE SELECT SIZE REQUIRED";s:5:"value";s:12:"36 inch (13)";s:11:"print_value";s:12:"36 inch (13)";s:9:"option_id";s:4:"1970";s:11:"option_type";s:9:"drop_down";s:12:"option_value";s:5:"17201";s:11:"custom_view";b:0;}}}'
    ]
];

$array[0]["product_options"] = unserialize($array[0]["product_options"]);

echo "<pre>";
print_r($array);
echo "</pre>";

Output:

Array
(
    [0] => Array
        (
            [sku] => WMS-M-VN-MRN
            [name] => Maroon V-neck Jumper
            [qty_ordered] => 1.0000
            [product_options] => Array
                (
                    [info_buyRequest] => Array
                        (
                            [uenc] => aHR0cDovL2VtYmxlbWF0aWMuY28udWsvd21zLW0tdm4tbXJuLz9fX19TSUQ9VQ,,
                            [product] => 780
                            [form_key] => gDXvCEtQOlRWihqc
                            [related_product] => 
                            [options] => Array
                                (
                                    [1970] => 17201
                                )

                            [qty] => 1
                        )

                    [options] => Array
                        (
                            [0] => Array
                                (
                                    [label] => PLEASE SELECT SIZE REQUIRED
                                    [value] => 36 inch (13)
                                    [print_value] => 36 inch (13)
                                    [option_id] => 1970
                                    [option_type] => drop_down
                                    [option_value] => 17201
                                    [custom_view] => 
                                )

                        )

                )

        )

)

Upvotes: 1

Related Questions