How do I get a particular value in a multidimensional array? - php

How do I prase this?
$scope = $facebook->api('/me/permissions','GET');
The result is as follows and I want to get the value of installed:
array(1) { ["data"]=> array(1) { [0]=> array(5) { ["installed"]=> int(1) ["offline_access"]=> int(1) ["email"]=> int(1) ["manage_pages"]=> int(1) ["user_about_me"]=> int(1) } } }
I've tried json_decode($scope, true), $scope['installed'], $scope['data']['installed'] etc. What am I missing?

That is a super nested array - Your $scope['data']['installed'] was close. However, you forgot one layer. It should be $scope['data'][0]['installed']. Note the 0 in there - there is a third level.
Accessing any of the scope will start with $scope['data'][0], so I would assign that to a new var to remove those two layers.
$scope = $scope['data'][0]
Then, all you need is the key for the permission

Try $scope['data'][0]['installed']

Related

PHP acess value of array using key of 2D array

I'm working on a php magento script which have a array variable for store some script urls.
array variable $items['js']
var_dump
array(1) {
[""]=>
array(17) {
["prototype/prototype.js"]=>
string(22) "prototype/prototype.js"
["varien/form.js"]=>
string(14) "varien/form.js"
["mage/translate.js"]=>
string(17) "mage/translate.js"
["mage/cookies.js"]=>
string(15) "mage/cookies.js"
["wyomind/layer/native.history.js"]=>
string(31) "wyomind/layer/native.history.js"
["varien/weee.js"]=>
string(14) "varien/weee.js"
["geissweb/vatvalidation-min.js"]=>
string(29) "geissweb/vatvalidation-min.js"
}
}
I tried to access the "geissweb/vatvalidation-min.js" value like this
$items['js']['geissweb/vatvalidation-min.js']
but it return empty value, is there have a way to get that value without use foreach or for loop. Thank You
Your index is '', shown by...
array(1) {
[""]=>
so you need to use...
$items['js']['']['geissweb/vatvalidation-min.js']
You have your variable $items['js'] as an array of arrays what your looking for without a foreach is this :
$items['js'][0]['geissweb/vatvalidation-min.js'] is not valid
after tests
$items['js'][""]['geissweb/vatvalidation-min.js'] is valid.

PHP square brackets in object variable - how to call the variable

I have a simple answer I just can't solution to:
var_dump(obj) =
object(stdClass)#15 (3) {
["properties"]=>
object(stdClass)#14 (2) {
["user_name"]=>
string(4) "somename"
["email_address"]=>
string(12) "test#test.com"
["arrays"]=>
object(stdClass)#17 (1) {
["sites[]"]=>
object(stdClass)#18 (4) {
["0"]=>
int(1)
["1"]=>
int(1)
["2"]=>
int(0)
["3"]=>
int(0)
}
}
}
How to call the 'sites[]' object in my 'obj'?
I tried the following:
obj->sites[]
obj->{'sites[]'}
Both options aren't working...
It might be better to clean up the code that generates that object, but you should be able to access the sites[] object via:
$sites = $obj->arrays->{'sites[]'};
However $sites will still be an object, so you would need to access its elements in a similarly awkward way:
echo $sites->{'0'};
It would be better to cast it to an array at that point:
$sites = (array) $obj->arrays->{'sites[]'};
Then you can access as an array:
echo $sites[0];
EDIT, seams you cannot access array elements indexed by a numerical string.
A better option (as discovered by a SO question i just posted about this) would be to use get_object_vars:
$sites = get_object_vars($obj->arrays->{'sites[]'});
Then you can access as an array:
echo $sites[0];

PHP Associative Array Specific Value.

I have been trying to figure this out for a while now. Please any advice would be appreciated. I just need to access the array for ["Item"]. How do I gain access to this?
array(1) {
[0]=>
object(SimpleXMLElement)#16 (2) {
["#attributes"]=>
array(2) {
["Name"]=>
string(10) "AuthorList"
["Type"]=>
string(4) "List"
}
["Item"]=>
array(3) {
[0]=>
string(9) "Smith, Joe"
[1]=>
string(10) "Peter, Ann"
[2]=>
string(18) "Magoo, Mr"
}
}
}
Assuming this structure is in a variable called var1, you should be able to access Item using:
$var1[0]->Item // returns the array
I try to explain it.
Like you see, your first array index is an object.
If you see something like this in your var_dump than you can access it through deferencing the object.
It's the same like you would create an object and would like to access a public variable:
$var1 = new Object();
// when your Object variables are public so you could access them by deference the Object
echo $var1->myVariable; // will echo the public variable "myVariable"
So the answer from adam is the right one :)

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.

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

Categories