Getting data from a json file with arrays - php

I'm having troubles to get specific data from a json file using the built-in php functions.
Let me show you some code :
Json File :
{"votes": [ { "date":"November 3rd, 2017 10:08 PM EST", "timestamp":1509743288, "nickname":"Th3ProHack3R", "claimed":"0" }, { "date":"November 3rd, 2017 10:06 PM EST", "timestamp":1509743160, "nickname":"TheKing", "claimed":"0" }, { "date":"November 3rd, 2017 09:45 PM EST", "timestamp":1509741902, "nickname":"some0ne", "claimed":"0" } ] }
My php code :
$json= file_get_contents("myfile.json");
$data = json_decode($json, true);
$voter1 = $data['votes']->nickname;
echo $firstvoter;
I had this code working on a very basic json code without arrays. But something is wrong here because I can't get the nicknames of the voters.
I'm just a little bit confused, so what I wanna do is picking up the nicknames of the voters and then I'll put them on a table using html.
I hope I get a detailed answer so I can understand some of the confusing stuff.

you need to get array to json object. hear "votes" is json object so $data['votes'][0] is return first array see code...
<?php
$json= file_get_contents("myfile.json");
$data = json_decode($json, true);
$voter1 = $data['votes'][0];
$voter2 =$data['votes'][1];
print_r( $voter1);
echo "<br>".$voter1["nickname"];
echo "<br>".$voter2["nickname"];
?>

After converting into json Decode you array make look like this,
Array
(
[votes] => Array
(
[0] => Array
(
[date] => November 3rd, 2017 10:08 PM EST
[timestamp] => 1509743288
[nickname] => Th3ProHack3R
[claimed] => 0
)
[1] => Array
(
[date] => November 3rd, 2017 10:06 PM EST
[timestamp] => 1509743160
[nickname] => TheKing
[claimed] => 0
)
[2] => Array
(
[date] => November 3rd, 2017 09:45 PM EST
[timestamp] => 1509741902
[nickname] => some0ne
[claimed] => 0
)
)
)
In this you need the nickname:
foreach ($jsonData['votes'] as $key => $value) {
echo "Nickname : ". $value['nickname']."<br/>";
}

Related

using usort on simpleXML that's been converted to an array

I have read a standard RSS feed into simpleXML and get the following after converting to an array:
[0] => SimpleXMLElement Object (
[guid] => https://www.zazzle.com/scissors_by_any_other_name_dreadful_pun_t_shirt-235586301981812394
[pubDate] => Wed, 23 Aug 2017 13:41:08 GMT
[title] => SimpleXMLElement Object ( )
[link] => https://www.zazzle.com/scissors_by_any_other_name_dreadful_pun_t_shirt-235586301981812394
[author] => HightonRidley
[description] => SimpleXMLElement Object ( )
[price] => $30.15
)
[1] => SimpleXMLElement Object (
[guid] => {not enough reputation points to show link}
[pubDate] => Sat, 19 Aug 2017 15:53:19 GMT
[title] => SimpleXMLElement Object ( )
[link] => {not enough reputation points to show link}
[author] => HightonRidley
[description] => SimpleXMLElement Object ( )
[price] => $15.65 )
)
This is the code used to create and display the above
$sortable = array();
foreach($product_grid->channel->item as $node) {
$sortable[] = $node;
}
print_r($sortable);
To prove to myself it's working and accessible as intended I used this
foreach ($sortable as $rssitem) {
echo "<br>" . $rssitem->pubDate;
}
echo "<br>".$sortable[0]->pubDate;
echo "<br>".$sortable[1]->pubDate;
and got this output
Wed, 23 Aug 2017 13:41:08 GMT
Sat, 19 Aug 2017 15:53:19 GMT
Wed, 23 Aug 2017 13:41:08 GMT
Sat, 19 Aug 2017 15:53:19 GMT
...but when I try to sort using usort, I get no output and Firefox console tells me I have a server error 500
Here's how I'm using usort
usort($sortable, function($a, $b)
{
return strtotime($a->pubDate) > strtotime($b->pubDate);
});
I've checked various questions/answers on using usort here and as many other places as I can find online but to no avail.
Can someone please tell me how to reference the various elements of $sortable from within that anonymous function. I will be doing other sorts using other elements once I get this first one cracked.
Thanks!

Remove string from an stdClass object in php

I have an array called $field->selectoptions[1]
print_r($field->selectoptions[1]) gives these results:
stdClass Object ( [voption] => clabels [clabel] => Data Inizio
[values] => Qualsiasi Data 1 Luglio 2016 12 Settembre 2016 14 Novembre
2016 [slabel] => Data Inizio )
I need to remove from [values] all invalid dates, in this case "1 Luglio 2016" (July 1st 2016) because the date has passed
I tried using unset but that's appears to be a single string.
I don't think a str_replace would work either...
Any idea?

Unset(remove) array key in php

Following is my $newArr0 which is an array of objects.
Array
(
[0] => stdClass Object
(
[created_at] => Mon Dec 08 03:04:47 +0000 2014
[text] => How a individual man can adopt a village in andhra real guds needed hats off to you MASTER BLASTER #Sachin
[source] => Twitter Web Client
)
[1] => stdClass Object
(
[created_at] => Sun Dec 07 17:23:25 +0000 2014
[text] => #two #cool #peoples ..#Coolfieee ..#me nd #Sachin http://t.co/JU971nWAPo
[source] => Instagram
[2] => stdClass Object
(
[created_at] => Sun Dec 07 15:18:22 +0000 2014
[text] => Snga hit 90 odi international fifty...can he hit 6 more to break sachin s fifty record ..#sachin 96 odi fifty ...
kya lgta h tod dega record
[source] => Twitter Web Client
)
[3] => stdClass Object
[created_at] => Sun Dec 07 14:50:53 +0000 2014
[text] => #jeeturaaj Jeeturaaj want #sachin chi book pahje, please give #Sachin #playingitmyway ......:)
[source] => Twitter Web Client
)
[4] => stdClass Object
[created_at] => Sun Dec 07 14:33:49 +0000 2014
[text] => RT #UthMag: Old... http://t.co/b1HMzE3TZI #BCCI #cricket #featured #global #ICC #india #littlemaster #News #ODI #retire #sachin #sports #te…
[source] => Twitter for Android
)
)
I am trying to drop a key with source name from the array, so I tried this code -
foreach ($newArr0 as $nkey1 => $nval1) {
if($nkey1 == "source") {
unset($newArr0["source"]);
}
}
But it is not removing the key, from $newArr0 Let me know what I am doing wrong here.
First off, its a collection of objects inside the array, so you'd use the -> arrow operator on each object inside your loop. Then, alternatively, you could reference each copy of the object inside the foreach and make your unset.
foreach($newArr0 as &$nval1) {
// for each object inside `$newArr0` is in `$nval1`
unset($nval1->source);
// unset $nval1's source
}
<?php
$newArr = array();
foreach ($newArr0 as $key => $value) {
unset($value->source);
$newArr[$key] = $value;
}
$newArr0 = $newArr;
Demo
Here:
foreach ($array as $key => & $object) {
if ($object->source === 'source') {
unset($array[$key]);
}
}

Parse XML with incresing tag values using PHP

I'm using some api which returns the list of transactions done by the user in XML format. This is how it looks like.
<Response>
<Status>00</Status>
<STMT>
<T0>
<ID>25624</ID>
<DATE>30 JUNE 2014</DATE>
<Amount>1500</Amount>
</T0>
<T1>
<ID>11495</ID>
<DATE>29 JUNE 2014</DATE>
<Amount>1000</Amount>
</T1>
<T2>
----
----
----
</STMT>
<Bal>55</Bal>
</Response>
Now, how do we get those values inside STMT tags? I tried this, but didn't work.
$result=simplexml_load_string($xmlstring);
$i='0';
$tx ='T'.$i;
while ($result->STMT->$tx) {
$result->STMT->$tx->ID;
$tx='T'.strval(intval($i++));
}
Please help.
Actually this is just straightforward. Do something like this:
$xml_string = '<Response><Status>00</Status><STMT> <T0> <ID>25624</ID> <DATE>30 JUNE 2014</DATE> <Amount>1500</Amount> </T0> <T1> <ID>11495</ID> <DATE>29 JUNE 2014</DATE> <Amount>1000</Amount> </T1> <T2> <ID>11496</ID> <DATE>28 JUNE 2014</DATE> <Amount>500</Amount> </T2></STMT><Bal>55</Bal></Response>';
$xml = simplexml_load_string($xml_string);
$stmt = $xml->STMT;
$stmt = json_decode(json_encode($stmt), true);
echo '<pre>';
print_r($stmt);
Output:
Array
(
[T0] => Array
(
[ID] => 25624
[DATE] => 30 JUNE 2014
[Amount] => 1500
)
[T1] => Array
(
[ID] => 11495
[DATE] => 29 JUNE 2014
[Amount] => 1000
)
[T2] => Array
(
[ID] => 11496
[DATE] => 28 JUNE 2014
[Amount] => 500
)
)
It works. You didn't echo the result...
echo $result->STMT->$tx->ID . "\n";
Also, $i++ should be changed to ++$i.

php want to understand foreach and if result on particular example

Have this array
$date_and_currency_array = Array
(
[0] => Array
(
[number_of_input_row] => 1
[date_day] => 01
[date_month] => 12
[date_year] => 2013
[currency] => BGN
)
[1] => Array
(
[number_of_input_row] => 2
[date_day] => 01
[date_month] => 12
[date_year] => 2012
[currency] => DKK
)
[2] => Array
(
[number_of_input_row] => 3
[date_day] => 11
[date_month] => 12
[date_year] => 2013
[currency] => ILS
)
)
Then
foreach ( $date_and_currency_array as $i => $date_and_currency_value ) {
echo $date_and_currency_value['date_year']. ' __$date_and_currency_value[date_year]<br>';
if ($date_and_currency_value['date_year'] = 2013) {
echo '2013 ....<br>';
}//if
}//foreach
Here if ($date_and_currency_value['date_year'] = 2013) { expect to echo 2013 only 2 times, because there are only two [date_year] => 2013. But 2013 echo 3 times.
Please advice why 2013 echo 3 times.
Thanks to replies. My stupid negligence. Need to rest
Its because your assigning instead of comparing as you missed a = in your if statement
if ($date_and_currency_value['date_year'] == 2013) {
in this if ($date_and_currency_value['date_year'] = 2013) you should add two == that is if ($date_and_currency_value['date_year'] == 2013)
As here you have used single '=' condition will be true for each time the loop rotates so as answer given above you need to use '==' so that it exactly notifies compiler to compare as single '='is assignment operator.
so try out
if($date_and_currency_value['date_year'] == 2013)
{
echo "something";
}

Categories