0

So I got this array with associative object in PHP and I couldnot figure out how to get specific element here is an array:

extra_fields => [
                {"id":"1","value":"1055"},
                {"id":"2","value":"Link"},
                {"id":"3","value":"Name"}
                ]

I tried like this but it doesn't work extra_fields[0]["value"]) and extra_fields[0]->value

Please help. UPDATE: full output code:

    stdClass Object
    (
        [id] => 723
        [title] => XXXXXXX
        [alias] => XXXXXXX
        [catid] => 50
        [published] => 1
        [introtext] =>  
        [fulltext] => 
        [video] => 
        [gallery] => 
        [extra_fields] => [
                {"id":"1","value":"1055"},
                {"id":"2","value":"Link"},
                {"id":"3","value":"Name"}
                ]
     )

this is an $item coming out of Joomla CMS K2 plugin when I use print_r() command I can access normal stuff like this $item->title and get XXXXXXX for my value, but could not figure out how to get items from extra_fields

user117911
  • 79
  • 1
  • 6

2 Answers2

0

as others mentioned, this doesn't seem valid PHP data, however if you would convert array of objects to multidimensional array, then do it this way:-

<?php 
// new var that holds the arrays
$multiArray = [];
foreach ($extra_fields as $field){
$multiArray[] = (array) $field;
}
// now this var has the exact data that you want...
print_r($mulitArray);

Hope this helps.

Eissaweb
  • 142
  • 1
  • 10
0

The solution to my problem was pretty simple, I used json_decode() function to convert it too array

    $someArray = json_decode($item->extra_fields, true);
                    print_r($someArray[0]["value"]);

Thank you all for your help

user117911
  • 79
  • 1
  • 6