Php get stdclass properties

The stdClass class

Objects of this class can be instantiated with new operator or created by typecasting to object. Several PHP functions also create instances of this class, e.g. json_decode() , mysqli_fetch_object() or PDOStatement::fetchObject() .

Despite not implementing __get()/__set() magic methods, this class allows dynamic properties and does not require the #[\AllowDynamicProperties] attribute.

This is not a base class as PHP does not have a concept of a universal base class. However, it is possible to create a custom class that extends from stdClass and as a result inherits the functionality of dynamic properties.

Class synopsis

This class has no methods or default properties.

Examples

Example #1 Created as a result of typecasting to object

The above example will output:

object(stdClass)#1 (1) < ["foo"]=>string(3) "bar" >

Example #2 Created as a result of json_decode()

The above example will output:

object(stdClass)#1 (1) < ["foo"]=>string(3) "bar" >

Example #3 Declaring dynamic properties

The above example will output:

object(stdClass)#1 (2) < ["foo"]=>int(42) ["1"]=> int(42) >

User Contributed Notes 1 note

In PHP8 this has been changed

A number of warnings have been converted into Error exceptions:

Attempting to write to a property of a non-object. Previously this implicitly created an stdClass object for null, false and empty strings.

So if you add properties to a $var, you first need to make it a stdClass()

$var = new stdClass();
$var->propp1 = «nice»;
$var->propp2 = 1234;

Источник

Get values stdClass Object PHP

Solution 1: If you’re just after the first achievement for that particular game, you could use this hack: The API is probably giving you a JSON response, so you may also want to consider this: The second parameter turns the JavaScript objects into PHP arrays, which in most cases makes it easier to handle them, e.g. Solution 2: You can use complex curly notation to reference dynamic properties of an object This should give you , given == and == Just do: If you wanted an array you would access like this since the array contains an object: Solution 2: Its an object array, so just as you call the array element,

Get values stdClass Object PHP

I work with mp4 data extract php script. this returns a stdClass Object as below. can anyone tell me how to show the duration from below object. thank you

stdClass Object ( [hasVideo] => 1 [hasAudio] => 1  => stdClass Object ( [width] => 472 [height] => 360 [codec] => 224 [codecStr] => H.264 )  => stdClass Object ( [codec] => 224 [codecStr] => AAC ) [duration] => 622.8 ) 

Extracting a value out of a stdClass object, Teams. Q&A for work. Connect and share knowledge within a single location that is structured and easy to search. Learn more

Читайте также:  Sign Up

How to get a variable value in a stdClass object?

I get a returned result from an API in stdClass format. I don’t know anything about this type of data. So here it looks like:

object(stdClass)#41 (1) < ["return"]=>object(stdClass)#42 (7) < ["afterPayOrderReference"]=>string(32) "d4ab78df6ab2ef84194dd1c1d66240b8" ["checksum"]=> string(32) "4f8826a99e9c0a67e578d04b6a625117" ["resultId"]=> int(0) ["statusCode"]=> string(1) "A" ["timestampIn"]=> float(1408533108515) ["timestampOut"]=> float(1408533113616) ["transactionId"]=> int(129525) > > 

What I need is retrieving the statusCode value. I tried doing like in a post I read:

$array = (array) $stringResult; $array[0]->statusCode; 

But it didn’t work. Please, someone explain to me in the simplest way because it’s really new to me. Thanks.

Object properties are accessed with the -> operator. Just do:

echo $stringResult->return->statusCode; 

If you wanted an array you would access like this since the array contains an object:

$array = (array)$stringResult; echo $array['return']->statusCode; 

Its an object array, so just as you call the array element,

echo $stringResult["return"]->statusCode 

Php get array values from stdClass object, php get array values from stdClass object. Ask Question Asked 8 years, 2 months ago. Modified 1 year, 5 months ago. Viewed 14k times 3 1. Here is my function which collects data from page. Data is stored into array and at the end I echo it using var_dump(). Data is bunched into array of length 40.

Php get array values from stdClass object

Here is my function which collects data from page. Data is stored into array and at the end I echo it using var_dump().

Data is bunched into array of length 40. I was trying to get each array value seperatelly.

Array result looks like this when I dump using var_dump($root) :

object(stdClass)[1] public 'items' => array (size=40) 0 => object(stdClass)[12983] public 'comment' => string 'Locanda is great ! Maybe a bit overrated. But I definitely enjoyed my first dinner here! 

Trippa alla romana is so good! My first time eating tripe and the texture wasn't bad at all plus the sauce was fantastic!

The leg of lamb was nice, a bit too rare for me but I didn't specify how I wanted it so next time I will.

The hen wrapped in pancetta was so delicious and juicy! Would recommend that over the lamb.

We also had a nice pasta dish.. Orecchiette which was of cour'. (length=738) public 'rating' => string '4.0' (length=3) public 'date' => string '2014-04-03' (length=10) 1 => object(stdClass)[12984] public 'comment' => string 'Made reservations last friday night here for a fun special dinner, had a pretty good time but was a bit underwhelmed.

We arrived early for our reservation, about 15 minutes and were going to go to the bar for a cocktail but the hostess told us that she could sit us early and to not to go to the bar and then we were sat 10 minutes after our reservation time. A bit annoying but I get it, it's Friday night. We got a great table in the back half or the restaurant so we weren't by the door and awk'. (length=1676) public 'rating' => string '3.0' (length=3) public 'date' => string '2014-04-21' (length=10) 2 => object(stdClass)[12985] public 'comment' => string 'Thinking to have something special for dinner on Sunday, we ordered takeout from Locanda through Postmates. Â Words cannot describe how sad we were when the food came. Â The portions are TINY TINY TINY and slopped into large paper takeout containers (no, not even the wax lined ones) that make them look even smaller. Â Miniscule, beyond small portions. Â The sauce soaked into the containers, so icky. Â Food has never looked so pitiful.

We paid $70 for three dishes that would all fit on a single plate '. (length=2081) public 'rating' => string '2.0' (length=3) public 'date' => string '2014-03-31' (length=10) 3 => object(stdClass)[12986] public 'comment' => string 'I can't say enough good things about locanda. Finally reviewing after eating dinner there as opposed to just visiting the bar.

Dinner was fantastic - warm bread and brussel sprouts to start. Pasta with broccoli rabe and sausage was so wonderful (and so hard to find in sf!)

Now for the bar. Bartenders are so knowledgeable and friendly. Â I love how they'll listen to your feedback and then get creative on your next drink. Most recently tried the steam shandy - excellent! Â Oh, and old f'. (length=530) public 'rating' => string '5.0' (length=3) public 'date' => string '2014-03-30' (length=10) 4 => object(stdClass)[12987] public 'comment' => string 'The wait for 2 on a Saturday night at 6:15pm was only 20 minutes. Cocktails were delicious, though the ginger cocktail was too gingery. We got the whole grilled fish. It was pretty good but a bit too salty. The atmosphere is great.' (length=231) public 'rating' => string '4.0' (length=3) public 'date' => string '2014-04-20' (length=10)

the code which give this result.

 function Getdata($url)< print("$url\n"); $root = new stdClass(); $items = array(); $html = file_get_html($url); if($html)< $containers = $html->find('div.review-list div.review div.review-wrapper'); foreach($containers as $container)< $comments = $container->find('div.review-content p.review_comment'); $item = new stdClass(); foreach($comments as $comment)< $comment_html = $comment->innertext(); $item->comment = $comment_html; > $metas = $container->find('div.review-content meta'); foreach($metas as $meta)< $itemprop = $meta->itemprop; $content = $meta->content; if($itemprop == 'ratingValue') $key = 'rating'; else $key = 'date'; $item->$key = $content; > $items[] = $item; > > $root->items = $items; if($html)< $html->clear(); unset($html); > var_dump $root; > 

Here I tried to get each of the comment , rating , date separately

 for($i = 0 ; $i "; echo $root[$j]['rating']."
"; echo $root[$j]['date']."
"; > //var_dump($root); flush(); ob_flush(); >

I want to get each comment, rate, date separate to store in db. so please help me accordingly.

Читайте также:  Установить плеер html 5

$root->items is the array you want to iterate:

$root = yelp($url); foreach($root->items as $item) < echo $item->comment."
"; echo $item->rating."
"; echo $item->date."
"; >
$comment = array_map(create_function('$o', 'return isset($o->comment)? $o->comment: "" ;'), $item); 

this code will return all the comment in single array.

Php — Get data from stdClass Object, Hi I want to get the name in this objects how can I get these names for each stdClass. stdClass Object ( [page] => 0 [items] => 10 [programItems] => stdClass Object (

Get stdClass Object by value

Given this stdClass structure.

[RecentAchievements] => stdClass Object ( [1446] => stdClass Object ( [3319] => stdClass Object ( [ID] => 3319 [GameID] => 1446 [Title] => Hello World! [Description] => Complete World 1-1 [Points] => 15 [BadgeName] => 03909 [IsAwarded] => 1 [DateAwarded] => 2013-10-20 19:35:46 ) 

I need to check the objects inside 3319, like «Title», so it’d be like:

$data->RecentAchievements->$gameid->ACHVIDHERE->Title; 

To get the $gameid, it came from other object (RecentlyPlayed), this way:

[RecentlyPlayed] => Array ( [0] => stdClass Object ( [GameID] => 1446 [ConsoleID] => 7 [ConsoleName] => NES [Title] => Super Mario Bros [LastPlayed] => 2013-10-20 21:38:22 [ImageIcon] => /Images/000385.png [ImageTitle] => /Images/000272.png [ImageIngame] => /Images/000387.png [ImageBoxArt] => /Images/000275.png ) ) $gameid = $data3->RecentlyPlayed['0']->GameID; 

API don’t let me use the same RecentlyPlayed to get the achievement ID, i have to extract it from RecentAchievements. Is it possible?

If you’re just after the first achievement for that particular game, you could use this hack:

$firstAchievement = reset($data->RecentAchievements->$gameid); $title = $firstAchievement->Title; 

The API is probably giving you a JSON response, so you may also want to consider this:

$data = json_decode($response, true); ^^^^ 

The second parameter true turns the JavaScript objects into PHP arrays, which in most cases makes it easier to handle them, e.g.

$firstAchievement = reset($data['RecentAchievements'][$gameid]); $title = $firstAchievement['Title']; 

You can use complex curly notation to reference dynamic properties of an object

$data->RecentAchievements->->->Title; 

This should give you Hello World! , given $gameid == 1446 and $someotherid == 3319

Читайте также:  Php log debug message

How do I get a value from an array of stdClass object, How do I get a value from an array of stdClass object. Ask Question Asked 12 days ago. Modified 12 days ago. Viewed 30 times -1 How do I retrieve get the last value that was entered. I should get rem1 = good, but I’m getting rem1 = improving, which was the last value that was entered.

Источник

Оцените статью