How would I read this array ("stdClass Object") - php

I am using the Quizlet API 2.0, and I am pretty new to this
How do I read a value(s) from something like this:
stdClass Object ( [id] => 102269 [name] => Learn Spanish with Cats! [set_count] => 3 [user_count] => 10 [created_date] => 1308035691 [is_public] => 1 [has_password] => [has_access] => 1 [has_discussion] => 1 [member_add_sets] => 1 [description] => This set is exclusively for Spanish flashcard sets with relevant cat images as the set definitions. [sets] => Array ( [0] => stdClass Object ( [id] => 6081999 [url] => http://quizlet.com/6081999/lesson-4-with-catsdogs-flash-cards/ [title] => Lesson 4 (with cats+dogs) [created_by] => wsvincent [term_count] => 33 [created_date] => 1311984796 [modified_date] => 1312490710 [has_images] => 1 [subjects] => Array ( [0] => spanish cats dogs ) [visibility] => public [editable] => groups [has_access] => 1 ) [1] => stdClass Object ( [id] => 5855751 [url] => http://quizlet.com/5855751/paso-a-paso-book-1-chapter-4-flash-cards/ [title] => Paso a Paso Book 1 Chapter 4 [created_by] => catdawg426 [term_count] => 30 [created_date] => 1307761267 [modified_date] => 1307819129 [has_images] => 1 [subjects] => Array ( [0] => spanish ) [visibility] => public [editable] => only_me [has_access] => 1 ) [2] => stdClass Object ( [id] => 5873819 [url] => http://quizlet.com/5873819/los-gatos-de-viaje-flash-cards/ [title] => Los Gatos de Viaje! [created_by] => tiffreja [term_count] => 21 [created_date] => 1307996657 [modified_date] => 1307996796 [has_images] => 1 [subjects] => Array ( [0] => spanish [1] => language [2] => foreign ) [visibility] => public [editable] => only_me [has_access] => 1 ) ) [members] => Array ( [0] => stdClass Object ( [username] => philfreo [role] => creator [email_notification] => 1 ) [1] => stdClass Object ( [username] => milkncookies [role] => member [email_notification] => 1 ) [2] => stdClass Object ( [username] => Icypaw [role] => member [email_notification] => ) [3] => stdClass Object ( [username] => luckycat10 [role] => member [email_notification] => ) [4] => stdClass Object ( [username] => jeffchan [role] => member [email_notification] => ) [5] => stdClass Object ( [username] => catchdave [role] => member [email_notification] => 1 ) [6] => stdClass Object ( [username] => tiffreja [role] => member [email_notification] => 1 ) [7] => stdClass Object ( [username] => catdawg426 [role] => member [email_notification] => 1 ) [8] => stdClass Object ( [username] => ihaque [role] => member [email_notification] => 1 ) [9] => stdClass Object ( [username] => jalenack [role] => member [email_notification] => 1 ) ) )
For instance, if I want to get the name of that first set, "Learn Spanish with Cats", how do I echo it via variable?
It already converts the JSON to an array I think:
$data = json_decode($json);

Your object is not an array, but rather, well, an Object. So use the -> operator to access its properties:
echo $data->name;
It contains a property which itself is an array of additional objects. For example, to get the URL of id 6081999, you would do:
echo $data->sets[0]->url;
// http://quizlet.com/6081999/lesson-4-with-catsdogs-flash-cards/

Here is a simple solution to convert a stdClass Object in array in php with get_object_vars
Look at : https://www.php.net/manual/en/function.get-object-vars.php
Example :
dump($array);
$var = get_object_vars($array);
dump($var);
Or replace dump() function by print_r()

Use function key
eg echo key($array)

I have looked something before, when you use the json_decode()
$data = json_decode();
U can send some parameters, the first of them is "$json", it will be the json string
{"1":"first","2":"second"}
But that json decode with one parameter return an Object and the default value of the second parameter is "false". If you want that back in array you only need to use the second parameter and send "true".
$data =json_decode($json,true);
And you can recibe it like an array. :)

In case you got stdClass Object in the array, for example
$user = $result1->fetch_object() then set $user into a variable
$val = $user->user_id (make sure user_id is your database column name, its the column name). You will get a single value in $val that comes from database.

Related

PHP Please tell me how to access the Object key

[zzcrewssadd] => stdClass Object
(
[items] => stdClass Object
(
[item] => stdClass Object
(
[0] => abc
[#0] => stdClass Object
(
[no] => 1
[id] => 01_00_00_00_00
[depth] => 1
[itemCnt] => 126647
)
[1] => def
[#1] => stdClass Object
(
[no] => 2
[id] => 01_01_00_00_00
[depth] => 2
[itemCnt] => 37119
)
[2] => ghi
[#2] => stdClass Object
(
[no] => 3
[id] => 01_01_01_00_00
[depth] => 3
[itemCnt] => 13730
)
)
)
)
How do I approach it?
The code I tried.
zzcrewssadd->items->item[#0];
This will result in an error.
This will result in an error.
This will result in an error.
This will result in an error.
Why don't you run a foreach loop on this zzcrewssadd->items->item
or try this to access object keys
echo zzcrewssadd->items->item->{'#0'};

how to retrieve variable from object

I want to retrieve $se_module value from my object. How can I get that? I can easily get others by $object->Owner->name. I've tried $object->{$se_module}, but got error as undefined variable.
stdClass Object
(
[Owner] => stdClass Object
(
[name] => Merv Henry
[id] => 2880896000003943001
)
[Modified_Time] => 2019-01-09T21:25:05-05:00
[$attachments] =>
[Created_Time] => 2019-01-09T21:25:05-05:00
[Parent_Id] => stdClass Object
(
[name] =>
[id] => 2880896000011553097
)
[$editable] => 1
[$se_module] => Deals
[Modified_By] => stdClass Object
(
[name] => Merv Henry
[id] => 2880896000003943001
)
[$size] =>
[$voice_note] =>
[id] => 2880896000011676081
[Created_By] => stdClass Object
(
[name] => Merv Henry
[id] => 2880896000003943001
)
[Note_Title] =>
[Note_Content] => This unit needs 1# DA97-12540K & 1# DA97-13718C
Quote is needed
)
you cane make it as array then you can get it
$data = (array)$se_module;
then can get data like this
$data['Modified_Time'];

Softalyer API - calling getBlockDeviceTemplateGroups

we have a problem in using getBlockDeviceTemplateGroups(): It returns always "blockDevicesDiskSpaceTotal" values as zero.
PHP CODE:
$mask = "mask[name, blockDevicesDiskSpaceTotal, storageRepository[billingItem[associatedBillingItem]], createDate, datacenter, datacenters,id,parentId]";
$client = SoftLayer_SoapClient::getClient('SoftLayer_Account', null, $apiUsername, $apiKey);
$client->setObjectMask($mask);
//Retrieve private template group objects (parent and children) and the shared template group objects (parent only) for an account.
$templates = $client->getBlockDeviceTemplateGroups();
OUTPUT of print_r($templates):
Array
(
[0] => stdClass Object
(
[createDate] => 2016-02-29T17:42:04+01:00
[id] => 966271
[name] => 20160229-JMeter-Master.bluereply.it
[parentId] =>
[blockDevicesDiskSpaceTotal] => 0
[datacenters] => Array
(
[0] => stdClass Object
(
[id] => 814994
[longName] => Amsterdam 3
[name] => ams03
[statusId] => 2
)
[1] => stdClass Object
(
[id] => 815394
[longName] => Milan 1
[name] => mil01
[statusId] => 2
)
)
)
[1] => stdClass Object
(
[createDate] => 2016-02-29T17:31:10+01:00
[id] => 966247
[name] => 20160229-JMeter-Slave.bluereply.it
[parentId] =>
[blockDevicesDiskSpaceTotal] => 0
[datacenters] => Array
(
[0] => stdClass Object
(
[id] => 815394
[longName] => Milan 1
[name] => mil01
[statusId] => 2
)
[1] => stdClass Object
(
[id] => 814994
[longName] => Amsterdam 3
[name] => ams03
[statusId] => 2
)
[2] => stdClass Object
(
[id] => 449500
[longName] => Paris 1
[name] => par01
[statusId] => 2
)
)
and so on...
Thanks in advance
try using this objectMask:
mask[name, blockDevicesDiskSpaceTotal, storageRepository[billingItem[associatedBillingItem]],children[blockDevicesDiskSpaceTotal,datacenter], createDate, datacenter, datacenters,id,parentId]
the problem is that the images templates have a container which stores the real images templates for each datacenter, currently you are getting the "blockDevicesDiskSpaceTotal" of the container which is empty, you need to get that information form the container's children.
Regards

How to make single array of two array object?

How to make single array array object of two array object
I have the following two arrays of objects
How to make single array array object of two array object
I have the following two arrays of objects
Array
(
[0] => stdClass Object
(
[id] => 25
[authorityId] => 2
[grantVillage] => test
[agency] => test
[certificate] => Panding
[amount] => 50000
[startDate] => 2015-12-08
[endDate] => 2015-12-08
[grantArea] => test
[added_date] => 0000-00-00
)
[1] => stdClass Object
(
[id] => 26
[authorityId] => 2
[grantVillage] => testing2
[agency] => testing
[certificate] => Verified
[amount] => 50000
[startDate] => 1970-01-01
[endDate] => 1970-01-01
[grantArea] => test
[added_date] => 0000-00-00
)
)
Array
(
[0] => stdClass Object
(
[name] => kandi Area
)
[1] => stdClass Object
(
[name] => kandi Area
)
)
I want to make it:
Array
(
[0] => stdClass Object
(
[name] => kandi Area
[id] => 25
[authorityId] => 2
[grantVillage] => test
[agency] => test
[certificate] => Panding
[amount] => 50000
[startDate] => 2015-12-08
[endDate] => 2015-12-08
[grantArea] => test
[added_date] => 0000-00-00
)
[1] => stdClass Object
(
[name] => kandi Area
[id] => 26
[authorityId] => 2
[grantVillage] => testing2
[agency] => testing
[certificate] => Verified
[amount] => 50000
[startDate] => 1970-01-01
[endDate] => 1970-01-01
[grantArea] => test
[added_date] => 0000-00-00
)
)
Any help will be appreciated.Thanks in advance.
You can merge the two array objects and can get an array object by following this question :
How to merge two arrays of object in PHP
Once you get the merge array object. You can convert the array object(std class) into array by this code snippet :
$array = json_decode(json_encode($ArrayObject),true);
You can simply use array_merge for this:
$combinedArray = array_merge($array1, $array2);
Edit: Misunderstood your question, the following would work as you simply need to loop through array 2 and add these values to array 1:
<?php
$array1 = array (
0 => array (
"id" => "25",
"authorityId" => "2",
),
1 => array (
"id" => "26",
"authorityId" => "2",
)
);
$array2 = array (
0 => array (
"name" => "kandi Area"
),
1 => array (
"name" => "kandi Area"
)
);
foreach ($array2 as $array2Key => $array2ValuesArray) {
# This works for serialized arrays:
// $array1[$array2Key]['name'] = $array2ValuesArray['name'];
# However this does not relate to the OP which means that this will work for stdClass Objects:
$array1[$array2Key]->name = $array2ValuesArray->name;
}
echo '<pre>';
print_r($array1);
echo '</pre>';
?>
Edit: This results in the following output:
array
(
[0] => stdClass Object
(
[id] => 25
[authorityId] => 2
[name] => kandi Area
),
[1] => stdClass Object
(
[id] => 26
[authorityId] => 2
[name] => kandi Area
)
)

Access object with in array

Array
(
[0] => Contact Object
(
[id] => 1
[status] => ACTIVE
[first_name] => Ahmed
[middle_name] =>
[last_name] => Taibah
[confirmed] =>
[source] =>
[email_addresses] => Array
(
[0] => EmailAddress Object
(
[id] => 1
[status] => ACTIVE
[confirm_status] => NO_CONFIRMATION_REQUIRED
[opt_in_source] => ACTION_BY_OWNER
[opt_in_date] => 2009-07-23T05:54:30.315Z
[opt_out_date] =>
[email_address] => info#test.sa
)
)
How can i get the value of [confirm_status] as this is under object then array then again object then this index value i need.
$arr[0]->email_addresses[0]->confirm_status
$contact[0]->email_addresses[0]->confirm_status
You can do something like this.
You can use this like
$confirm_status = $arr['email_addresses'][0]->confirm_status;

Categories