Convert object to array in php - php

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);

Related

How to convert Object Array to specific attribute value array in php

I want to convert from this array
[{"tagid":"422"},{"tagid":"467"},{"tagid":"146"},{"tagid":"097"}]
to
["422","467","146","097"]
Is there a way to convert this without using loop in php, or the best possible method in php?
$json = '[{"tagid":"422"},{"tagid":"467"},{"tagid":"146"},{"tagid":"097"}]';
$arr = array_column(json_decode($json, true), 'tagid');

convert stdclassobject into array

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

Json to Array to XML[Php]

I'm trying to convert an array to xml using a json in php
The json and array work fine but when I try converting the array to xml I keep getting an error.
Call to undefined function array_to_xml()
This is the json
{"host":"127.0.0.1","username":"root","password":"something","dbname":"something","table":"something"}
Json to `array
$json = json_decode(file_get_contents('something.json'), true);
$info = array(
$json['host'],
$json['username'],
$json['password'],
$json['dbname'],
);
Array to xml
simplexml_load_string(array_to_xml($json, new SimpleXMLElement('<connection/>'))->asXML()) or die("can't read");
Maybe because there is no function called array_to_xml()? You have to define it first.

after json in php i get stdClass and cannot use array

i use JSON.stringify(pageSettings) in jquery to ajax an array to php and save the file. file content:
{"MidHeight":367,"BotTop":502}
i use json_decode to load it back to array in php:
$pageSettings=json_decode(file_get_contents($path.$file);
when i print_r($pageSettings,true) results are:
stdClass Object
(
[MidHeight] => 276
[BotTop] => 411
)
but when i try to read from it with:
$pageSettings["MidHeight"]
i get:
PHP Fatal error: Cannot use object of type stdClass as array.
Either use property-access notation ($pageSettings->MidHeight) or tell json_decode to always give you an associative array using the second argument: $pageSettings = json_decode($json_str, true);

Can't get value from array

I'm trying to output the value of the email value of an array, but have problems doing so.
The array is based on json_decode()
This is the error I receive
Fatal error: Cannot use object of type stdClass as array in /home/.... line 57
JSON (value of: $this->bck_content)
{"email":"test#email.com","membership_id":"0","fname":"Kenneth","lname":"Poulsen","userlevel":"1","created":"2012-04-23 10:57:45","lastlogin":"2012-04-23 10:58:52","active":"y"}
My code
# Display requested user details
$details_array = json_decode($this->bck_content);
$value = $details_array['email'];
print $value;
You need to use the second argument to json_decode to force array structures on JS objects.
json_decode($this->bck_content, true);
This will make sure all JS objects in the json are decoded as associative arrays instead of PHP StdObjects.
Of course that is assuming you want to use array notation to access them. If you're fine with using object notation then you can just use:
$value = $details_array->email;
try this one
$value = $details_array->email;
or
json_decode($json, true);
or
$details_array = (array)json_decode($json);
what have you done wrong is writen in error description

Categories