0

I have a JSON array returned from db query in Joomla, using loadRow().
We're using Custom Fields for some fancy page stuff.

I need the text/value pair from this single record, in a different usable array.

I have tried explode, array_map, lots of different manipulation but I'm struggling with the right combination of code.

This is what the object looks like.

Array ( [0] =>   
    {"options":  
        {"options0":  
            {"name":"soph","value":"School of Public Health"},  
        "options1":  
            {"name":"son","value":"School of Nursing"},  
        "options2":  
            {"name":"sohp","value":"School of Health Professions"}  
        }
    }
)

What I need, something more like this:

Array (
    [0] => {"name":"soph","value":"School of Public Health"},  
    [1] => {"name":"son","value":"School of Nursing"},  
    [2] => {"name":"sohp","value":"School of Health Professions"}
)
5
  • Is that actual JSON? Commented Apr 22, 2019 at 19:31
  • This is what the object looks like. That looks like an array with one json string to me. Correct? Commented Apr 22, 2019 at 19:31
  • Joomla says it is: loadRow() returns an indexed array from a single record in the table. Yes, it's JSON. Seeing that now. Commented Apr 22, 2019 at 19:33
  • Hmmm... Ok. Why do you want to get an array with json stringss in it? Why not just return it all as an array? Like: 3v4l.org/v2c5f Commented Apr 22, 2019 at 19:35
  • I'm limited in how I get the data from Joomla db. docs.joomla.org/Selecting_data_using_JDatabase Commented Apr 22, 2019 at 19:37

2 Answers 2

1

Looks like you're storing this in the DB as JSON data. Something like this should work:

$row = json_decode($arr[0], true)['options'];
$optionList = [];
foreach($row as $option => $options)
{
   array_push($optionList, [
      'name' => $options['name'],
      'value' => $options['value']
   ]);
}

putting your new data in $optionList.

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

1 Comment

That worked! Thank you so much! I don't think I really realized it was already a JSON object that I could try json_decode and manipulate from there.
0

Your question is a bit vague on what you need.
I assume that is because you don't know what question to ask.

If I was you, I believe this is what I would want.
Decode the array and use array column to flatten the array and make the "name", key and "value" value.

That way you can use the array as the echo line in the code below does.

$collegeparams = '{"options":  
        {"options0":  
            {"name":"soph","value":"School of Public Health"},  
        "options1":  
            {"name":"son","value":"School of Nursing"},  
        "options2":  
            {"name":"sohp","value":"School of Health Professions"}  
        }
    }';

$arr = array_column(json_decode($collegeparams[0], true)['options'], "value", "name");
var_dump($arr);

echo $arr['sohp'];

Output of above:

array(3) {
  ["soph"]=>
  string(23) "School of Public Health"
  ["son"]=>
  string(17) "School of Nursing"
  ["sohp"]=>
  string(28) "School of Health Professions"
}
School of Health Professions

https://3v4l.org/3MdIN

3 Comments

Thank you, yes this would also work. I was focused on Joomla documentation calling all of this an object and not seeing what was obvious, which was a JSON array that could be decoded. This is my first time playing around with Joomla data & PHP.
@hpgodwin it all depends on what you are going to do with the array. A flat array is far easier to manage and search. Having a multidimensional array means you need to loop it in order to search it/use it. But it all depends on what you are going to do.
I did not think to put why I needed it. It will be used for different modules across the site, the array was the easiest way for me to be able to use it for different things. One use is combining it with an associative array that lists out other information that was stored in different tables. A different use is matching the school name with that text value, which is the friendly url link basically.

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.