2

I have some arrays:

$officers & $dates

Each $officers has two arrays ($i being officers number):

${'raw_tot_data_'.$i} & ${'raw_pd_data_'.$i}

these respectively being (if $i=0):

$raw_tot_data_0 & $raw_pd_data_0

Now I currently have a JSON array ($ourData) which looks similar to:

//$ourData
[
    //$officer_0
    {
        "code": "cg",
        "tots": [],
        "pds": []
    },
    //$officer_1
    {
        "code": "crg",
        "tots": [],
        "pds": []
    },
    //$officer_2
    {
        "code": "jan",
        "tots": [],
        "pds": []
    },
    ...

I would like to populate each officers tots and pds. To do that I attempted the following (this is pre json_encode($ourData)):

$i=0;
foreach($officers as $officer){
    $n=0;
    foreach($dates as $date){

        $tmp = ${'officer_'.$i};

        $ourData[$tmp]['tots'][$n] = (    //error here
            $date.' : '.${'raw_tot_data_'.$i}[$n]
        );
        $ourData[$tmp]['pds'][$n] = (    //error here
            $date.' : '. ${'raw_pd_data_'.$i}[$n]
        );

        $n++;
    }
    $i++;
}

This returns errors stating

Illegal offset type

after some research I found this:

Illegal offset type errors occur when you attempt to access an array index using an object or an array as the index key.

How could I correct this to work?

1 Answer 1

1

Your value here...

$tmp = ${'officer_'.$i};

Is setting $tmp to the value of a variable, when (I think) you want it to be just the string itself...

$tmp = 'officer_'.$i;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your response you are correct, I also had to change a bit of my code before this issue but this helped me

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.