Array help [0], somehow take [0] data and merge with correct area

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());

The line where you have $this->responses['topics'][] = $responses; is where you are getting the zero index. See the empty second of square brackets? That is pushing it onto the end of your topics, but it is going to assign the key… which is numeric starting at zero.

Since $responses is going to be an array, you have a few choices. You can use array_merge like $this->responses['topics'] = array_merge($this->responses['topics'], $responses);

Here we are saying “Take the responses array (which came from the JSON) and merge it with the topics array in $this->responses”. Now of course this means that if the same key is found twice, there might be some overwriting.

Second option is to simply add the arrays together…$this->responses['topics'] = $this->responses['topics'] + $responses;

Another option is a bit more of a long way, but loop through your $responses variable from JSON and add each to $this->responses[‘topics’] using the key and value…

foreach ($responses as $key => $value) {
   $this->responses['topics'][$key] = $value;
}

I probably would go with one of the first two if it were me. :slight_smile:

1 Like

Thankyou so much. I really appreciate the help and detailed explanation. :slight_smile: