Shiva
1 week ago

ErrorException : Illegal offset type

Posted 1 week ago by Shiva

I have a csv that looks like this

product,title,category,price\n
PR001,"Product Title 1","Category 1", 123\n
PR002,"Product Title 2","Category 2", 123\n
PR003,"Product Title 3","Category 3", 123\n
PR004,"Product Title 4","Category 4", 123\n
PR005,"Product Title 5","Category 5", 123\n

What I would like to do is get an array that looks like this

array:5 [
  "PR001" => "123"
  "PR002" => "123"
  "PR003" => "123"
  "PR004" => "123"
  "PR005" => "123"
]

When I run my code I get this error

 ErrorException  : Illegal offset type

Here is my code


$users = User::all();

$productArr = [];

foreach($users as $user)
{
	$csv = $user->getCsv();

	$lines = explode(PHP_EOL, $csv);
    $csvArray = [];

    foreach($lines as $line)
    {
        $csvArray[] = str_getcsv($line);
    }

    $keys = array_shift($csvArray);

    $result = array_combine($keys, array_map(null, ...$csvArray));

    $productArr[$result['product']] = $result['price'];
}

dd($role);

Please sign in or create an account to participate in this conversation.