VirtueMart Forum

VirtueMart 2 + 3 + 4 => Virtuemart Development and bug reports => Topic started by: guardiano78 on June 28, 2017, 09:41:49 AM

Title: get value from product_attribute field [SOLVED]
Post by: guardiano78 on June 28, 2017, 09:41:49 AM
Hello,
i need to make a external script that read only the value of product attribute from #__virtuemart_order_items table.

this is my product_attribute string value:

{
    "1": {
        "2": {
            "comment": "AJ4F9F"
        }
    }
}


Well, i need to extract from this string only AJ4F9F.

How can i do this?
thanks!
Title: Re: get value from product_attribute field
Post by: guardiano78 on June 28, 2017, 09:56:44 AM
I tried with this code:

$stringa = '{
    "1": {
        "2": {
            "comment": "AJ4F9F"
        }
    }
}';

preg_match_all('/".*"/', $stringa, $ris);
$a= $ris[0][2];
$data = explode(':', $a);
echo str_replace('"','',$data[1]);

But i don't know if this is the right solution.
Can anyone help me?

Thank you!
Title: Re: get value from product_attribute field
Post by: Ghost on June 28, 2017, 10:05:24 AM
That's probably JSON data. Use json_decode to turn it into object or array.

This works if you just need to echo the comment once:

echo json_decode($stringa,true)[1][2]['comment'];
Title: Re: get value from product_attribute field
Post by: guardiano78 on June 28, 2017, 10:27:56 AM
Thank you for suggestion.
Now i used this code:

$data_attribute = json_decode($product_attribute, true);
$code = $data_attribute ['1']['2']['comment'];

and seem it works.

Thank you so much!