Avatar of tjyoung
tjyoung
 asked on

Retrieving record but the column appears to be a string, not array

Hi,
I'm in laravel, working on retrieving a record of a vehicle. Within the record is a column 'vehicle_options' which saves the options of a vehicles like this:
["Driver Side Air Bag","Passenger Air Bag","Side Impact Airbag"] etc. etc.

As part of a form the record is being pulled into, there are a series of checkboxes that need to be checked or not checked depending if the option exists.

Was trying below but I'm thinking the $vehicle->vehicle_options is returning as a string so my code below fails. Any idea where I'm going wrong?

if (in_array("Driver Side Air Bag", $vehicle->vehicle_options)) {
echo "found";
} else {
echo "not found";
}

Open in new window


When I var_dump the variable I get:
string(659) "["Driver Side Air Bag","Passenger Air Bag","Side Impact Airbag","Child-Safety Locks","ABS Brakes","Traction Control","Stability Control","Fog lights","Alarm","Anti-Theft","Power-Assist Disc Brakes","Power Steering","Power Windows","Power Locks","Power Mirrors","Tilt Steering","Telescoping Steering Wheel","Cruise Control","Keyless Entry","Heated Mirrors","Intermittent Wipers","Rear Defrost","Auxiliary 12v Outlet","Steering Wheel Controls","Split Folding Rear Seats","Air Conditioning","Heated Front Seats","Bucket Seats","Privacy Glass","Climate Control","Cloth Interior","AM\/FM Stereo","CD Player","MP3","Auxiliary Audio Jack","Bluetooth","Alloy Wheels"]" 

Open in new window

LaravelPHP

Avatar of undefined
Last Comment
tjyoung

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Gauthier

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Gauthier

Also, you may want to check the vehicle_options string if only one options is checked, or if no option are checked the "[" "]" may then be missing.
Olaf Doschke

To me, this looks like the same case you already had solved from Ray Paseur and gr8gonzo in https://www.experts-exchange.com/questions/28936728/Arrays-saved-in-mysql-DB-how-to-extract-properly-in-php.html

There is no data type array in MySQL, what's stored is a string in a JSON notation.
What is normal is getting a row of an SQL query as an associative array with column names as their key, but not getting an array out of a single column. So somehow this is preprocessed when saving to save that string and you post process that after retrieving it from the database.

Bye, Olaf.
tjyoung

ASKER
I apologize it is very close if not the same. I did however try Ray's fix and find this is happening:

I'm testing like this:
$options =  explode(',', $vehicle->vehicle_options, true);
if (in_array("Passenger Air Bag", $options)) {
echo "found";
} else {
echo "not found";
}

Open in new window


Array looks like below, but returns 'not found'. Any idea why?

array(1) { [0]=> string(659) "["Driver Side Air Bag","Passenger Air Bag","Side Impact Airbag","Child-Safety Locks","ABS Brakes","Traction Control","Stability Control","Fog lights","Alarm","Anti-Theft","Power-Assist Disc Brakes","Power Steering","Power Windows","Power Locks","Power Mirrors","Tilt Steering","Telescoping Steering Wheel","Cruise Control","Keyless Entry","Heated Mirrors","Intermittent Wipers","Rear Defrost","Auxiliary 12v Outlet","Steering Wheel Controls","Split Folding Rear Seats","Air Conditioning","Heated Front Seats","Bucket Seats","Privacy Glass","Climate Control","Cloth Interior","AM\/FM Stereo","CD Player","MP3","Auxiliary Audio Jack","Bluetooth","Alloy Wheels"]" }

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Olaf Doschke

The answer you accepted was about json_decode, wasn't it?

Bye, Olaf.
tjyoung

ASKER
In the end, this worked for me perfectly and was able to located the item in the array. No idea why, but works! If you ran this code, returns true:

$options = str_getcsv($vehicle->vehicle_options);
if (in_array('Power Windows', $options)) {
echo "found";
} else {
echo "not found";
}