PHP - Cannot access array in stdclass object - php

Currently I have a stdclass object. How can I access the properties of the object such as the name of the author? I have tried to echo out $authors = $book->authors_summary[0]->name; but it returns 'Trying to get property of non-object in ...' error.
[authors_summary] => Array
(
[0] => Array
(
[id] => 123456789
[name] => Ben Smith
)
[1] => Array
(
[nconst] => 987654321
[name] => Tommy Lee
)
)

Your authors_summary[0] is an array, so you cannot use -> object pointer, you must call it as array:
$authors = $book->authors_summary[0]['name'];

'name' is the key of array so you may access name value like this
$authors = $book->authors_summary[0]['name'];
echo $authors;

Array
(
[data] => Array
(
[0] => stdClass Object
(
[Id] => 5
[Name] => nilu
[Email] => nilu#gmail.com
[Password] => nilu
[Image] => LOVE_A_O_D.jpg
)
)
)
how to fetch password from this.....
in controller----
$datas['data']= $this->Login_model->loginchk($value);
in model-----
$results=$this->db->get();
return $results->result();

Related

how get value feild[ID] stdClass Object in php code

how get value feild[id] in php code
stdClass Object ( [List_inserted] => Array ( [0] => stdClass Object ( [ID] => 145001 [value] => 40 ) ) [Sucssess] => 1 [ErrorMassage] => OK )
You didn't give us a name of stdClass so I'm assuming it's $stdClass.
$stdClass->List_inserted[0]->ID
Let's break it down;
stdClass Object ( [List_inserted] => Array ( [0] => stdClass Object ( [ID] => 145001 [value] => 40 ) ) [Sucssess] => 1 [ErrorMassage] => OK )
We access objects with a -> and we access arrays with []
The first part tells us it's an object, so it's;
$stdClass->List_inserted
List_inserted is an array thanks to the => Array. We can access this with [0].
$stdClass->List_inserted[0]
Well, List_inserted[0] is an object, thanks too [0] => stdClass Object; and you wanted to access the ID? So we need another ->
$stdClass->List_inserted[0]->ID

Push array object into another array object in php

I'm getting an error while pushing one object into another object. But the 2nd object is an array and inside an array there is an object. How can I fix this cause I want to add that into my object
My object just like this
I want to add the the Object2 into Object1
Objet1
stdClass Object
(
[id_laporan_pemeriksa] => 5
[no_pkpt] => SNE
[tgl_pkpt] => 2010
[no_penugasan] => ST-4000/PW25/2/2017
[tgl_penugasan] => 2017-08-09
[judul_laporan] => Masukkan Kode disini
[no_laporan] => LBINA-9000/PW25/2/2017
[tgl_laporan] => 2017-08-01
[tahun_anggaran_penugasan] => 2009
[nilai_anggaran_penugasan] => 10000000
[realisasi_anggaran_penugasan] => 100000000
[jenis_anggaran_penugasan] => Utang
[sumber_laporan] => Inspektorat Maluku
[nama_sumber_penugasan] => PKPT
[nama_ketua_tim] => Abdul Rofiek, Ak.
[nama_pengendali_teknis] => Alfian Massagony, S.E.
[nama_unit_penugasan] => Irban Wil. I
[nama_penugasan] => Penjaminan
[nama_sub_penugasan] => Audit
[id_s_sub_penugasan] => 010105
[nama_s_sub_penugasan] => Audit atas hal-hal lain di bidang kepegawaian.
)
Object2
stdClass Object
(
[id] => 3
[data_sebab] => Array
(
[0] => stdClass Object
(
[id] => 4
[data_rekomendasi] => Array
(
[0] => stdClass Object
(
[id] => 4
[data_tindak_lanjut] => Array
(
[0] => stdClass Object
(
[id] => 9
[tgl_tindak_lanjut] => 0000-00-00
)
)
)
[1] => stdClass Object
(
[id] => 5
[id_rekomendasi] =>
[data_tindak_lanjut] => Array
(
[0] => stdClass Object
(
[id] => 10
[id_tindak_lanjut] =>
[tgl_tindak_lanjut] => 0000-00-00
)
[1] => stdClass Object
(
[id] => 11
[id_tindak_lanjut] =>
[tgl_tindak_lanjut] => 0000-00-00
)
)
)
)
)
)
)
I have tried
$Object1['data']->$Object2;
But i got an error
Cannot use object of type stdClass as array
The syntax of adding $Object2 as a property of $Object1 is:
$Object1->Object2 = $Object2;
Or:
$Object1->{'Object2'} = $Object2;
It should be:
$Object1->data = $Object2; // it will create data element with obj2 as value
As the objects are objects and not arrays, using:
$Object1['data']->$Object2;
wont work. However doing the following will work:
$Object1->data = $Object2;

PHP Access Object

I am new to Objects , and I've been playing with this little object. Now I would like to know how I can acess an specific element of it .
This is the object :
stdClass Object ( [responseHeader] => stdClass Object ( [status] => 0 [QTime] => 0 ) [response] => stdClass Object ( [numFound] => 1 [start] => 0 [docs] => Array ( [0] => stdClass Object ( [schoolname_s] => School of Art [schoolstate_s] => FL [schoolcity_s] => Miami [pk_id] => 111212 [schoolcountry_s] => United States [name_s] => Example ) ) ) )
I would like to print pk_id.
I have tried :
$phpArray->pk_id;
And this was the result:
Notice: Undefined property: stdClass::$pk_id in
You aren't accessing the right variable. What you want is this:
$phpArray->response->docs[0]->pk_id

PHP Printing JSON value

I have a JSON array that I want to be able to drill down to a lower level and print just that value. The problem occurs when I reach a level that has is indacted as [0] (or [n]). For example I have the following output, and I want to just print the game key for the first league.
This is how I am trying to print it
HtmlSpecialChars(print_r($user->fantasy_content->users[0]->user[1]->games[0]->game[0]->game_key,1))
However I keep getting this error:
Cannot use object of type stdClass as array
When I do it incrementally it seems to fail on this command (so I assume I'm not index correctly):
$user->fantasy_content->users[0]
Here is the output:
stdClass Object
(
[fantasy_content] => stdClass Object
(
[xml:lang] => en-US
[yahoo:uri] => /fantasy/v2/users;use_login=1/games
[users] => stdClass Object
(
[0] => stdClass Object
(
[user] => Array
(
[0] => stdClass Object
(
[guid] => IYEZUHTVBYRLIB3OAQC5WRZPQY
)
[1] => stdClass Object
(
[games] => stdClass Object
(
[0] => stdClass Object
(
[game] => Array
(
[0] => stdClass Object
(
[game_key] => 147
[game_id] => 147
[name] => Baseball
[code] => mlb
[type] => full
[url] => http://baseball.fantasysports.yahoo.com/b1
[season] => 2006
)
)
)
[count] => 1
)
)
)
)
[count] => 1
)
[time] => 52.390813827515ms
[copyright] => Data provided by Yahoo! and STATS, LLC
[refresh_rate] => 60
)
)
For objects you must use the -> syntax and if the key/property name is a number or has other special characters, you will need to use the $object->{'0'} syntax.
The game_key can be retrieved using:
$user->fantasy_content->users->{'0'}->user[1]->games->{'0'}->game[0]->game_key;
You can convert a stdClass object to an array by casting it like so:
<?php
$array = (array) $myObject;
echo json_encode($array);
You can also cast inline:
<?php
echo json_encode((array) $object);

Return value when searching for key in array PHP

I tried to get an answer for this in other posts with no luck, hope someone can help me here, i have a multidimensional array:
Array (
[0] => stdClass Object (
[affectsVersions] => Array ( )
[assignee] => hmontes
[attachmentNames] => Array ( )
[components] => Array ( )
[created] => 2012-08-15T05:31:26.000Z
[customFieldValues] => Array (
[0] => stdClass Object (
[customfieldId] => customfield_10201
[key] => [values] => Array (
[0] => 123456
)
)
[1] => stdClass Object (
[customfieldId] => customfield_10004
[key] => [values] => Array (
[0] => 30
)
)
)
[description] => [duedate] => [environment] => [fixVersions] => Array ( )
[id] => 10228
[key] => NTP-29
[priority] => 3
[project] => NTP
[reporter] => hmontes
[resolution] => [status] => 1
[summary] => case 123456
[type] => 3
[updated] => 2012-08-15T05:31:26.000Z
[votes] => 0
)
)
this is what i get when i do a print_r with the array variable, i need to search and get the value from [key] that would be in this case NTP-29 and keep it in a variable as string.
You can get the value of an array by the key using $array['keyName'];
But, for you it looks like you just need to go deeper $array[0]['key'];
Both arrays values and properties of objects can be accessed using associative array syntax. To get the value of the key property in your object within the array you'd do the following, assuming $array is a variable containing a reference to your array:
$key = $array[0]['key']; // accesses NTP-29 in this case.
Here's another way to access the same property, using object property-access syntax:
$key = $array[0]->key; // also accesses NTP-29.

Categories