Hi
I’m sure an extremely easy solution however I can’t figure it out.
I’m experimenting with data and merging json data into a dictionary. I’m trying to push this new data decoded from json to array however it gives it in a [0] key and I’m unsure how to get it out of the [0] and just part of the array tree where the already coded topics live.
My expected outcome was:
Thanks
Json File:
{
"weather":{
"sunny":"its sunny",
"stormy":"its stormy",
"rainy":"its raining"
},
"test":{
"dog":"a dog?",
"cat":"a cat?",
"fish":"a fish?"
}
}
Class:
<?php
declare(strict_types=1);
namespace BlueInTheStars;
class BotTest
{
private array $responses = [
'universal' => [
'hi' => 'Hi there :)',
'bye' => 'Thanks for getting in touch',
'unsure' => 'I\'m unsure, please try rewording',
],
'topics' => [
'medical' => [
'headache' => 'Make sure you drink plenty of water.. for more info visit nhs.uk',
'broken arm' => 'Please dial 999. An ambulance will be on the way',
'covid' => 'You might have covid, please order a test and stay home. visit nhs.uk for more info',
],
'another-topic' => [
'idk' => 'another topic',
'idk1' => 'dfgdgdg',
],
//add more topics as needed
]
];
public function __construct($json = null)
{
if(i$json !== null) { // is_null()
$responses = json_decode($json, true); // add try/catch
$this->responses['topics'][] = $responses;
//$this->responses['topics'][$responses];
}
}
public function getTopics()
{
return $this->responses['topics'];
}
Output:
print_r($bot->getTopics());