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

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

Related

How to loop through multidimensional array?

I'm currently getting a JSON response from a company's API and converting it into a PHP array like this:
$api_url = file_get_contents('http://example.com');
$api_details = json_decode($api_url, true);
When I run var_dump on $api_details, I am getting this:
array(2) {
["metadata"]=>
array(5) {
["iserror"]=>
string(5) "false"
["responsetime"]=>
string(5) "0.00s"
["start"]=>
int(1)
["count"]=>
int(99999)
}
["results"]=>
array(3) {
["first"]=>
int(1)
["result"]=>
array(2) {
[0]=>
array(4) {
["total_visitors"]=>
string(4) "3346"
["visitors"]=>
string(4) "3249"
["rpm"]=>
string(4) "0.07"
["revenue"]=>
string(6) "0.2381"
}
[1]=>
array(4) {
["total_visitors"]=>
string(6) "861809"
["visitors"]=>
string(6) "470581"
["rpm"]=>
string(4) "0.02"
["revenue"]=>
string(7) "13.8072"
}
}
}
}
I'm trying to do 2 things and can't figure out how to do either with a multidimensional array.
I need to check to see if metadata > iserror is false. If it is not false, I want to show an error message and not continue with the script.
If it is false, then I wants to loop through the results of results > result and echo the total_visitors, visitors, etc for each of them.
I know how to echo data from array, I guess I'm just getting confused when there's multiple levels to the array.
Anyone that can point me in the right direction would be much appreciated :)
You can iterate over arrays using foreach. You can read up on it here: http://php.net/manual/en/control-structures.foreach.php
Since you're using associative arrays, your code will look something like this:
if ($arr['metadata']['iserror']) {
// Display error here
} else {
foreach($arr['results']['result'] as $result) {
echo $result['total_visitors'];
echo $result['visitors'];
}
}
You'll have to tweak the code to fit exactly what you're doing, but this should get you over the line.
Hope that helps!

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

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;

Php array output confusion

In PHP when I do
var_dump($galleryCategoriesThumb);
Output is
array(1) {
[0]=>
array(1) {
["Gallery"]=>
array(3) {
["id"]=>
string(3) "190"
["photofile"]=>
string(6) "50.jpg"
["gallery_category_id"]=>
string(2) "58"
}
}
}
When I do
var_dump($galleryCategoriesThumb["photofile"]);
I get NULL output. I tried various other options also.
All I want to do is echo ["photofile"]. Please help.
Try $galleryCategoriesThumb[0]['Gallery']["photofile"]
Try var_dump($galleryCategoriesThumb[0]["Gallery"]["photofile"]);.
It's a 3 dimensions array.
It is expected that var_dump($galleryCategoriesThumb["photofile"]) returns NULL because "photofile" key does not exist in $galleryCategoriesThumb.
To access "photofile", you need a 'full path' in the array as posted by others
var_dump($galleryCategoriesThumb[0]["Gallery"]["photofile"]);.

A list of arrays contained in a master array PHP

I hope I can make this question clear enough.
I'm looking to put a list of arrays inside one master array, dynamically, so that it looks like this:
masterarray {
array1
{ [0]=>VAL1 [1]=>VAL2 }
array2
{ [0]=>VAL1 [1]=>VAL2 }
array3
{ [0]=>VAL1 [1]=>VAL2 }
}
I've tried, but I could only get it to look like this:
array(1) { [0]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } }
array(2) { [0]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } [1]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } }
array(3) { [0]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } [1]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } [2]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } }
And that's definitely not what I'm aiming for. Nothing seems contained. I need the format specified above.
I'm using the explode function on a string pulled from a file to make this table of arrays (I think you call it that)
Here is the code I'm using that's not working.
$variabledebugging = file("FILE.TXT");//LOOK IN THIS FILE FOR THE NUMBER AND SET IT TO A VAR.
$i=0;
foreach($variabledebugging as $placeholder){
$variabledebuggingtbl[] = explode("\t",$variabledebugging[$i]);
var_dump($variabledebuggingtbl);
$i++;
}
I've tried a couple of different variations, but that's the one I'm using now.
To be clear, that file being pulled (each line as a value in an array) has 2 things written to each line, separated by a tab character, so that's the system I'm going on.
Thank you! I'm sure this is a simple task, I just can't think it through.
Oh and while I'm at is there a way to make debugging more readable?
You ARE getting the right result. The reason it seems wrong is that you are running var_dump inside the loop. And why don't you use the $placeholder variable?
$variabledebugging = file("FILE.TXT");
foreach($variabledebugging as $placeholder){
$variabledebuggingtbl[] = explode("\t", $placeholder);
}
var_dump($variabledebuggingtbl);
I'm not sure what you mean by "making debugging more readable", but if you want some linebreaks and indentation you should just look in the generated HTML code. var_dump do add spacing to make it readable but it is ignored by the web browser. If you don't want to read the HTML source, just add your var_dump to a <pre> element.

json_decode variables php

I'm simply trying to extract variables from a json_decode
the resulting decoded code (a snippet of) is:
auth_info:
array(4) {
["profile"]=>
array(13) {
["name"]=>
array(3) {
["givenName"]=>
string(6) "John"
["familyName"]=>
string(7) "Doe"
["formatted"]=>
string(14) "John Doe"
}
}
}
I'm trying all things such as:
echo "\n\nMy name is ".$auth_info['profile']->name->givenName;
But all to no avail..ideas please?
Many thanks as always
Darren
They are all arrays, try this.
$auth_info['profile']['name']['givenName']

Categories