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
Related
This question already has answers here:
How to get the last element of an array without deleting it?
(33 answers)
What is an array internal pointer in PHP?
(1 answer)
PHP behavior and arrays pointers
(2 answers)
Closed 11 months ago.
I attempted to pass an explode statement into end() and got the following notice from my IDE, "Only variables can be passed by reference", which is not surprising.
However, what confuses me is that if I do something like
$array = ['cat', 'dog', 'bird'];
end($array);
echo print_r($array, true);
['cat', 'dog', 'bird'] prints out.
It is/was my understanding that passing something by references changes the value of the variable so I had expected $array to only print out 'Bird' after being passed into end().
I suspect I have a fundamental misunderstanding of passing something by reference...
The end() function moves the internal pointer to, and outputs, the last element in the array. This array is passed by reference because it is modified by the function. This means you must pass it a real variable and not a function returning an array because only actual variables may be passed by reference.
end() function returns a value and does not change the reference array!
so you may save the value to another variable first. in fact it returns the value of the last element or false for empty array.
so you may try:
$array = ['cat', 'dog', 'bird'];
$last = end($array);
echo print_r($last, true);
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:
Accessing an associative array by integer index in PHP
(6 answers)
Closed 7 years ago.
I want to get the value of the KEY of an associative PHP array at a specific entry. Specifically, I know the KEY I need is the key to the second entry in the array.
Example:
$array = array('customer' => 'Joe', 'phone' => '555-555-5555');
What I'm building is super-dynamic, so I do NOT know the second entry will be 'phone'. Is there an easy way to grab it?
In short, (I know it doesn't work, but...) I'm looking for something functionally equivalent to: key($array[1]);
array_keys produces a numerical array of an array's keys.
$keys = array_keys($array);
$key = $keys[1];
If you're using PHP 5.4 or above, you can use a short-hand notation:
$key = array_keys($array)[1];
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:
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.
}