Want to get array value - php

I have an array like below.
Here, I am getting array key like [_aDeviceTokens:protected] => Array.
$array= ApnsPHP_Message Object
(
[_bAutoAdjustLongPayload:protected] => 1
[_aDeviceTokens:protected] => Array
(
[0] => BD74940085E1579333E93B7D172CF82F5A3E0B17617D904107CD77573C42CEC9
)
[_sText:protected] => test
[_nBadge:protected] => 1
[_sSound:protected] => default
[_sCategory:protected] =>
[_bContentAvailable:protected] =>
[_aCustomProperties:protected] => Array
(
[channel_id] => 1xxxx8
[detail_id] => 1
)
[_nExpiryValue:protected] => 1500
[_mCustomIdentifier:protected] =>
)
As an array have object value so I am trying to get value of this key like,
$array->_aDeviceTokens:protected[0]
But this gives me an error.
So how can I achieve the value of these array keys?

It seems you are trying to access protected properties of an object that you are treating like an array.
Looking at the code here: https://github.com/immobiliare/ApnsPHP/blob/master/ApnsPHP/Message.php
There are publically accessible 'getters' for those attributes.
Extract of class ApnsPHP_Message:
public function getCustomIdentifier()
{
return $this->_mCustomIdentifier;
}
So instead of trying to access those properties as you have been, use the corresponding getter.
$custom_identifier = $message->getCustomIdentifier();

Convert object to array.
$array = json_decode(json_encode($array),true);
Now, you get value from $array like this
$array['_aDeviceTokens:protected'][0]

Related

How to pass and access the array variable using php

I have a array $param and while giving Print_r, the output as follows,
Array (
[pattern] =>
[status] => Array ( [0] => 0 [1] => 4 )
)
I have to pass the status value to one function like,
function value($action, $param){
// want to use the value here
}
how can i get the value here. please help
If you want to pass a multi-dimensional array as a parameter, simply pass the child array with the name of the parent (container array).
So if an array like
Array (
[pattern] =>
[status] => Array (
[0] => 0
[1] => 4
)
)
if you want both the elements of status to be passed into the function,pass the name of the array parent. (in this case, param)
function foo($x)
{
echo "<pre>"; // just to make reading easy ;)
print_r($x);
}
The function foo() displays the contents passed into the function which you can use to see what's being passed.
so things like foo($param['status'])
gives this :
Array( [0] => 0 [1]=> 4 )
and something like foo($param['status'][1])
gives this :
4

Parsing a timestamp based PHP std class obkect

I have a PHP standard class object converted from json_decode of a REST call on an API which looks like :
Array
(
[1437688713] => stdClass Object
(
[handle] => Keep it logically awesome.
[id] => 377748
[ping] => stdClass Object
(
[url] => https://api.me.com
[id] => 377748
[name] => web
[active] => 1
[events] => Array
(
[0] => data_new
[1] => data_old
)
So far i had no issues in parsing any of the PHP objects. However this one is failing because i can not access the nested object elements using a key since 1437688713 is not assigned to a key and accessing an object is failing if i try to do this:
$object->1437688713->handle
Is there a way to access these elements ?
Update: one more thing, i would never know this value (1437688713) in advance. Just like a key. All i get is a stdclass object which i have to parse.
The outer part of your data is an array, not an object. Try:
$array['1437688713']->handle;
or if you don't know the key, you can iterate over the array (handy if it may contain multiple objects too):
foreach ($array as $key => $object) {
echo $key; // outputs: 1437688713
echo $object->handle; // outputs: Keep it logically awesome.
}
Get the first item from $object array
$first_key = key($object);
Use it with your response array,
$object[$first_key]->handle;
Or, the first element of array
$first_pair = reset($object)->handle;

php get array values

I have this array called inputs:
Array ( [0] => InputValidation Object ( [array:InputValidation:private] => Array ( [Id] => demo1 [Required] => [MinLength] => 10 [MaxLength] => [RegexName] => [RegexMsg] => ) ) [1] => InputValidation Object ( [array:InputValidation:private] => Array ( [Id] => demo2 [Required] => [MinLength] => 20 [MaxLength] => [RegexName] => [RegexMsg] => ) ) )
I must get value Id, Required, MinLength, MaxLength, RegexName and RegexMsg. I have tried to foreach like this
foreach ($this->inputs as $input){
echo $input['Id'];
}
But it give me an error: Cannot use object of type InputValidation as array
How can I do? Thanks
inputs is an array in which the first element (index 0) is an object.
But in your loop, you try to echo an object property as if the object was an array.
Try:
foreach ($this->inputs as $input){
echo $input->Id;
}
Compared to JavaScript, objects in PHP cannot be accessed using the [] accessor, you have to use the -> accessor.
Yes I made a mistake.
Array inputs has 1st index with an object InputValidation.
This object also have a PRIVATE property named InputValidation (which is an associative array, where located the IdIindex you're looking for).
But this property is private. You won't be able to access it directly. You need a method on object InputValidation for that.
If that propery wasn't private but public, you woukd be able to access it as follow:
inputs [0]-> InputValidation-> InputValidation['Id'];
1st advice: avoid having an object which has a property with the same name than this object. That is confusing (I'd been trapped in it!)
2nd advice: read the doc about the objects you're using and PHP objects doc.
The advice from Comfreek looks like new feature in PHP or something that required well knowledge of it.

PHP Array is filled with data, but can't return values

I have an array $presettings
print_r($presettings); outputs:
Array (
[0] => stdClass Object (
[uuid] => xxx-1ef8-aac6-xxx-xxx
[name] => etime
[owner] => eder112T Resident
[online] => 1
[channel] => 63b525ae-xxx-3555-1c74-xxx
[owner_uuid] => a371751c-eb77-xxx-899c-xxx
[simname] => Plainfield
[slurl] => xxx://xxx/xx/xx/243/24/xx/?title=xx
[design] => 2
[msg_oftheday] => two
[machine_name] => one
[autopay] =>
[autolog_leave] =>
[autolog_offline] =>
[allow_activation] =>
)
)
and now i want to get a special key:
echo "test output : "$presettings['machine_name']." testend";
outputs "" (nothing).
my method look like this
function preloadSettingsFromMYSQL($ownername,$prim_uuid)
{
$result = $this->instance->get_rows("SELECT * FROM etime_rims where owner='".$ownername."' AND uuid='".$prim_uuid."'");
return $result;
}
$result is an object array, also tried it with $presettings->machine_name, did not work too.
where is the error?
thank you.
If you look closely in the print_r result, you can see that there is a 0 there, meaning that those values aren't directly in $presettings, but actually in the first element of $presettings.
Just try:
$presettings[0]->machine_name
You object is multi dimensional so just add a level
$presettings[0]->machine_name;
As side note you have an object here and not array do don't try to access alues with scopes.
In your array, you have stdClass Object at index 0. You're looking to access the object's variables, which is a slightly different syntax than arrays:
echo $presettings[0]->machine_name;

PHP: How to extract a property from this array

I have an array of values returned from Facebook - let's call it $array.
If I do print_r($array) - it looks like this:
Array
(
[code] => 200
[headers] => Array
(
[0] => Array
(
[name] => Some value
[value] => *
)
[1] => Array
(
[name] => Some value
[value] => Some value
)
[2] => Array
(
[name] => Some value
[value] => Some value
)
)
[body] => {"about":"Some more values.","can_post":true}
)
I need to extract the body part from this array.
I cannot refer to it by it's position, I'm looking for something like $array->body and receive the {....} string.
$array->body would work if the variable $array was an object
For arrays, just use:
$body = $array['body'];
(see: http://be2.php.net/manual/en/language.types.array.php)
If you want to access to your array via -> just do 1 more step:
$array = (object) $array;
And now, you can access to your body via:
$array->body;
Else without this step there is just one way:
$array['body'];
If you are more interested about converting arrays into objects, you can visit this question: How to convert an array to object in PHP?
Access array elements by using their name.
$array['body'];

Categories