Solved

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

Posted on 2016-08-02
6
66 Views
Last Modified: 2016-08-02
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

0
Comment
Question by:tjyoung
  • 2
  • 2
  • 2
6 Comments
 
LVL 7

Accepted Solution

by:
Gauthier earned 500 total points
ID: 41739641
http://php.net/manual/en/function.str-getcsv.php

$optionsArr = str_getcsv(substr($vehicle->vehicle_options, 1, -1), ',', '"');

Open in new window


Be sure to check how AM\/FM is read.
0
 
LVL 7

Expert Comment

by:Gauthier
ID: 41739670
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.
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 41739686
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.
1
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Author Comment

by:tjyoung
ID: 41739746
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

0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 41739758
The answer you accepted was about json_decode, wasn't it?

Bye, Olaf.
0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 41739781
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";
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP Healthcheck 2 85
wamp versus xampp 4 45
myqsl update statement on phpMyAdmin 8 22
How to loop through records  using SELET COUNT 2 14
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now