i am laraval5.1. i am taking database table into array.
data is getting stored into stdclassobject.
i want to convert into normal array. how to do that
$silver_plans = array();
$silver_plans = DB::select('select * from ins_gold ');
print_r($silver_plans);
Dont know how to convert stdclass object into normal array
after storing database table ... by default it is storing data into into stdclass object. but i want to store into array
You can try this (built in in laravel 5):
$silver_plans = DB::select('select * from ins_gold ')->toArray();
print_r($silver_plans);
$array = (array) $yourObject;
check Convert PHP object to associative array
Related
I am converting object to array using
$data['payment_cheque'] = (array)$data['payment_cheque'];
What i Get is
"\u0000*\u0000items": ["2017-04-18","2017-06-28","2017-07-05"]
What I want is
["2017-04-18","2017-06-28","2017-07-05"]
Just try this
$data['payment_cheque'] =json_decode(json_encode($data['payment_cheque'] ,true),true);
I store array structure in database table.
example
table name - example
id=1
data= array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43")
I want to get that array structure from table and assign data column to an array.
Example
while($row=mysqli_fetch_array($result))
{
$table=$row['data'];
}
I did this way.. but it's not working.
It results in:
$table[0]=>array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43")
You can save array data to database field in multiple ways.
I suggest two ways:
1) Serialized array:
you can save data using serialize() function.
e.g. $arr = array('234' => 'asfdads', 'asdf' => 'asdf2');
$toDb = addslashes(serialize($arr));
And then you can unserialize() them to get it back like:
$toDb = unserialize(stripslashes($fromDb));
2) Using json_encode() and json_decode();
e.g. $arr = array('234' => 'asfdads', 'asdf' => 'asdf2');
$toDb = json_encode($arr);
And then you can json_decode() them to get it back like:
$toDb = json_decode($fromDb)
I have this data
$result = json_decode(curl_exec($ch));
print_r($result);
// stdClass Object ( [d] => {"id":10} )
$id = ?;
How can I get a value of id?
Since $result is an object, you have to use property notation to access its components.
$id = json_decode($result->d)->id;
You need the extra json_decode because the value of $result->d is another JSON string, not an object or array.
You would get value with following
$obj_result = json_decode($result->d);
echo $obj_result->id;
By this way you will get id value 10
I have an array that I'm storing as a string in a database to make it easier to retrieve (it's refreshed with new data every 15-30minutes via cron).
'player_list' -> 'Bob,Dave,Jane,Gordy'
'plugin_list' -> 'Plugin-A 1.4, Plugin-B 2.1, Plugin-C 0.2'
I originally store the array into the db as a string using:
$players = $liveInfo['players'] ? implode(",", $liveInfo['players']) : '';
$plugins = $liveInfo['plugins'] ? implode(",", $liveInfo['plugins']) : '';
I am currently using the following to retreive and then convert string back into array in preparation for a foreach:
$players = $server_live->player_list;
$playersArray = explode(",", $players);
$plugins = $server_live->plugin_list;
$pluginsArray = explode(",", $plugins);
For some reason, I am getting the following error: Array to string conversion I don't understand this error since I'm going from String to Array and I looked over the php.net/manual and it looks fine?...
If you need to convert from Object to String and from String to Object, then serialization is all you need to do, and you object should be supporting it.
in your case, Using Arrays, serialization is supported.
Array to String
$strFromArr = serialize($Arr);
String to Array
$Arr = unserialize($strFromArr);
for more information consider seeing the php.net website: serialize unserialize
If you must do it your way, by storing the array in the database, use the serialize() function. It's awesome!
http://php.net/manual/en/function.serialize.php
$string = serialize($array);
$array = unserialize($string);
I have a database table as follows.
<table border='1'><th>Id</th><th>FirstName</th><th>last Name</th><tr><td>1</td><td>Tom</td><td>T</td></tr><tr><td>2</td><td>Jerry</td><td>J</td></tr></table>
I would like to store all values as a multi dimensional array using php(using a while loop to retrieve fields).That is,
I would like the data to be echoed as:
array(array(1,Tom,T),array(2,Jerry,J));
$result = mysql_query("SELECT * FROM tablename;");
while($result_ar = mysql_fetch_array($result)) {
$multid_array[] = $result_ar;
}
after which $multid_array will be an array of arrays.
You can use phps serialize function to convert any variable into a string representation
$string = serialize($dbData);
You can use unserialize() to convert the string back into arrays, objects, etc
$dbData = unserialize($string);
Once you have the data within a string, it's very easy to store in a file, db, etc. A disadvantage is that you won't be able to search your database easily for this data.