I have a simple array like so :
stdClass Object
(
[Colors] => Array
(
[0] => stdClass Object
(
[value] => Blue
)
)
[Sizes] => Array
(
[0] => stdClass Object
(
[value] => 10
)
[1] => stdClass Object
(
[value] => 30
)
)
)
then I just want to count array keys [Colors] and [Sizes], which should give me 2 in total, but using count() like count($array), throws "Warning: count(): Parameter must be an array or an object that implements Countable"
You have an object with two properties (Colors and Sizes) (https://www.php.net/manual/en/language.types.object.php).
Use get_object_vars (https://php.net/get_object_vars) to get the properties of your object, then count it:
$number_of_properties = count(get_object_vars($your_std_class_object));
Assuming your variable is $obj
count(get_object_vars($obj));
// return 2, for your object variables are two: Colors & Sizes (both arrays)
count($obj->Colors);
// return 1, for your object variable (Colors) (which is array) has only one element
count($obj->Sizes);
// return 2, for your object variable (Sizes) (which is array) has two elements
count(get_object_vars($obj->Sizes[0]));
// return 1, for your object variable(Size)'s [0] index has only 1 object element, i.e. 10
Related
I have facing issue in accessing the index 1 in a array.
How can i access the index 1?
Tried to access like this.
$selection = $menu_selection->{1}->vl
but i it showing undefined offset error. can anyone help me.
Array
(
[1] => stdClass Object
(
[vl] => Array
(
[0] => 1
[1] => 2
)
[op] => Array
(
[0] => O
[1] => O
)
)
[189] => Array
(
[vl] => Array
(
[0] => 1
)
)
)
From PHP 7.2 on you can do this (costly way):
((object) $menu_selection)->{1}->v1;
If way of accessing values doesn't matter it should be:
$menu_selection[1]->v1;
You are making a two errors.
$selection = $menu_selection->{1}->vl
The first error is accessing the first array position, you must do something like this $selection = $menu_selection[1].
The second error is using ->, this notation is used to access object properties and to call object functions.
To access index 1, you do something like this:
$selection = $menu_selection[1]
To access vl from array 1 try:
$selection = $menu_selection[1]->vl
As you can see in the var_dump, $menu_selection[1] is an object of type stdClass, that's why you need -> to access the vl property.
I have an array with stdClass objects as below. How would I count how many I have under Interviewee_Name?
Tried counting as I would do an array but get an error that I can't becaused of the stdClass Object and then unsure how to proceed from there
Array
(
[0] => stdClass Object
(
[Interviewee_Name] => Array
(
[0] => stdClass Object
(
[id] => rn_Interviewee_Name_DIR_27
[value] => Janusz Jasinski
)
)
)
This'll get the number of elements under Interviewee_Name, but it'll count all elements, not just objects.
count($arr[0]->Interviewee_Name)
However, if you really only want to get the objects in Interviewee_Name, you'll need to array_filter the array to get only the objects, and then count that new array:
count(array_filter($arr[0]->Interviewee_Name, function ($el) {
return (gettype($el) == 'object');
}));
The syntax for getting an element from an array looks like $arr['index'], but in this case Interviewee_Name is a property of an object so you need to use the object syntax: $obj->prop
$array = json_decode(json_encode($formData), True);
I added that to before looking to do a count and it worked!
I got this part in an object:
[tcd_old_value] => {"14":{"name":"Nakon radnog vremena","name_changed":false,"added_cc":[],"removed_cc":["4"]},"15":{"name":"Dodatno radno vrijeme","name_changed":false,"added_cc":[],"removed_cc":["4"]}}
after json_decode
$json_object = json_decode(tcd_old_value);
I get:
stdClass Object
(
[14] => stdClass Object
(
[name] => Nakon radnog vremena
[name_changed] =>
[added_cc] => Array
(
)
[removed_cc] => Array
(
[0] => 4
)
)
[15] => stdClass Object
(
[name] => Dodatno radno vrijeme
[name_changed] =>
[added_cc] => Array
(
)
[removed_cc] => Array
(
[0] => 4
)
)
)
I'm trying to count how many indexes are in this object (obviously the result should be 2)
$result = count($json_object);
echo $result //returns 1
Any insight on what I'm doing wrong here?
You cannot use count() in this case, because you have an object and not an array.
You may use the second parameter of json_decode() to have the JSON converted to an associative array:
$json_object = json_decode(tcd_old_value, true);
$result = count($json_object);
echo $result; // Now prints 2
Keep in mind that $json_object is no longer an object, but an array instead.
As per the document count
Returns the number of elements in array_or_countable. If the parameter
is not an array or not an object with implemented Countable interface,
1 will be returned. There is one exception, if array_or_countable is
NULL, 0 will be returned.
You may need to use
json_decode('json', true);
to convert as an 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'];
I am trying to get specific data set from the object but unable to find out why I cannot call the number in my assignment. This is it as follows:
$teir = $league->data->summonerLeagues->0->teir;
First of all this is calling the data from the league which is set and so that yo can see what the data looks like here it is:
stdClass Object
(
[data] => stdClass Object
(
[summonerLeagues] => Array
(
[0] => stdClass Object
(
[queue] => RANKED_SOLO_5x5
[name] => Dr. Mundo's Crushers
[tier] => BRONZE
[requestorsRank] => III
[entries] => Array
at this point I am trying to assign the variable $teir to teir in the object but they use 0 in the object and the way I am calling it must be the issue.....
Any suggestions?
Array access is with brackets, while object properties are accessed with ->:
$tier = $league->data->summonerLeagues[0]->tier; // Fixed typo per #MikePurcell's comment