Php array output confusion - php

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"]);.

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

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!

Remove keys from multidimesional array

I have an array name $json_output.
array(3) {
["ProductsSummary"]=>
array(2) {
["TotalPages"]=>
int(2)
["CurrentPage"]=>
int(1)
}
["Products"]=>
array(60) {
[0]=>
array(3) {
["LastShopUpdate"]=>
string(26) "/Date(1382716320000+0200)/"
["Score"]=>
float(0.2208696)
["ProductId"]=>
int(1306413101)
["ArticleNumber"]=>
}
[1]=>
array(3) {
["LastShopUpdate"]=>
string(26) "/Date(1382716320000+0200)/"
["Score"]=>
float(0.2208696)
["ProductId"]=>
int(1306413101)
["ArticleNumber"]=>
}
And so on. I need to unset ProductId and LastShopUpdate from each one.
What i tried:
<?php
foreach($json_output["Products"] as $bla)
unset($bla['ArticleNumber'], $bla['LastShopUpdate']);
?>
But it is not working. How could I do this?
When looping over an array using foreach, a copy is usually made. Changing something in the copy of course has no effect on the original. Try this:
foreach($json_output["Products"] as & $bla)
unset($bla['ArticleNumber'], $bla['LastShopUpdate']);
The & causes $bla to be a reference instead of a copy. Therefore it should resolve your problem.

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.

Nested Variables in PHP

I think I am either just stupid or something but I still can't get my head around them.
I am trying to access "patient_age" from this variable $result.
Here is the var dump.
array(1) {
["intervention"]=>
array(1) {
[0]=>
object(stdClass)#23 (21) {
["intervention_id"]=>
string(1) "1"
["patient_id"]=>
string(1) "1"
["name_id"]=>
string(1) "1"
["department_id"]=>
string(1) "1"
["dosage_id"]=>
NULL
["edocument"]=>
string(10) "Bruce1.jpg"
["user_id"]=>
string(1) "0"
["duration"]=>
string(8) "02:26:00"
["submitted"]=>
string(19) "2011-07-31 19:56:29"
["intervention_comment"]=>
NULL
["patient_age"]=>
string(2) "34"
["patient_height"]=>
string(4) "1.34"
["patient_weight"]=>
string(2) "45"
["patient_gender"]=>
string(4) "Male"
["department_name"]=>
string(10) "Cardiology"
["intervention_name_id"]=>
string(1) "1"
["intervention_name"]=>
string(5) "IVH 2"
["intervention_description"]=>
string(0) ""
["dosage_emitted"]=>
NULL
["dosage_absorbed"]=>
NULL
["dosage_period"]=>
NULL
}
}
}
I have tried :
$result[0]->patient_age;
$result[1]->patient_age;
$result['intervention']->patient_age;
$result['intervention'][0]->patient_age;
Hopefully someone could give me the answer but also explain how they came to this answer as all the other Stackoverflow questions they just give the solution but not the method.
Anyone got any tips how to navigate nested variables.
Thanks
$object=$result['intervention'][0];
print $object->patient_age;
Check if any other variables are accessable
It should be your last example. It's not that hard, really. $result is an array which contains a single element, with the key "intervention". You can access an array's elements by using [ and ]. So, with $result['intervention'], you get an array that also contains a single element: the element at key 0, which is an instance of stdClass. You can reach that by using $result['intervention'][0]. If you want to get the patient_age from that stdClass, you can access the instance variables with the ->. So, this should work:
echo $result['intervention'][0]->patient_age;
The following would result in $patient being the stdClass instance, which you can then retrieve patient_age from:
$patient = $result['intervention'][0];
echo $patient->patient_age;
$result[0]->patient_age;
will work *as long as patient_age is a public variable*. If it's private or protected you'll need to use a method within the object to access it.
You never said what happened with the stuff you tried. Null values? Error/warning messages?
Proof:
<?php
class iv {
var $patient_age;
function __construct($val)
{
$this->patient_age=$val;
}
}
$t=new iv(40);
$result=array(0=>$t);
var_dump($result) . "\n\n";
print "val = " . $result[0]->patient_age . "\n\n";
[user#example ~]$ php -q t.php
array(1) {
[0]=>
object(iv)#1 (1) {
["patient_age"]=>
int(40)
}
}
val = 40
[user#example ~]$

Categories