PHP: Count stdClass objects within array - php

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!

Related

PHP count array keys in multidimensional array

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

Recursively loop through php array and convert object elements to array storing their class names

I've been hours trying to figure this, i know i'm missing something obvious. This is the problem:
I have an array that some of its elements are objects, others are arrays and others are of other types. What i need is:
Loop through the array and convert the object elements into array elements.
Loop recursively (or whatever you want to call it) through these new array elements (the ones that were converted from objects) and the elements that already are array and perform the previous task (that is: convert the object elements into array elements).
Here's the gotcha: every time an object element is converted into array, the class name of the object has to be added as the first element of the array that is generated by converting the object.
Here is a simplified example:
Array:
Array
(
[0] => PhpParser\Node\Expr\Assign Object
(
[var] => PhpParser\Node\Expr\Variable Object
(
[name] => bar
)
[expr] => PhpParser\Node\Scalar\LNumber Object
(
[value] => 22
)
)
)
I need a function like this:
//$arr is the array previously posted
$arr = cool_object_to_array($arr);
var_dump($arr );
outputs
Array
(
[0] => Array
(
[0] => PhpParser\Node\Expr\Assign
[var] => Array
(
[0] => PhpParser\Node\Expr\Variable
[name] => bar
)
[expr] =>Array
(
[0] => PhpParser\Node\Scalar\LNumber
[value] => 22
)
)
)
The nesting level is unknown. It can be many arrays nested on objects nested on other objects or arrays, etc. The example is just very simplified. I need a solution that handles that too.
Thanks in advance for all your answers!
This should do it:
function cool_object_to_array($array) {
foreach ($array as $key => &$value) {
if (is_object($value)) {
$type = get_class($value);
$value = (array) $value;
array_unshift($value, $type);
}
if (is_array($value)) {
$value = cool_object_to_array($value);
}
}
return $array;
}

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: Count number of objects within another object?

I'm still new at PHP and I can't seem to count the number of Objects within another object. The stdClass object looks like this:
stdClass Object (
[data] => Array (
[0] => stdClass Object (
[Code] => ABC
[Title] => Alphabet
[sections] => Array (
[0] => stdClass Object (
[Name] => Sounds
[sections] => Vowels
)
)
)
)
I must count the number of elements in this object so i can echo it properly. For the data, I was able to do it:
$number = count($hanap->data);
I don't know how to do it for the sections.
$number = count($hanap->data->sections); // does not work.
Thanks. Any help will be greatly appreciated. :)
this will solve your problem, just cast the object to array and count it
$total = count((array)$obj);
PHP: Count an stdClass object
count($hanap->data[0]->sections)
You are missing the first member of the array where they are...
$number = count($hanap->data[0]->sections)

problem with SimpleXML and inclosed arrays

I'm having a problem with SIMPLE XML. I seem to have an array with dumping the whole object. However, when I try to access the array, I get a single element of the array back.
Here is the full dump:
SimpleXMLElement Object
(
[#attributes] => Array
(
[type] => array
)
[person] => Array
(
[0] => SimpleXMLElement Object
(
..........................
),
[1] => SimpleXMLElement Object
(
..........................
)
)
)
when I try to access the person array through $xml->person, instead of getting the array, I get the first element back. Any ideas?
From the SimpleXML Basic usage documentation in the PHP Manual:
NOTE: Properties ($xml->movie in previous example) are not arrays. They are iterable and accessible objects.
So, in your case, $xml->person is not actually an array, just an iterable object. It can be easily converted to an array with:
$persons = array();
foreach ($xml->person as $person) {
$persons[] = $person;
}

Categories