How to interpret this json decoded array? - php

How can I interpret this JSON decoded array? It seems like more of a complex array than I'm accustomed to dealing with so any help would be appreciated.
Thanks.
array(1){
[0]=> object(stdClass)#1 (11){
["id"]=> string(5) "72324"
["txid"]=> string(64) "**bitcoin_tx_id**"
["from"]=> string(34) "**bitcoin_address**"
["to"]=> string(20) "email#somewhere.com"
["amount"]=> int(10000000)
["amount_sent"]=> int(0)
["note"]=> string(0) ""
["time"]=> float(1379742767000)
["to_addr"]=> string(34) "**bitcoin_address**"
["read"]=> string(1) "1"
["balance"]=> string(10) "0.10000000"
}
}

It is returning an array of objects, it looks like you're just dumping it to stdout. Toss the output from json_decode() inside a variable and you may access it like this:
$decoded = json_decode($data);
foreach($decoded as $obj) {
echo "ID: " . $obj->id . ', ';
echo "TXID: " . $obj->txid . ', ';
echo "From: " . $obj->from . ', ';
echo "To: " . $obj->to. ', ';
// ...
echo "<br>";
}
If you want json_decode() to return an associative array (which most people are used to), simply set the second parameter to true.

Related

Matching array key and value and displaying only those in the matched result

I am working with an API which provides a result set in JSON, I transform that data into a PHP array to work with it on my interface.
The issue i'm having now is trying to filter the data based on a particular key having a certain value.
Here is the result set returned:
[
{
"id":5,
"firstname":"Joel ",
"lastname":"Abase",
"displayName":"Abase, Joel ",
"officeId":3,
"officeName":"Birmingham",
"isLoanOfficer":true,
"isActive":true
},
{
"id":1,
"firstname":"Precious ",
"lastname":"Love",
"displayName":"Love, Precious ",
"officeId":4,
"officeName":"Manchester",
"isLoanOfficer":true,
"isActive":true
},
{
"id":2,
"firstname":"Bernard ",
"lastname":"Aikins",
"displayName":"Aikins, Bernice ",
"officeId":2,
"officeName":"Manchester",
"isLoanOfficer":false,
"isActive":true
},
{
"id":8,
"firstname":"Kwame",
"lastname":"Joseph",
"displayName":"Joseph, Kwame",
"officeId":2,
"officeName":"Manchester",
"isLoanOfficer":true,
"isActive":true,
"joiningDate":[
2018,
5,
1
]
},
{
"id":4,
"firstname":"Janine ",
"lastname":"Hayden",
"displayName":"Hayden, Janine ",
"officeId":1,
"officeName":"Head Office",
"isLoanOfficer":false,
"isActive":true
},
{
"id":6,
"firstname":"Esther",
"lastname":"Monroe",
"displayName":"Monroe, Esther",
"officeId":2,
"officeName":"London",
"isLoanOfficer":true,
"isActive":true,
"joiningDate":[
2017,
11,
1
]
}
]
I would like to filter the results in a loop where the 'isLoanOfficer' is equal to true, essentially only showing those who are loan officers.
Here is what I have tried so far:
<div class="col-sm-5">
<label class="control-label" for="staffId">Loan Officer<span style="color:red;">*</span></label>
<?php
$s = 0;
$temp_staff = json_decode($staff_json, true);
$count_staff = count($temp_staff);
echo '<select class="form-control" type="text" name="staffId" required>';
echo "<option value=". ">" . " </option>";
while($s < $count_staff && $temp_staff[$s]['isLoanOfficer'] == true){
echo "<option value=" . $temp_staff[$s]['id'] . ">" . $temp_staff[$s]['displayName'] . " </option>";
$s++;
}
echo '</select>';
?>
<br>
</div>
I also tried adding this code inside the while loop:
if($temp_staff[$s]['isLoanOfficer'] == true) {
echo "<option value=" . $temp_staff[$s]['id'] . ">" . $temp_staff[$s]['displayName'] . " </option>";
$s++;
}
It seems that it only evaluates the first value and then exits. Any help?
You'll have to use the condition inside the loop. Try the below code :
while($s < $count_staff){
if ($temp_staff[$s]['isLoanOfficer'] == true) {
echo "<option value=" . $temp_staff[$s]['id'] . ">" . $temp_staff[$s]['displayName'] . " </option>";
}
$s++;
}
Due to that condition your $s++ is not being executed properly.
Hope this helps.
You do not need to loop.
You can use array_intersect, array_intersect_key and array_column to find the true values.
First filter out the loanofficer column and use array_intersect to only get the true values.
Now we have an array with true values and it's corresponding key.
Use array_intersect_key to match it with the original array and the return is your filtered array.
$arr = json_decode($str, true);
$loan = array_intersect(array_column($arr, "isLoanOfficer"), [true]);
var_dump(array_intersect_key($arr, $loan));
From here is't a simple foreach outputing every value or use implode to build the output string.
https://3v4l.org/eh4uD
Output:
array(4) {
[0]=>
array(8) {
["id"]=>
int(5)
["firstname"]=>
string(5) "Joel "
["lastname"]=>
string(5) "Abase"
["displayName"]=>
string(12) "Abase, Joel "
["officeId"]=>
int(3)
["officeName"]=>
string(10) "Birmingham"
["isLoanOfficer"]=>
bool(true)
["isActive"]=>
bool(true)
}
[1]=>
array(8) {
["id"]=>
int(1)
["firstname"]=>
string(9) "Precious "
["lastname"]=>
string(4) "Love"
["displayName"]=>
string(15) "Love, Precious "
["officeId"]=>
int(4)
["officeName"]=>
string(10) "Manchester"
["isLoanOfficer"]=>
bool(true)
["isActive"]=>
bool(true)
}
[3]=>
array(9) {
["id"]=>
int(8)
["firstname"]=>
string(5) "Kwame"
["lastname"]=>
string(6) "Joseph"
["displayName"]=>
string(13) "Joseph, Kwame"
["officeId"]=>
int(2)
["officeName"]=>
string(10) "Manchester"
["isLoanOfficer"]=>
bool(true)
["isActive"]=>
bool(true)
["joiningDate"]=>
array(3) {
[0]=>
int(2018)
[1]=>
int(5)
[2]=>
int(1)
}
}
[5]=>
array(9) {
["id"]=>
int(6)
["firstname"]=>
string(6) "Esther"
["lastname"]=>
string(6) "Monroe"
["displayName"]=>
string(14) "Monroe, Esther"
["officeId"]=>
int(2)
["officeName"]=>
string(6) "London"
["isLoanOfficer"]=>
bool(true)
["isActive"]=>
bool(true)
["joiningDate"]=>
array(3) {
[0]=>
int(2017)
[1]=>
int(11)
[2]=>
int(1)
}
}
}
If you want to keep the arrays which only have "isLoanOfficer":true you could use array_filter:
$temp_staff = array_filter($temp_staff, function($x){
return $x["isLoanOfficer"] === true;
}
);
Demo
The easiest and most readable way, using a foreach:
$temp_staff = json_decode($staff_json, true);
foreach($temp_staff as $member) {
if($member['isLoanOfficer']) {
echo '<option value="' . $member['id'] . '">' . $member['displayName'] . '</option>';
}
}

Parsing object with arrays in PHP

I'm using Google's Natural Language API and it's working fine and returning data. I'm just not able to parse it correctly. I'd like to form a JSON object I can then use with AJAX or similar. What I need out of this are mainly the sentences and their sentiment. I'm struggling with this object that I get back:
object(Google\Cloud\NaturalLanguage\Annotation)#21 (1) {
["info":"Google\Cloud\NaturalLanguage\Annotation":private]=>
array(3) {
["documentSentiment"]=>
array(2) {
["magnitude"]=>
float(1.4)
["score"]=>
int(0)
}
["language"]=>
string(2) "en"
["sentences"]=>
array(2) {
[0]=>
array(2) {
["text"]=>
array(2) {
["content"]=>
string(19) "I love everything!\"
["beginOffset"]=>
int(0)
}
["sentiment"]=>
array(2) {
["magnitude"]=>
float(0.8)
["score"]=>
float(0.8)
}
}
[1]=>
array(2) {
["text"]=>
array(2) {
["content"]=>
string(18) "I hate everything!"
["beginOffset"]=>
int(21)
}
["sentiment"]=>
array(2) {
["magnitude"]=>
float(0.6)
["score"]=>
float(-0.6)
}
}
}
}
}
ADDED:
The very last bit of my PHP code is:
$annotation = $language->analyzeSentiment($text);
$sentiment = $annotation->sentiment();
echo 'Text: ' . $text . '
Sentiment: ' . $sentiment['score'] . ', ' . $sentiment['magnitude'];
return $sentiment;
This successfully returns the score and magnitude for the overall "document" as shown in the part of the array under "documentSentiment". What I need (in addition to this), is the data under sentences. In particular, content, magnitude and score.
Since $sentiment['sentences'] is an array -
["sentences"]=>
array(2) {
...
}
you need to loop over the values, with foreach() for example -
....
echo 'Text: ' . $text . '
Sentiment: ' . $sentiment['score'] . ', ' . $sentiment['magnitude'];
foreach($sentiment['sentences'] as $sentence){
echo 'Text: ' .$sentence["text"]["content"] . '
Sentiment: ' . $sentence["sentiment"]["score"] . ', ' . $sentence["sentiment"]["magnitude"];
}

Parse, format, and store as variable results from query

In PHP, I get this back:
array(1096) {
[0]=>
array(2) {
["exuid"]=>
string(36) "c056b5ce-3b0c-4494-858a-cf184b904dc3"
[0]=>
string(36) "c056b5ce-3b0c-4494-858a-cf184b904dc3"
}
[1]=>
array(2) {
["exuid"]=>
string(36) "8a6262c6-a4e0-41a4-8a47-ecaf5f79c2d5"
[0]=>
string(36) "8a6262c6-a4e0-41a4-8a47-ecaf5f79c2d5"
}
[2]=>
array(2) {
["exuid"]=>
string(36) "728cb9b6-6240-470f-87d5-706af554cd0b"
[0]=>
string(36) "728cb9b6-6240-470f-87d5-706af554cd0b"
}
[3]=>
array(2) {
["exuid"]=>
string(36) "a26d0fdd-9a9b-4611-8c41-3d2c9b012988"
[0]=>
string(36) "a26d0fdd-9a9b-4611-8c41-3d2c9b012988"
}
ETC
What I need to end up with is defining a variable that contains all the exuid returned separated by commas. Like this:
$something = 'c056b5ce-3b0c-4494-858a-cf184b904dc3', '8a6262c6-a4e0-41a4-8a47-ecaf5f79c2d5', '728cb9b6-6240-470f-87d5-706af554cd0b', 'a26d0fdd-9a9b-4611-8c41-3d2c9b012988'
I need it in this format to push into another query:
where `exuid` in ($something)
But I can't parse this correctly. If I do var dump($result) I get the array shown above. If I do var dump($result[0]['exuid']) I get the first value back (c056b5ce-3b0c-4494-858a-cf184b904dc3), but not the others. How do I parse this and format it in the way I need it?
Just extract the column and then join:
$something = "'" . implode("','", array_column($result, 'exuid')) . "'";
If you don't have PHP >= 5.5.0 then use this in place of array_column() :
array_map(function($v) { return $v['exuid']; }, $result)
Here is the PHP code:
<?php
$exuid = array();
foreach($result as $k=>$v)
{
$exuid[] = $v['exuid'];
}
$exuid_str = "'" . implode($exuid, "','") . "'";
?>

Loop through an array returned by vafpress

I know how to perform a normal foreach loop but I can't seem to work it for an array returned by the vafpress framework. I am using var_dump(vp_metabox('vp_meta_sample_2.binding_group')); which is generating the below mentioned array. How can I loop through all the images!
(1) { [0]=> array(4) { ["name"]=> string(1) "1" ["url"]=> string(10) "234234.com" ["image"]=> string(62) "http://localhost/wp/wp-content/uploads/2014/02/bottomright.jpg" ["shortcode"]=> string(108) "[shortcode name="1" url="234234.com" image="http://localhost/wp/wp-content/uploads/2014/02/bottomright.jpg"]" } }
I am using the following loop to get the values
$age=print_r (vp_metabox('vp_meta_sample_2.binding_group'));
print_r ($age);
foreach($age as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
In this statement:
$age=print_r (vp_metabox('vp_meta_sample_2.binding_group'));
from the code above, $age will be empty since print_rdoes not return anything.
Proper:
$age= vp_metabox('vp_meta_sample_2.binding_group');
Try this, without print_r()
$age=vp_metabox('vp_meta_sample_2.binding_group');
instead of
$age=print_r (vp_metabox('vp_meta_sample_2.binding_group'));

PHP Echo array values loop (no data displayed)

I have an array ... Here is the structure / data:
array(1) {
[0]=> object(SimpleXMLElement)#1 (18)
{
["data_123"]=> object(SimpleXMLElement)#3 (29)
{
["field1"]=> string(7) "123"
["field2"]=> string(2) "10"
["field3"]=> string(19) "2013-03-05 17:00:00"
["field4"]=> string(19) "2013-03-05 18:00:00"
}
["data_234"]=> object(SimpleXMLElement)#4 (29)
{
["field1"]=> string(7) "234"
["field2"]=> string(2) "10"
["field3"]=> string(19) "2013-03-05 17:40:00"
["field4"]=> string(19) "2013-03-05 18:10:00"
}
}
}
I am trying to create a loop to display the data but nothing is showing up:
foreach ($result as $key => $list) {
echo "key.: " . $key . "\n";
echo "field1: " . $list['field1'] . "\n";
echo "field2: " . $list['field2'] . "\n";
}
It's just not returning any data.
I'm guessing that the loop might be wrong for this array structure?
How can I get the data echoed for this array?
$list is an array of objects so you need two loops and appropriate syntax. e.g.:
foreach($list as $objects) {
foreach($objects as $key => $obj) {
echo "key.: " . $key . "\n";
echo $obj->field1 . "\n";
echo $obj->field2 . "\n";
echo $obj->field3 . "\n";
echo $obj->field4 . "\n";
}
}

Categories