Home > Software engineering >  PHP error: Uncaught TypeError: Cannot access offset of type string on string
PHP error: Uncaught TypeError: Cannot access offset of type string on string

Time:06-25

I understand there are many similar "TypeError: Cannot access offset of type string on string" but everyone is different, and i read them. How do I skip over the nested values? We only need address_line_1, City etc.

this is the code:

foreach ($jd as $line) {
  if(!in_array($line['phone'], array_column($csv, 'phone'))){
    $newarray[sizeof($newarray)]=$line; //add unique row
    fputcsv($file, $line); //update the master csv file
  }
}

This is what some of the data looks like

                [address_line_1] => 10860 Larry Dr, Northglenn, CO 80233
                [address_line_2] => 
                [anonymous] => 
                [blocked] => 
                [campaign_id] => o1RqNONn9jHnnBRd1M9A
                [campaign_name] => K1Keto  Camp 4/28
                [city] => Northglenn
                [company] => 
                [country] => US
                [created_date_unix] => 1656104474
                [customer_card] => Array
                    (
                    )

                [customer_group] => Array
                    (
                        [0] => Array
                            (
                                [id] => P67902g06ZfaN1O64p08
                                [name] => Prospect
                            )

                    )

                [email] => [email protected]
                [enabled] => 1
                [first_name] => William
                [full_address] => 10860 Larry Dr, Northglenn, CO 80233, USA
                [geocode_success] => 1
                [google_place_id] => ChIJy8IV-5p2bIcRE9IovuASaOY
                [id] => y2JWmaLGP1TdP0qoMzM5
                [internal_id] => 
                [last_name] => Wolters
                [lat] => 39.8933644
                [lifetime_value] => Array

CodePudding user response:

Check if $line is an array before trying to index it.

if(is_array($line) && !in_array($line['phone'], array_column($csv, 'phone'))){

CodePudding user response:

You can try 2 things:

is_array($line) to see if $line is really an Array

if(is_array($line)){...}

and see if the nodes really exist using the isset();

if (isset($line['phone']) ) {....}
  • Related