PHP: Count number of objects within another object? - php

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)

Related

PHP Get / check value from associative array in array of individual stdClass Objects

Having real issues with this. I want to be able to get a value from this data which is returned via an API.
ie get value by
$CM_user_customfields['Organisation'],
$CM_user_customfields->Organisation.
is that even possible? I have tried loops and rebuilding the array but i always end up with a similar results and perhaps overthinking it.
I can't use the [int] => as the number of custom fields will be changing a lot.
$CM_user_customfields = $CM_details->response->CustomFields ;
echo '<pre>' . print_r( $CM_user_customfields, true ) . '</pre>';
// returns
Array
(
[0] => stdClass Object
(
[Key] => Job Title
[Value] => Designer / developer
)
[1] => stdClass Object
(
[Key] => Organisation
[Value] => Jynk
)
[2] => stdClass Object
(
[Key] => liasoncontact
[Value] => Yes
)
[3] => stdClass Object
...
many thanks, D.
I recommend convert to associative array first:
foreach($CM_user_customfields as $e) {
$arr[$e->Key] = $e->Value;
}
Now you can access it as:
echo $arr['Organisation'];
You can also achieve it by: (PHP 7 can convert stdClass and will do the trick)
$arr = array_combine(array_column($CM_user_customfields, "Key"), array_column($CM_user_customfields, "Value")));

PHP: Count stdClass objects within array

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!

Accessing #attribute from php object

I'm trying to access values from a PHP object returned from an API. I am trying to get the 'total' atribute.
stdClass Object
(
[#attributes] => stdClass Object
(
[status] => ok
)
[invoices] => stdClass Object
(
[#attributes] => stdClass Object
(
[page] => 1
[per_page] => 25
[pages] => 1
[total] => 5
)
My returned object is stored in a variable called $list.
$list->invoices->attributes->total
I'm trying to echo / print_r that, but getting nothing?
Any help is appreciated!
The # is a part of the property name, you can't just ignore it.
echo $list->invoices->{'#attributes'}->total;
$total = $list->invoices->attributes()->total;
As it turns out, you don't need to even specify #attributes to read this data. The key trick to get to it is to cast the result as a string.
So, I know it seems strange, but this will work in this case:
echo (string)$list->invoices['total'];
In my case i used:
$att_side = $xml->item->attributes()->side;

Convert array objects to string and separate the values

I have the following result
Array (
[0] => stdClass Object ( [name] => Identification )
[1] => stdClass Object ( [name] => Assay )
[2] => stdClass Object ( [name] => pH(Acidity/Alkalinity))
[3] => stdClass Object ( [name] => Sterility )
)
What i want is to separate the object array values using a comma and return as a string,
so as to have this result:
Identification, Assay, ph(Acid/Alkalinity), Sterility
I have tried the following
$data=(array)$result;
$answer=implode(",",$data);
This return :
Message: Object of class stdClass could not be converted to string
How best can this be achieved?
You are missing the fact that you are dealing with an array of objects.
Looks like you can achieve that by doing:
$output = array_map(function ($object) { return $object->name; }, $input);
echo implode(', ', $output);
To use this feature in the 'title' attribute I did:
echo implode('&NewLine;', $output);
It will convert the array of objects to a string, and you can store it in the database, use
serialize(array_of_object)
Might help someone. I used it in my case. Thank you!

How to access stdclass object after a specific key value pair?

I have a stdclass object as shown below:
stdClass Object
(
[text] => Parent
[values] => Array
(
[0] => stdClass Object
(
[id] => /m/0c02911
[text] => Laurence W. Lane Jr.
[url] => http://www.freebase.com/view/m/0c02911
)
)
)
I iterate over multiple such objects, some of which have
stdClass Object
(
[text] => Named after
[values] => Array
(
[0] => stdClass Object
(
[id] => /m/0c02911
[text] => Stanford
[url] => SomeURL
)
)
)
I was wondering how I would access the "values" object if it comes after a "text" that has "Parent" as its value?
there are serveral ways to turn it to array:
First Solution:
$value = get_object_vars($object);
Second Solution:
$value = (array) $object;
Third Solution
$value = json_decode(json_encode($object), true);
to get value of converted array
echo $value['values']['0']['id'];
The alternate way to access objects var without convert the object, try
$object->values->{'0'}->id
Expanding (or rather minimalizing) upon answer by Somwang Souksavatd, I like accessing Object values like this:
echo get_object_vars($object)['values']['0']['id'];
I had the same issue, still not so sure why but I was able to get it working using this workaround:
$k2 ="1";
$elements = json_decode('{"id":"1","name":"User1"}');
//$elements['id'] == $k2; //****Not Working
$tmp = (object)$elements;
$tmp = $tmp ->id; //****Working
//$tmp =$elements['id'] ; //****Not Working
return $tmp == $k2;
I have to say that sometimes accessing the element as array works and some times not,(On PHP7 it worked for me but on PHP5.6 it didn't).
$elements can be Array to but I chose to demonstrate with json string.
I hope this helps somehow !!!
$Obj=stdClass Object
(
[text] => Named after
[values] => Array
(
[0] => stdClass Object
(
[id] => /m/0c02911
[text] => Stanford
[url] => SomeURL
)
)
)
$Values= $result->values;
$Item = $Values[0];
$id=$Item->id;
$text = $Item->text;
$url=$Item->url;
I'm doing the same thing and all I did was this;
<?php
$stdObject = json_decode($stdClassObject);
print $stdObject->values[0]->id;
this can help you accessing subarrays in php using codeigniter framework
foreach ($cassule['tarefa'][0] as $tarefa => $novo_puto_ultimos_30_dias) {
echo $novo_puto_ultimos_30_dias;
What you are looking for is the Object['values'][0]: 'values' is the keymap just like 'text', and [0] is the index inside that array you wish to access. so if you would like to get the id deep in the nest, you'd have to do something like
Object['values'][0]['id']
or
Object['values'][0]->id
which should give you /m/0c02911. But I have no idea how you are doing your loop, so you will have to adjust it to your needs and place proper variables where they need to go in that code in your loop. Not exactly sure which language you are working with.

Categories