Solved

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

Posted on 2016-08-02
6
92 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 
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 Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
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.

705 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