Solved

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

Posted on 2016-08-02
6
55 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 1

Author Comment

by:tjyoung
Comment Utility
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
Comment Utility
The answer you accepted was about json_decode, wasn't it?

Bye, Olaf.
0
 
LVL 1

Author Closing Comment

by:tjyoung
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

743 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

8 Experts available now in Live!

Get 1:1 Help Now