menu

Questions & Answers

How do I fix this illegal offset type error on php?

I'm trying to get the post of the title from an array where the post id it's equal to post_id on second array, but i get this error:

Fatal error: Uncaught TypeError: Illegal offset type on line $array[$week][$days][$day]['post_title'] = $post_data['post_title'];

$posts = Array ( [0] => Array ( [post_id] => 3 [0] => 3 [post_title] => Task 1 );

$array = [{"week1":{"day1":[{"post_id":"3","post_type":"timebased","post_reps":"","post_sets":"","post_time":"60","post_rest":"30"},{"post_id":"2","post_type":"repsbased","post_reps":"12","post_sets":"4","post_time":"","post_rest":"35"}],"day2":[{"post_id":"1","post_type":"repsbased","post_reps":"8","post_sets":"4","post_time":"","post_rest":"30"},{"post_id":"2","post_type":"repsbased","post_reps":"8","post_sets":"5","post_time":"","post_rest":"60"}],"day3":[],"day4":[],"day5":[],"day6":[],"day7":[]}}];

function getTitle($array, $posts){
    if(is_array($array)) {
    foreach($array as $week){
    foreach($week as $days){
        foreach($days as $day => $post){
        foreach($post as $data){
            foreach($posts as $post_data){
            if($data['post_id'] == $post_data['post_id']){
                $array[$week][$days][$day]['post_title'] = $post_data['post_title'];
                                }
                            }
                        }
                    }
                }
            }
        return $array;
        }
    return false;
}
Comments:
2023-01-11 09:00:31
Your $array is currently in json?
2023-01-11 09:00:31
@Darren yes in json.
Answers(0) :