How to echo out a result from this var_dump()? - php

This seems like it should be easy but I'm clearly just misunderstanding something. I'm making an API call to 5 Day Weather with the following code:
$file = file_get_contents("http://5DayWeather.org/api.php?city=Glasgow");
$weather = json_decode($file);
If i var_dump($weather) I get the following output:
object(stdClass)#1824 (2) {
["apiVersion"]=> string(3) "1.0"
["data"]=> object(stdClass)#1826 (7) {
["location"]=> string(12) "Glasgow, GBR"
["temperature"]=> string(2) "55"
["skytext"]=> string(13) "Partly Cloudy"
["humidity"]=> string(2) "88"
["wind"]=> string(1) "8"
["date"]=> string(10) "2014-10-03"
["day"]=> string(6) "Friday"
}
}
Right, that's all well and good, but how can I echo out single results? I feel like I've tried everything and just can't get anything to work, including (many for good measure):
$weather->temperature;
$weather['temperature'];
$weather{'temperature'};
I appreciate that it's my fundamental misunderstanding of how this array/object thing works but I've tried looking into it and can't figure out exactly what I should be doing with this feed.
Thanks a lot!

Try this:
echo $weather->data->temperature;

Related

PHP get specific value of a specific key in a multidimensional array

Say I have the following array (this comes from a var_dump):
($defaults = )array(3) {
["sitewide_typography_title"]=>
array(2) {
["font-family"]=>
string(16) "Playfair Display"
["variant"]=>
string(7) "regular"
}
["sitewide_typography_text"]=>
array(2) {
["font-family"]=>
string(6) "Roboto"
["variant"]=>
string(3) "300"
}
["sitewide_typography_btn"]=>
array(2) {
["font-family"]=>
string(6) "Roboto"
["variant"]=>
string(3) "300"
}
}
I guess this is an easy question, but I really can't find the answer. I think my googling skills failed me in this, but how do I get the font-family value for sitewide_typography_title?
Thanks a lot in advance!
It's simple
$defaults['sitewide_typography_title']['font-family'];
To echo it out like this
echo $defaults['sitewide_typography_title']['font-family'];
Should output
Playfair Display

PHP: Incorrect value after assignment

I have really no idea what's happening. The story:
I use PDO for a SELECT statement on a database.
$sql = "SELECT a,b,c,performance,points,compare
FROM normvalues
WHERE x=:x AND y=1 AND z=:z";
$stmt = $GLOBALS['PDO']->prepare($sql);
$stmt->bindParam(":x",$x);
$stmt->bindParam(":z",$z);
$stmt->execute();
$res=$stmt->fetchAll(PDO::FETCH_ASSOC);
So that's fine and it is working. When I var_dump the $res variable I get something like:
array(6) {
["a"]=> string(2) "44"
["b"]=> string(4) "1176"
["c"]=> string(4) "1166"
["performance"]=> string(4) "50.1"
["points"]=> string(1) "1"
["compare"]=> string(2) "-1"
}
[1]=>
array(6) {
["a"]=> string(2) "57"
["b"]=> string(4) "1176"
["c"]=> string(4) "1166"
["performance"]=> string(4) "47.7"
["points"]=> string(1) "2"
["compare"]=> string(2) "-1"
}
[2]=>
array(6) {
["a"]=> string(2) "70"
["b"]=> string(4) "1176"
["c"]=> string(4) "1166"
["performance"]=> string(4) "44.7"
["points"]=> string(1) "3"
["compare"]=> string(2) "-1"
}
...
That's also okay. But I have to sort the result another way. So I am doing:
foreach($res as $e){
$this->normTable[$e['a']][$e['points']]=$e['performance'];
$this->normTable[$e['a']]['compare']=$e['compare'];
}
And now I am completely lost. By assigning $e['performance'] I get wrong values. Actually this should be the performance values.
[1176]=>
array(4) {
[1]=> string(2) "50"
["compare"]=> string(2) "-1"
[2]=> string(2) "48"
[3]=> string(2) "45"
}
I already checked the values in the database and they are correct. By doing doubleval() I'd get the right values, but the problem is that not every value is a double but also integer or string. I also tried to typecast with (string) but it's the same result. I have no explanation.
Update:
It's a very big project and I just tried to minimize it as possible and to make my problem as clear as possible. But now I have figured out something new:
I do an 'echo()' of my first variable in the normTable during the loop:
foreach($res as $e){
$this->normTable[$e['a']][$e['points']]=$e['performance'];
echo "a:".$e['a']." pt: ".$e['points']." perf: ".$e['performance']."-".$this->normTable[1176][1]."\n";
$this->normTable[$e['a']]['compare']=$e['compare'];
}
and the value is changing from '50.1' to '50'. Still can't figure out the reason. Is there a size limitaion of arrays in PHP?
UPDATE 2 and a big SORRY!
As I said, it is a big project. So the table I read out, has some values for some attributes twice or more. Actually such a case should not happen. That's why the answer is simple: It became 50 because 50 was assigned. I'm so sorry for having waisted your time. But I totally excluded this case and since I am also coding in C, my first thought was: memory leak - clear case!
Thanks for your help.
Meh, I don't have 50 reputation; can't comment only answer.
If you replace a with be in your loop, you should get your expected result (given your data sample). I.e.:
foreach($res as $e){
$this->normTable[$e['b']][$e['points']]=$e['performance'];
$this->normTable[$e['b']]['compare']=$e['compare'];
}
But I'm not sure this really solves your problem. YMMV.

How to order results from CouchbaseViewQuery on Couchbase PHP SDK 2.0?

I started studying both Couchbase and MongoDB to decide wich one to implement on a social network, but the lack of documentation on couchbase side is almost making me give up.
Almost everything I must guess, as documentation are poor, and easier to get confuse between PHP SDK 2.0 and previous versions. There is a lot of documentation but about older sdk versions.
http://docs.couchbase.com/sdk-api/couchbase-php-client-2.0.2/index.html
Now after my outflow, my question.
I have this code, and the necessary view created:
$cb = CouchbaseViewQuery::from('dev_testimonials', 'by_uid')->key($uid)->limit($max)->skip($inicio);
It works as expected, except that I need to order the results by ascending or descending, but I could't find anywhere documentation about that. I thought ->descending(true) should do the trick but doesn't work. Doesn't exist.
All that the API reference says about ordering on CouchbaseViewQuery, is a list of constants:
UPDATE_BEFORE, UPDATE_NONE, UPDATE_AFTER, ORDER_ASCENDING, ORDER_DESCENDING
But there is not explanation about how and where to use them.
Could you help? Thanks.
The function you need to use is order() which accepts one of two constants:
ORDER_ASCENDING
ORDER_DESCENDING
In php all class constants are publicly visible. To access the constants the following code can be used: CouchbaseViewQuery::ORDER_ASCENDING or CouchbaseViewQuery::ORDER_DESCENDING.
Below is a code example using the Beer-sample data shipped with Couchbase Server.
<?php
// Connect to Couchbase Server
$cluster = new CouchbaseCluster('http://127.0.0.1:8091');
$bucket = $cluster->openBucket('beer-sample');
$query = CouchbaseViewQuery::from('beer', 'by_location')->skip(6)->limit(2)->reduce(false)->order(CouchbaseViewQuery::ORDER_ASCENDING);
$results = $bucket->query($query);
foreach($results['rows'] as $row) {
var_dump($row['key']);
}
echo "Reversing the order\n";
$query = CouchbaseViewQuery::from('beer', 'by_location')->skip(6)->limit(2)->reduce(false)->order(CouchbaseViewQuery::ORDER_DESCENDING);
$results = $bucket->query($query);
foreach($results['rows'] as $row) {
var_dump($row['key']);
}
Here is the output from the above code:
array(3) {
[0]=>
string(9) "Australia"
[1]=>
string(15) "New South Wales"
[2]=>
string(6) "Sydney"
}
array(3) {
[0]=>
string(9) "Australia"
[1]=>
string(15) "New South Wales"
[2]=>
string(6) "Sydney"
}
Reversing the order
array(3) {
[0]=>
string(13) "United States"
[1]=>
string(7) "Wyoming"
[2]=>
string(8) "Cheyenne"
}
array(3) {
[0]=>
string(13) "United States"
[1]=>
string(7) "Wyoming"
[2]=>
string(6) "Casper"
}

PHP return object variables inside of an array

I'm writing a PHP script to work with some JSON data. Below is an (abridged) var_dump($data). I want to return the value associated with ["[question(13), option(0)]"] which is 20. I can't figure out how to do it. I've tried $data->[question(13), option(0)] and $data->question(13). (I tried to look this up but I'm not sure what the notation means, so I'm not sure what I'm looking for)
object(stdClass)#133 (36) {
["id"]=>
string(1) "1"
["contact_id"]=>
string(0) ""
["status"]=>
string(8) "Complete"
["is_test_data"]=>
string(1) "0"
["datesubmitted"]=>
string(19) "2012-04-19 17:11:00"
["[question(5)]"]=>
string(11) "C. 40%, 40%"
["[question(9)]"]=>
string(47) "D. EBITDA and Free cash flow are the same thing"
["[question(10)]"]=>
string(48) "A. Accounts Payable as % of sales would increase"
["[question(11)]"]=>
string(20) "E. None of the above"
["[question(12)]"]=>
string(97) "A. A larger portion of initial investment is equity which can increase exit return potential."
["[question(13), option(0)]"]=>
string(2) "20"
["[url("embed")]"]=>
string(0) ""
["[variable("STANDARD_IP")]"]=>
string(13) "38.107.74.230"
["[variable("STANDARD_LONG")]"]=>
string(10) "-73.976303"
["[variable("STANDARD_LAT")]"]=>
string(9) "40.761902"
}
Either use extended object access notation:
$data->{'[question(13), option(0)]'}
Or just ask for normal array and use it as normal array.
json_decode($json, true);
try this
echo $data->{'[question(13), option(0)]'};

Targeting specific group when creating a campaign with mailChimp API

I cannot seem to figure out how to send to a group in my list using mailChimp API
My code looks like this:
$conditions = array('field'=>'interests-1', 'op'=>'all', 'value'=>'myGroup');
$opts = array('match'=>'any', 'conditions'=>$conditions);
$retval = $api->campaignSegmentTest($listId, $opts);
But this yields bool(false). When fetched by
$retval = $api->listInterestGroupings($listId);
my list looks like this:
array(1) {
[0]=>
array(5) {
["id"]=>
int(1)
["name"]=>
string(10) "myList"
["form_field"]=>
string(5) "radio"
["display_order"]=>
string(1) "0"
["groups"]=>
array(5) {
[0]=>
array(4) {
["bit"]=>
string(1) "1"
["name"]=>
string(9) "myGroup"
["display_order"]=>
string(1) "1"
["subscribers"]=>
int(1)
}
[1]=>
array(4) {
["bit"]=>
string(1) "2"
["name"]=>
string(9) "myGroup_2"
["display_order"]=>
string(1) "2"
["subscribers"]=>
int(1)
}
}
}
}
I have looked in the API documentation and searched for the answer, but cannot seem to figure it out. Grateful for help!
Looks like you are using the PHP wrapper - the first thing to do, like the examples included with it do, is to check for any errors by looking at $api->errorCode before messing with the $retval.
When you do that I'm certain you will see an error telling you that you haven't passed a proper "conditions" parameter since it is an array of arrays, not an array.

Categories