This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Able to see a variable in print_r()'s output, but not sure how to access it in code
$response = $amazonEcs->category('DVD')->responseGroup('Large')->search("Matrix Revolutions");
var_dump($response);
I was using var_dump($response) and now I want to know how can I get the values of Item from 0 to 9.
Item is nested inside a couple of objects. Assuming your outer object is $response, you are looking for:
$response->Items->Item[0]
items is an object stdClass, and item is a property of that object. item itself is an array, having the keys 0-9 you are looking for.
Each of those array elements is then an object stdClass itself, so access its properties (which we can't see in your output) with the -> operator.
$response->Items->Item[0]->someProperty
$response->Items->Item[9]->someOtherProperty
Edit: Changed item to Item, as it is capitalized in the sample output.
Use "->" to go inside objects and use [] to go inside arrays.
So, you are looking for
$response->items->item
Use foreach to loop :
foreach ($response->items->item as $item)
{
// Process $item, which will be $item[0], $item[1].. in each iteration.
}
Related
This question already has answers here:
php array difference against keys of an array and an array of keys?
(3 answers)
Unset elements using array keys
(2 answers)
Closed 5 years ago.
I am attempting to mirror the behavior of newArray = oldArray, with the caveat of excluding some key/values of the oldArray, so something like newArray = oldArray - undesiredOldKeyValue. I realize this is fully doable with a foreach on the oldArray and using an if to see if the encountered key is desired or not, but I am interested in a simpler or more concise approach if possible.
A couple of things to keep in mind, I need to exclude key/value pairs based on key, not value. I do not want to modify the oldArray in the process of doing this.
You may try to use array_filer. Something like:
$new_array = array_filter($old_array, function ($value, $key) {
// return false if you don't want a value, true if you want it.
// Example 1: `return $value != 'do not keep this one';`
// Example 2: `return !in_array($key, ['unwanted-key1', 'unwanted-key2', 'etc']);`
}, ARRAY_FILTER_USE_BOTH);
It will filters elements of an array using a callback function.
This question already has an answer here:
How can I access a property with an invalid name?
(1 answer)
Closed 6 years ago.
I have a stdClass, that has an attribute with a dot in it's name. It's returned to me via an API, so there's nothing I can do with the name.
It looks like this:
stdClass Object ( [GetPlayerResult] => stdClass Object ( [RegistrationResponses.Player] => Array (...)
Now, how do I access the array? When I do this:
print_r($result->GetPlayerResult->RegistrationResponses.Player);
it (obviously) prints just "Player". I have tried putting apostrophes around the last part, using [''] syntax (like an associative array), none of that works and throws a 500 error.
Any ideas?
Thanks!
You can try using braces:
$object->GetPlayerResult->{"RegistrationResponses.Player"}
Or you can cast it to an associative array
$result = (array) $object->GetPlayerResult;
$player = $result["RegistrationResponses.Player"];
If you are parsing your website response using json_decode, note the existence of the second parameter to return as associative array:
assoc
When TRUE, returned objects will be converted into associative arrays.
Please convert your object to array by using this function
$arr = (array) $obj;
echo "<pre>";print_r($arr);exit;
by print $arr you can show elements of array.
This question already has answers here:
How to filter an array by a condition
(9 answers)
Closed 6 years ago.
I have a webservice that returns a very large json object. I decode this to an array for parsing.
What I'm trying to do though is extract all array members from the json array where there is [id] => 21 at the start of the individual array object.
A nice simple task using LINQ, but not an idea how to do this in PHP. There has to be a way to filter through and extract.
I'd use something like array_filter for this
function find_id($var)
{
// returns whether the is is 21
return($var['id'] === 21);
}
$result = array_filter($jsondata, "find_id"));
array_filter Iterates over each value in the array passing them to the callback function. If the callback function returns true, the current value from array is returned into the result array. Array keys are preserved.
This question already has answers here:
String to multidimensional array path
(1 answer)
Nested numbering to array keys
(1 answer)
Closed 8 years ago.
I would like a multiple child 'key' as a 'lookup' to get a object out of various associative arrays. For example:
$lookup_key = "['objects'][0]['object2']";
// this will be stored so when I need to get an object's value from various different arrays I can use these string to form a lookup key to get certain objects
$object_array= array();
$object_array= ["objects"=>[["object1"=>"boot", "object2"=>"shoe"],"object"], "flowers"];
// using that key get the value
$object_value= $object_array->lookup($lookup_key);
Now does php have a method that already does this type of lookup or I suppose its an multidimensional key?
Any help would be great! Thanks in advance. This is part of an object lookup table.
I approached this in a slightly different way by storing a string of the objects then exploding that into an array from that travelling across that array to get the value:
$object_array= array();
$object_array= ["objects"=>[["object1"=>"boot", "object2"=>"shoe"],"object"], "flowers"];
$lookup=array();
$lookup_key="objects,0,object2";
$lookup=explode(',',$lookup_key);
$temp_object=array();
$new_object_array=array();
$new_object_array=$object_array;
$count=count($lookup);
for($i=0; $i<$count; $i++) {
$temp_object=$new_object_array[$lookup[$i]];
$new_object_array=$temp_object;
}
echo "\n value: $new_object_array";
As I was search for a quick implementation this seemed to be the best way within my time constraints and php lose variable casting.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Diddling with arrays with numeric string keys
As specified in PHP's manual, we can do type casting on objects and covert them to array as follows:
$arrayResult = (array)$someObject;
But I found very interesting remark in documentation:
If an object is converted to an array, the result is an array whose
elements are the object's properties. The keys are the member variable
names, with a few notable exceptions: integer properties are innaccessible
What does "integer properties" stands for?
I believe this means that you cannot use typical integer properties of arrays to iterate through, such as in a for loop. The elements are not integer-indexed.
The below is the example:
$obj = new stdClass;
$obj->{'1'} = 1;
$arr = (array) $obj;
var_dump($arr);
var_dump(isset($arr[1])); // will get false