0

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?

4
  • What is it - in-line? Commented Oct 31, 2016 at 21:21
  • @u_mulder - as in can i unserialise without breaking the array apart in a loop Commented Oct 31, 2016 at 21:23
  • $arr[key] = unserialize($arr[key]);? Commented Oct 31, 2016 at 21:25
  • No. you don't work with the serialized string. If you do something wrong, you've corrupted the ENTIRE string. you unserialize, manipulate the data structure you get from that, then re-serialize. Commented Oct 31, 2016 at 21:37

1 Answer 1

1

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] => 
                                )

                        )

                )

        )

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

1 Comment

yes this worked, although i had to tweak it a bit as there was more than one entry, but that method worked a charm :) thanks.

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.