I'm building a php site and need some of the functionality provided by the blockchain API (https://github.com/blockchain/api-v1-client-php)
I'm trying to print out an overview of all transactions done to a specific address, but no success so far.
I've gathered the info of the address, but the transactions are stored in an array (as written in the documentations), and can't get them out.
$limit = 50;
$offset = 0;
$address = "xxx";
$address_info = $Blockchain->Explorer->getAddress($address, $limit, $offset);
echo $address_info->n_tx; //just as a test, this works
$transactions = $address_info->transactions; //no error here
echo $transactions->version;
The last line of code throws this error: "Trying to get property of non-object". echo $transactions[0] also doesn't work.
The github page doesn't feature any examples of printing out transactions.
The var_dump function of $transactions produces this:
array (size=2)
0 =>
object(Blockchain\Explorer\Transaction)[11]
public 'double_spend' => boolean false
public 'block_height' => int 382334
public 'time' => int 1446833376
public 'lock_time' => int 0
public 'relayed_by' => string '192.99.2.32' (length=11)
public 'hash' => string 'd9f625afe46ea8bbe9dc74484cefbcb15fbd6887a1bc619b44161114b78ab038' (length=64)
public 'tx_index' => int 109866616
public 'version' => int 1
public 'size' => int 374
public 'inputs' =>
array (size=2)
0 =>
object(Blockchain\Explorer\Input)[12]
...
1 =>
object(Blockchain\Explorer\Input)[13]
...
public 'outputs' =>
array (size=2)
0 =>
object(Blockchain\Explorer\Output)[14]
...
1 =>
object(Blockchain\Explorer\Output)[15]
...
Any ideas?
$transactions is a PHP array, not an object. You can access the version of the first object in the array using $transactions[0]->version, or iterate through the array using something like foreach ($transaction in $transactions) { ... }.
Related
I've got told many times, if there is a new question even on the same code to just create a new thread so here I am. Thanks to the guys for helping me with the previous question.
I have the following code:
/* Return an array of _octopus_ids */
$offices = array_map(
function($post) {
return array(
'id' => get_post_meta($post->ID, '_octopus_id', true),
);
},
$query->posts
);
/* Dump out all the multi-dimensional arrays */
var_dump($offices);
$test = array_column($offices, 'id');
var_dump($test);
var_dump($offices) dumps the following:
array (size=10)
0 =>
array (size=1)
'id' => string '1382' (length=4)
1 =>
array (size=1)
'id' => string '1330' (length=4)
var_dump($test) dumps the following:
array (size=10)
0 => string '1382' (length=4)
1 => string '1330' (length=4)
Problem:
How can I use the following code:
$results = $octopus->get_all('employees/' . $test; which results in an Notice: Array to string conversion error.
I want to be able to make a results call such as this $results = $octopus->get_all('employees/1382'); - So I want just the numeric string of $test to be appended to the end of employees/
If I hardcode the 1382 after employees/, I get the following result:
object(stdClass)[1325]
public 'id' => int 1382
What's the proper way to array of strings into just strings?
Hope you guys can help me... Still new at PHP and I am struggling to display parts of this Object/Array set of Results.
I am getting the following result $results back from a SOAP webservice:
`object(stdClass)[9]
public 'Summary' =>
object(stdClass)[2]
public 'ID' => string '1096408402' (length=10)
public 'IKey' => string '1440010962' (length=10)
public 'Address' =>
object(stdClass)[4]
public 'Forename' => string 'TEST' (length=4)
public 'Surname' => string 'TESTER' (length=6)
public 'DOB' => string '0000-00-00' (length=10)
public 'Telephone' => string 'Unavailable' (length=11)
public 'Occupants' =>
array (size=3)
0 =>
object(stdClass)[12]
...
1 =>
object(stdClass)[13]
...
2 =>
object(stdClass)[14]
...
3 =>
object(stdClass)[15]
...
Now I am attempting to put the data into a table format.
I have been successful in creating the table using a foreach on the section marked Occupants. I do this by calling Occupants as follows:
$occupants = ($results->Address->Occupants); and the data is extracted and populated into my table using my code (not relevent for this question).
My problem now is that when I try and do the same for Summary or Address it doesnt work: I get the error "Trying to get property of non-object"
I have tried $summary = $results->Summary and $summary = $results['Summary'] and neither works.
What I then want to do is run
<?php $summary = ($results->Summary);foreach($summary as $person):?>
and then I insert it into my table as follows:
<td><?=$person->ID?></td>
So any idea why I get this error? I dont think it is in the foreach aspect...?
Normally, you should get the "Summary" object with:
$summary = $results->Summary
in this case $summary is an object with 2 properties: "ID" and "IKey".
If you iterate over $summary with foreach, the value of $person would have the value of $summary->ID in the first loop iteration and the value of $summary->IKey in the second loop iteration. Both $summary->ID and $summary->IKey are strings and therefore non-objects, so I think that is why you get the error.
I suppose that you want do do this:
$summary = $results->Summary;
foreach ($summary as $value)
echo "<td>$value</td>";
This should output (for the given example):
<td>1096408402</td><td>1440010962</td>
For more information about Object Iteration, I recommend: http://php.net/manual/en/language.oop5.iterations.php
This question already has answers here:
Convert laravel object to array
(14 answers)
Closed 5 years ago.
i queried a DB like this which got me an array:
foreach($oid as $orderid) {
$orderdetailData[] = DB::table('order_details')
->join('orders', 'order_details.oid', '=', 'orders.oid')
->select('order_details.oid', 'orders.ostatus')
->where('order_details.oid', $orderid)->get();
}
$data = array_flatten($orderdetailData);
return $data;
This is the array i get
array (size=2)
0 =>
object(stdClass)[174]
public 'oid' => int 1
public 'ostatus' => string 'Placed' (length=6)
1 =>
object(stdClass)[158]
public 'oid' => int 2
public 'ostatus' => string 'Placed' (length=6)
I am trying to get this array in the form
array (size=2)
0 =>
array (size=2)
public 'oid' => int 1
public 'ostatus' => string 'Placed' (length=6)
1 =>
array (size=2)
public 'oid' => int 2
public 'ostatus' => string 'Placed' (length=6)
I tried doing this:
foreach($orderdetailData as $key => $value){
$data[] = array_flatten($orderdetailData[$key]);
}
But doing this gets me an array in this form:
array (size=2)
0 =>
array (size=1)
0 =>
object(stdClass)[174]
public 'oid' => int 1
public 'ostatus' => string 'Placed' (length=6)
1 =>
array (size=1)
0 =>
object(stdClass)[158]
public 'oid' => int 2
public 'ostatus' => string 'Placed' (length=6)
Which is not i what i am looking for. Can someone tell me what would be an easy way to do this ? Thanks
Using array_map and casting to an array should be sufficient:
$data = array_map(function($object){
return (array) $object;
}, $data);
I also wouldn't run queries inside a loop. You should try getting that data in one query from the db. Something like this could work:
$data = DB::table('order_details')
->join('orders', 'order_details.oid', '=', 'orders.oid')
->select('order_details.oid', 'orders.ostatus')
->whereIn('order_details.oid', $oid)->get();
Edit
As it has been mentioned in another answer shortly let me explain how you can accomplish the same by setting PDO to FETCH_ASSOC:
DB::setFetchMode(PDO::FETCH_ASSOC);
$data = DB::table('order_details') .... ->get();
However this changes the fetch mode globally for the rest of the request (at least if you don't open a new connection). To be save you should change it back afterwards:
DB::setFetchMode(PDO::FETCH_ASSOC);
$data = DB::table('order_details') .... ->get();
DB::setFetchMode(PDO::FETCH_CLASS);
Or even back it up first if you can't be sure what default is used:
$fetchModeBefore = DB::getFetchMode();
DB::setFetchMode(PDO::FETCH_ASSOC);
$data = DB::table('order_details') .... ->get();
DB::setFetchMode($fetchModeBefore);
On the PDO/DB object you can set the return style to assoc.
I use an api of League Of Legends, i would get the summoners stats.
I am using an API of League of Legends to get summoners stats.
The code:
object(stdClass)[3]
public 'summonerId' => int XXXXXXXXXXX
public 'playerStatSummaries' =>
array (size=15)
0 =>
object(stdClass)[4]
public 'playerStatSummaryType' => string 'AramUnranked5x5' (length=15)
public 'wins' => int 100
public 'modifyDate' => float 1412611932000
public 'aggregatedStats' =>
object(stdClass)[5]
...
I want to get data ONLY when the value of "Playerstatsummarytype" is "RankedSolo5x5".
I've been trying to use this:
$win_ranked = $parsed_json2->{'playerStatSummaries'}[8]->{'wins'};
$losses_ranked = $parsed_json2->{'playerStatSummaries'}[8]->{'losses'};
But with certain summoners, sometimes the number is 8 or 9.
So how do I get data ONLY when "Playerstatsummarytype" is RankedSolo5x5.
Thanks =)
So it looks like you simply need to filter the array at $parsed_json2->playerStatSummaries.
That should be simple:
$filtered_array = array_filter($parsed_json2->playerStatSummaries, function($item) {
return ($item->playerStatSummaryType === 'RankedSolo5x5');
});
// now you can get at wins/losses for items in filtered array like:
foreach ($filtered_array as $obj) {
echo 'Wins: ' . $obj->wins;
echo 'Losses: ' . $obj->losses;
}
I have the following code:
$b = $br->b;
var_dump($b);
$iCountBlock = count($b);
Where b is a SimpleXMLElement object. The var dump outputs:
object(SimpleXMLElement)[16]
public 'blockID' => string '160999' (length=6)
public 'blockDesc' => string 'Description' (length=37)
public 'moduleID' => string '1' (length=1)
public 'pubID' =>
object(SimpleXMLElement)[18]
public 'contentID' => string '93305' (length=5)
public 'linkToPageID' =>
object(SimpleXMLElement)[19]
public 'moduleFunction' => string 'replaceHTML' (length=11)
public 'moduleVars' =>
object(SimpleXMLElement)[20]
public 'c' =>
object(SimpleXMLElement)[21]
public 'contentID' => string '93305' (length=5)
public 'contentType' => string '1' (length=1)
public 'description' => string 'new.usdish.com index redesign content' (length=37)
public 'content' =>
object(SimpleXMLElement)[22]
However, $iCountBlock gets set to 1... it doesn't appear to be counting all the public properties of the object as it should. I also tried using a foreach loop to loop over each property of b and it didn't even enter the loop.
foreach($b as $key => $val) { ... }
I'm kinda at a loss here, as I'm not sure what's going on. Any thoughts?
Form PHP 5.3 and higher SimpleXMLElement does use a count function for the length!
$count = $b->count();
In PHP before 5.3 you have to use the childern property for getting the count.
$count = count($b->children());
Info at: http://php.net/manual/en/simplexmlelement.count.php