PHP json_decode() not working - php

I have a problem I am getting a json file I can echo it and it looks like this
[{"sys_data":"0MxPPaza","date":"2015-02-15","objective":"VIDEO"}]
in my code I am doing this
$json = FROM THE SERVER;
$obj = json_decode($json);
$res = $obj->["objective"];
echo $res;
res is NULL obj is NULL also

Your json_decode call returns an array, with one member.
Here is a dump of your json object:
array (size=1)
0 =>
object(stdClass)[10]
public 'sys_data' => string '0MxPPaza' (length=8)
public 'date' => string '2015-02-15' (length=10)
public 'objective' => string 'VIDEO' (length=5)
so replace this line:
$res = $obj->["objective"];
With this:
$res = $obj[0]->objective;

Just replace [] quotes to {}. Like $res = $obj[0]->{"objective"};
Or you may use assoc array convertation instead of object:
$json = FROM THE SERVER;
$obj = json_decode($json, true);
$res = $obj["objective"];
echo $res;`

Related

2-dimensional array to 1-dimensional array convertion

With my code:
$sql = "SELECT number.phone_number FROM number, ordered_number WHERE ordered_number.number_id=number.id AND ordered_number.order_id=$id";
$connection = \Yii::$app->getDb();
$command = $connection->createCommand($sql);
$numery = $command->queryAll();
I get array that looks like this:
array (size=3)
0 =>
array (size=1)
'phone_number' => string '546732354' (length=9)
1 =>
array (size=1)
'phone_number' => string '565345456' (length=9)
2 =>
array (size=1)
'phone_number' => string '456557546' (length=9)
I want to get simple array, where the first element is just the number (here - the string), without name 'phone_number' and additional 1-element arrays inside the main array. When I try to do foreach on this array, it tells me that I use "Illegal offset type". I found that it means I'm using object, instead of an array, but that's an array, not an object and I have no idea what to do.
Even simplier (but for php5.5 and php7):
$numery = array_column(
$command->queryAll(),
'phone_number'
);
Use below loop to get desired result
$numery = $command->queryAll();
$number_arr = array();
foreach($numery as $number)
{
array_push($number_arr,$number['phone_number']);
}
print_r($number_arr);

PHP - Convert foreach to arrays

Anyone can help me
I have a little problem with the following code
foreach ($products as $product) {
$product->name;
$product->code;
...
}
My output code with var_dump($products)
array
0 =>
object(stdClass)
...
...
...
1 =>
object(stdClass)
...
...
...
And I need output something like this
$output = array(
array('name' => 'item1', 'code' => 'code1', 'price' => '10.00'),
array('name' => 'item2', 'code' => 'code2', 'price' => '20.00')
);
For this purpose, there is a function is php json_decode()
mixed json_decode ( string $json [, bool $assoc = false [, int $depth
= 512 [, int $options = 0 ]]] )
Need to use this function sensibly.
As you see in the function description,
The function's three parameters:
1) Variable to be converted.
2) Return as associative array.
3) Depth: default => 512. Means upto depth of 512 levels (in case of multi-dimensional array or complex object), the child elements will be converted into arrays if second parameter is set to true.
First encode your variable by json_encode() and the decode it with using
json_decode().
json_decode() 's second parameter should be set to true.
This true means return associative array instead of original type.
Pseudo code:
$output = json_decode(json_encode($yourVar), TRUE);

Add new element in result Fetchrow without use of toArray

My request with zend 1 :
$table_mail = Table_Factory::getInstance()->getTable('Table_Dossier');
$req = $table_mail->select()->where('id_email =?', $id_email);
$data_email = $table_mail->fetchRow($req);
$data_email contains :
object(Zend_Db_Table_Row)[75]
protected '_data' =>
array (size=17)
'id_email' => string '1685' (length=4)
'id_operation' => string '931' (length=3)
...
But after i want add one element (to_moi) in $data_email (without use of toArray()).
I have try without success :
$data_email[] = array('to_moi' => true);
$data_email->put('to_moi' , true);
$data_email->to_moi = true;
$data_email(array('to_moi' => true));
Thank you.
Try this will may help you ,
$data_email['to_moi'] = true;

json_decode() expects parameter 1 to be string, array given?

this is my data ,After Json_encode()
Array
(
[{"customerId":"1","customer_name":"Jon_doe","amount":"12312312","billcode":"b1231","billname":"cashbilname","billcategorycode":"1234","billcategory":"utility","month":"May","year":"2015","txcode":"10","stationid":"152","station":"Coroom","operatorcode":"1200","operator":"jame","terminal":"ter12312","txdate":"12\/2\/2015","txtime":"12:21:22_PM"}]
=>
)
now i want to decode it back ,by applying json_decode() it gives the following error
json_decode() expects parameter 1 to be string, array given
any idea sugestion what to do ?
Your json must be in string, not in array
$json_string = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
$json_array = json_decode($json_string);
$json_array : ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5];
If your json is in array you can do :
$json_string_in_array = ['{"a":1,"b":2,"c":3,"d":4,"e":5}'];
$json_array = json_decode($json_string_in_array[0]);
json_encode() returns a string, so I don't know how you can be getting an array out of it unless you are storing it in an array yourself like:
$json = [];
$json[] = json_encode($someArray);
Instead, just store it in a non-array variable:
$jsonString = json_encode($someArray);
Then you can decode it like this:
$decodedArray = json_decode($jsonString);
$jsonstr = '[{"customerId":"1","customer_name":"Jon_doe","amount":"12312312","billcode":"b1231","billname":"cashbilname","billcategorycode":"1234","billcategory":"utility","month":"May","year":"2015","txcode":"10","stationid":"152","station":"Coroom","operatorcode":"1200","operator":"jame","terminal":"ter12312","txdate":"12\/2\/2015","txtime":"12:21:22_PM"}]';
$ar = json_decode($jsonstr,true); # json string to Array
$obj = json_decode($jsonstr); # json string to Object
var_dump($ar,$obj);

Getting Blank instead of a value when using -> in PHP

I am making a web app. In one part of it, I have JS send a JSON string to PHP. The conent of the string is:
{"date":"24-03-2014","Cars":["Cheap","Expensive"]}
I want to convert the string into an object, for which I am doing:
$meta = $_POST["meta"];
$obj = json_decode($meta);
echo $obj->date;
Anyhow, Instead of having 24-03-2014 as the output, I am getting a blank line as the output.
What's wrong? What's the correct way of doing this?
Not able to re-produce it:
$jsonStr = '{"date":"24-03-2014","Cars":["Cheap","Expensive"]}';
$jsonObj = json_decode($jsonStr);
var_dump($jsonObj);
var_dump($jsonObj->date);
Outputs:
object(stdClass)[1]
public 'date' => string '24-03-2014' (length=10)
public 'Cars' =>
array (size=2)
0 => string 'Cheap' (length=5)
1 => string 'Expensive' (length=9)
string '24-03-2014' (length=10)
Are you sure your $_POST['meta'] is set & has values?
Below works like a charm. Your $_POST["date"] has not correct value inside. Try var_dump($_POST) to debug it.
<?php
$input = '{"date":"24-03-2014","Cars":["Cheap","Expensive"]}';
$meta = $input;
$obj = json_decode($meta);
var_dump($obj->date); //Prints string(10) "24-03-2014"
?>

Categories