Solved

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

Posted on 2016-08-02
6
79 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 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