Get First Element in PHP Stdobject

Get first element in PHP stdObject

Update PHP 7.4

Curly brace access syntax is deprecated since PHP 7.4

Update 2019

Moving on to the best practices of OOPS, @MrTrick's answer must be
marked as correct, although my answer provides a hacked solution its not
the best method.

Simply iterate its using {}

Example:

$videos{0}->id

This way your object is not destroyed and you can easily iterate through object.

For PHP 5.6 and below use this

$videos{0}['id']

How to get the first element value of a php object

If you can guarantee it is the first value you need (as the title of your question suggests), then reset will do what you want:

$firstvalue = reset($obj);

But it would make more sense if you would change your SQL query to return the key with a fixed alias. For example:

select user_id as key,
/* some other fields come here */
from users

And then you would just do $obj->key.

PHP stdClass Object, how to get element with the 0 index

Try this:

$obj = new stdClass();

$obj->{0} = 1;

echo $obj->{0};

source: http://php.net/manual/en/language.types.object.php

Accessing first element of stdobject

This should do the trick:

<?php

$json = '{"BTC_LTC":{"last":"0.0251","lowestAsk":"0.02589999","highestBid":"0.0251","percentChange":"0.02390438",
"baseVolume":"6.16485315","quoteVolume":"245.82513926"},"BTC_NXT":{"last":"0.00005730","lowestAsk":"0.00005710",
"highestBid":"0.00004903","percentChange":"0.16701570","baseVolume":"0.45347489","quoteVolume":"9094"}}';

$result = json_decode($json);
$vars = get_object_vars($result);
$keys = array_keys($vars);

echo $vars[$keys[0]]->last;

?>

You can try the code here

PHP Array get first item

The other answers offer good explanation about the empty key that I won't reiterate, but a simple way to get the first item in each of the sub-arrays is to map the reset function which we mentioned in the comments over your main array.

$firsts = array_map('reset', $your_array);

This should work regardless of what the key is.

How to access a property of an object (stdClass Object) member/element of an array?

To access an array member you use $array['KEY'];

To access an object member you use $obj->KEY;

To access an object member inside an array of objects:

$array[0] // Get the first object in the array

$array[0]->KEY // then access its key

You may also loop over an array of objects like so:

foreach ($arrayOfObjs as $key => $object) {
echo $object->object_property;
}

Think of an array as a collection of things. It's a bag where you can store your stuff and give them a unique id (key) and access them (or take the stuff out of the bag) using that key. I want to keep things simple here, but this bag can contain other bags too :)

Update (this might help someone understand better):

An array contains 'key' and 'value' pairs. Providing a key for an array member is optional and in this case it is automatically assigned a numeric key which starts with 0 and keeps on incrementing by 1 for each additional member. We can retrieve a 'value' from the array by it's 'key'.

So we can define an array in the following ways (with respect to keys):

First method:

$colorPallete = ['red', 'blue', 'green'];

The above array will be assigned numeric keys automatically. So the key assigned to red will be 0, for blue 1 and so on.

Getting values from the above array:

$colorPallete[0]; // will output 'red'
$colorPallete[1]; // will output 'blue'
$colorPallete[2]; // will output 'green'

Second method:

$colorPallete = ['love' => 'red', 'trust' => 'blue', 'envy' => 'green']; // we expliicitely define the keys ourself.

Getting values from the above array:

$colorPallete['love']; // will output 'red'
$colorPallete['trust']; // will output 'blue'
$colorPallete['envy']; // will output 'green'

PHP get first array element

You can point to the array with this $theMovie['result'][0]['backdrop_path']; or you can loop through it like this,

foreach($theMovie['results'] as $movie){
echo $movie['backdrop_path'];
}

PHP Easier way to get unique in array of objects

If the JSON string of the objects is used as a key, this can also be implemented with a simple foreach loop.

$arr = [
(object)['Name' => 'Kory Kelly', 'PhoneNumber' => '(555) 555-5555'],
(object)['Name' => 'Kory Kelly', 'PhoneNumber' => '(555) 555-5555'],
(object)['Name' => 'Kory Kelly', 'PhoneNumber' => '(555) 555-5555X'], //different
];

foreach($arr as $key => $object){
$arr[json_encode($object)] = $object;
unset($arr[$key]);
}
$arr = array_values($arr);
var_dump($arr);

Try it yourself at 3v4l.org.

Compared to array_unique($arr, SORT_REGULAR ), this approach only becomes interesting if only certain keys are to be taken into account in the object comparison.



Related Topics



Leave a reply



Submit