from json array with brackets to variable or mysql - php

I have this script to display some value from a json file:
<?php
//read the json file contents
$jsondata = file_get_contents('http://website.com/file.json');
//convert json object to php associative array
$data = json_decode($jsondata, true);
//get the weather details
$icon = $data['weather']['icon'];
//Display variables
echo "icon value: $icon"; ?>
the json file is :
{"coord":{"lon":130.84,"lat":-12.46},"weather":[{"id":501,"main":"Rain","description":"moderate rain","icon":"10n"}],"base":"stations","main":{"temp":21,"pressure":1011,"humidity":100,"temp_min":21,"temp_max":21},"visibility":5000,"wind":{"speed":1.5,"deg":130},"clouds":{"all":90},"dt":1479589200,"sys":{"type":1,"id":8209,"message":0.162,"country":"AU","sunrise":1479501624,"sunset":1479547451},"id":2073124,"name":"Darwin","cod":200}
I need to display the icon value (10n), but my script didn't work... Brackets in weather section give me some trouble...
thanks for your help

you should access this way
$icon = $data['weather'][0]['icon'];
//Display variables
echo "icon value: $icon"; ?>
output
icon value: 10n
var_dump will help you to understand structure of array.this is how $data['weather'] looks like.
//var_dump($data['weather']);
array (size=1)
0 => // you forget to access this element first?
array (size=4)
'id' => int 501
'main' => string 'Rain' (length=4)
'description' => string 'moderate rain' (length=13)
'icon' => string '10n' (length=3)
weather is a array.so you should access nth element first.in this case first index $data['weather'][0] then you can access icon

Related

how to access data from an array placed inside an other array in php (codeigniter)?

array (size=2) //top array containg an other arrays
0 =>
array (size=2) //array containing 2 index
'content' => string ' The Lion King ' (length=33)
'slide_style' => string '' (length=0)
1 =>
array (size=2)
'content' => string ' Current fruit : mana bhanya the =
'slide_style' => string '' (length=0)
2 =>
array (size=2)
'content' => string ' Current fruit : mana bhanya the king
'slide_style' => string '' (length=0)
i have that structure of array please tell me how i can access data in my view file
Let's say that your top array is $array.
You could loop through the array to access each inner array and its items:
foreach($array as $inner_array) {
echo $inner_array['content'];
echo $inner_array['slide_style'];
}
Or to access certain values of inner arrays directly without looping through the top array, you could do this:
echo $array[0]['content'];
echo $array[0]['slide_style'];
Within this second method, you can swap out the 0 for whichever inner array you want to access (0 being the first, 1 being the second and so on).
Hope this helps.
You pass that array as a second argument to $this->load->view().
For example, you have a view page called posts.php and you pass data to it via controller like the following:
$data['posts'] = $this->post_model->getPosts();
$this->load->view('posts.php', $data);
and you access it in the view page posts.php like:
foreach($posts as $post) {
// Here the other code goes ...
}
Like you mentioned in the comment, if you want to loop through the array, this is the code you might be looking for:
foreach($posts as $post) {
echo "<h2>{$post['title']}</h2>";
echo "<p>{$post['description']}</p>";
}
This is how you access multi-dimensional array.
Hope you got it.

PHP - retrieving and parsing this JSON file

I'm looking for a way to access this JSON file data. What I'm interested in is to extract the property named documentjsonblob, as below.
object(Quadrem\Model\Order)[201]
protected '_dateCreated' => null
protected '_buyerCode' => null
array (size=2)
2415 =>
array (size=23)
'#storeid' => string '813' (length=3)
'#active' => string '1' (length=1)
'#created' => string '2013-11-25 12:28:21' (length=19)
'documentjsonblob' => string '{"HEAD":{"ORDERSEQUENCE":"0","TOTAL_TAX":7.9,"TOTAL_AMOUNT":86.9,"NUMBER":"AKMon3","TYPE":"NB","TYPE_NAME":"Standard PO","SUPPLIER":"0000002122","CREATED":"2013-04-29T12:00:00Z","CONTRACT_NUMBER":"","EXTERNAL_REFERENCE":"","CONTACT_PERSON":"Caroline Howlett","COMMENT":[""],"CURRENCY":"AUD","DELIVERY_TERMS":"DeliveryCondition|","PAYMENT_TERMS":[{"TEXT1":"21st day of next month after receipt"}],"NET_VALUE":79.0,"DELIVERY_ADDRESS":{"NAME_1":"KGTP FACILITY","NAME_2":"GAS TREATMENT PLANT","NAME_3":"KGPF","POSTAL_CODE":"6714","CITY":"Karratha","STREET":"Withnell Bay","REGION":"AUWA","REGION_NAME":"AUWA","COUNTRY":"AU"}]}' (length=3029)
'documenttype' => string 'PO' (length=2)
1890 =>
array (size=23)
'#storeid' => null
'#active' => null
I'm not quite sure which kind of an encoded JSON file this is by the way it looks. Would appreciate it if somebody could help.
Thanks.
Since in the comment you have mentioned it is the out put of var_dump and you want to get documentjsonblob, let's follow this:
On first line you have object(Quadrem\Model\Order)[201], that means you var_dumped an object, say $my_obj
On line four array (size=2) says you have an array, and what you need is located in the first element of the array, so $my_obj[0]
$my_obj[0] holds an associative array, and the desired index is documentjsonblob, so we can get the String representation as:
$my_arr = $my_obj[0];
$json_str = $my_arr['documentjsonblob'];
Now $json_str holds the String representation, to convert it to an PHP object:
$json_obj = json_decode($json_str);
So now $json_obj holds the object you want.

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"
?>

Array manipulation

Hey I am stuck in the middle of my code. I am getting some input data from my partner and I get an array. At the 0 index of the array there are 3 strings. The array looks like this
array
0 =>
object(MunichInnovationGroup\PatentBundle\Entity\PatentIdJson)[1405]
private 'patentId' => string 'EP.02708872.A' (length=13)
private 'jsonData' => string '{"ops:world-patent-data": {
"#xmlns": {
"ops": "http://ops.epo.org",
"$": "http://www.epo.org/exchange",
"ccd": "http://www.epo.org/ccd",
"xlink": "http://www.w3.org/1999/xlink"
},
"ops:meta": {
"#name": "elapsed-time",
"#value": "69"
}
private 'status' => string 'Found' (length=5)
What I am interested in is the 'jsonData' String. Can I covert this whole array to a multidimensional array or just get the 'jsonData' and convert it into an array so that I get the information what I need from the 'jsonData'.
Thanks
what you need is json_decode and you can do the following:
$jsonObject = json_decode($myArray[0]->getjsonData(), true);
That will generate an object from the jsonData your partner is providing in the array
Based on your comment, you can get to the data using:
$jsonData = $your_array[0]->getjsonData();
Assuming that the MunichInnovationGroup\PatentBundle\Entity\PatentIdJson extends the MunichInnovationGroup\PatentBundle\Entity class.

How to get content from array

I'm total newbie to PHP and Drupal but I fix this simple(?) thingo on my template. I want to get title, date, text and link path from this array? I can't get it out of there. I think its because its in inside of another array and because im noob in PHP I cant get it out of there ? Also I would like to get it to a loop. If theres more content, I would get it as a list or something. I would use foreach for this? Like foreach $contents as $content and so on?
I get this output from this: var_dump($contents);
array
'total' => string '1' (length=1)
'data' =>
array
0 =>
array
'cid' => string '13231' (length=3)
'title' => string 'TITLEBLABLABLA' (length=3)
'body_text' => string 'TEXTBLABLABAL' (length=709)
'created' => string '313131' (length=10)
'created' => string '2010-07-13 14:12:11' (length=19)
'path' => string 'http://goog.fi' (length=72)
Think of accessing multidimensional arrays in the same way you'd access a file in a subdirectory: just reference each level/directory in sequence. Assuming that array is stored in $arr, you'd get the title as follows:
$arr['data']['0']['title']
Here is a basic PHP array tutorial
It comes down to this:
You retrieve an element of an array with []
$array = array( 'a' => 'b');
echo $array['a']; //Prints b;
To loop through a multi-dimensional array and echo date try this...
foreach ($contents as $key => $content) {
echo $content[$key]['created'];
}
If that doesn't work, post the output of print_r($content) in your question so I can't build you exact array. I'm kinda confused over the structure of your array in your question.

Categories