PHP read first item in object array - php

I have my code in PHP which is returning this Array of data:
GoCardlessPro\Core\ListResponse Object
(
[records] => Array
(
[0] => GoCardlessPro\Resources\Mandate Object
(
[model_name:protected] => Mandate
[created_at:protected] => 2017-04-01T16:49:09.642Z
[id:protected] => ID001
[links:protected] => stdClass Object
(
[customer_bank_account] => CB001
[creditor] => CR001
[customer] => CU001
)
[metadata:protected] => stdClass Object
(
)
[next_possible_charge_date:protected] => 2017-04-06
[payments_require_approval:protected] =>
[reference:protected] => RE001
[scheme:protected] => bacs
[status:protected] => active
[data:GoCardlessPro\Resources\BaseResource:private] => stdClass Object
(
[id] => 123
[created_at] => 2017-04-01T16:49:09.642Z
[reference] => RE001
[status] => active
[scheme] => bacs
[next_possible_charge_date] => 2017-04-06
[payments_require_approval] =>
[metadata] => stdClass Object
(
)
[links] => stdClass Object
(
[customer_bank_account] => 001
[creditor] => CR001
[customer] => CU001
)
)
[api_response] =>
)
)
)
I want to be able to read the ID of the first item in therecords array.
This data is contained inside a variable called $GC_Mandate;
I have tried these:
echo $GC_Mandate->records->{0}->id;
echo $GC_Mandate->records->0->id;
echo $GC_Mandate->records->[0]->id;
$GC_Mandate = $GC_Mandate->records;
echo $GC_Mandate->{0}->id;
But none will return the data

To get the first record, the syntax you need is $GC_Mandate->records[ 0 ].
However, that object is a GoCardlessPro\Resources\Mandate object and its member id is protected1, so we'd need to know the interface of GoCardlessPro\Resources\Mandate (its public methods1), to know if we can somehow retrieve the value of id.
My guess would be getId(), so the full syntax would become
$GC_Mandate->records[ 0 ]->getId()
But, that's just a guess. You'd have to look into the documentation/class definition of GoCardlessPro\Resources\Mandate, to be sure if you can retrieve id.
Turns out (provided I'm linking to the correct github repository) you can do:
$GC_Mandate->records[ 0 ]->id
since GoCardlessPro\Resources\Mandate extends GoCardlessPro\Resources\BaseResource, which exposes the protected members through GoCardlessPro\Resources\BaseResource::__get()2.
1. visibility in PHP
2. magic methods in PHP

I can't comment so I guess I'll have to post.
You should try to print_r($GC_Mandate); and see what it gives out and then go from there.

Try $GC_Mandate->records[0]->__get('id')

it will return all data ..for perticulat data put this in foreach loop
print_r($GC_Mandate['records']);

Related

How to display info from an array in PHP?

If my array states:
[mostPlayedGames] => Array ( [0] => stdClass Object ( [gameName] => Counter-Strike: Global Offensive [gameLink] => http://steamcommunity.com/app/730 [gameIcon] => http://cdn.akamai.steamstatic.com/steamcommunity/public/images/apps/730/69f7ebe2735c366c65c0b33dae00e12dc40edbe4.jpg [gameLogo] => http://cdn.akamai.steamstatic.com/steamcommunity/public/images/apps/730/d0595ff02f5c79fd19b06f4d6165c3fda2372820.jpg [gameLogoSmall] => http://cdn.akamai.steamstatic.com/steamcommunity/public/images/apps/730/d0595ff02f5c79fd19b06f4d6165c3fda2372820_thumb.jpg [hoursPlayed] => 28.0 [hoursOnRecord] => 527 [statsName] => CSGO ) [1] => stdClass Object ( [gameName] => Borderlands: The Pre-Sequel [gameLink] => http://steamcommunity.com/app/261640 [gameIcon] => http://cdn.akamai.steamstatic.com/steamcommunity/public/images/apps/261640/af5ef05eac8b1eb618e4f57354ac7b3e918ab1bd.jpg [gameLogo] => http://cdn.akamai.steamstatic.com/steamcommunity/public/images/apps/261640/df64c72fd335a03dbcc0a19b1f81acc8db1b94ba.jpg [gameLogoSmall] => http://cdn.akamai.steamstatic.com/steamcommunity/public/images/apps/261640/df64c72fd335a03dbcc0a19b1f81acc8db1b94ba_thumb.jpg [hoursPlayed] => 10.9 [hoursOnRecord] => 10.9 [statsName] => 261640 )
and I want to display info from the first part of the array( 0 ), how would I go about doing that if I was using code like this to display it:
echo "CS:GO Hours Played: {$user->mostPlayedGames???}, PHP_EOL;
Thank you for your time.
Your mostPlayedGames array doesn't seem like it is an stdClass of any variable named $user, so let's not over complicate it.
$mostPlayedGames = [mostPlayedGames] => Array ( [0] => stdClass Object ( [gameName]....[snippet]
Now that we have that clear:
echo "CS:GO Hours Played: ${mostPlayedGames[0]->hoursPlayed}".PHP_EOL;
You see, The first element in this is an array at position 0 so we must first move to that index position. The element at index 0 is an stdClass so then we can use the accessor method -> to grab properties of this object.

Kaltura KalturaMetadataListResponse print_r result

I get the following returned result from a print_r of a variable returned from a $metadataPlugin->metadata->listAction($filter, $pager) function. I am trying to echo out the [xml] piece but can't figure out how to do it.
KalturaMetadataListResponse Object ( [objects] => Array ( [0] => KalturaMetadata Object ( [id] => 1 [partnerId] => 8 [metadataProfileId] => 2 [metadataProfileVersion] => 1 [metadataObjectType] => 1 [objectId] => 0 [version] => 1 [createdAt] => 1353093894 [updatedAt] => 1353093894 [status] => 1 [xml] => 1353049200 ) ) [totalCount] => 1 )
I have tried
echo $result['objects'][0]->xml;
and
echo $result[0]->xml;
with no success.
It looks like it should be
$result->objects[0]->xml
Just remember when looking at print_r or var_dump results that any time you see Object (...) that you will need to use -> to access the property listed for that object.
So in this case, you have a base Object set as $result, which you then need to get the objects property from. That property contains an array. At index 0 of the array, is another Object, which is the Object where your xml property resides.

joomla 1.6 : how $app object print out the entire page contents

I was checking joomla 1.6 index.php and I found the following code at the last line
echo $app;
this prints the entire page contents.
I just printed out the contents in this object using print_r() and I got the following details
JSite Object
(
[template:JSite:private] => stdClass Object
(
[id] => 6
[home] => 1
[template] => beez5
[params] => JRegistry Object
(
[data:protected] => stdClass Object
(
[wrapperSmall] => 53
[wrapperLarge] => 72
[logo] => images/sampledata/fruitshop/fruits.gif
[sitetitle] => Matuna Market
[sitedescription] => Fruit Shop Sample Site
[navposition] => left
[html5] => 0
)
)
)
[_language_filter:JSite:private] =>
[_detect_browser:JSite:private] =>
[_clientId:protected] => 0
[_messageQueue:protected] => Array
(
)
[_name:protected] => site
[scope] =>
[requestTime] => 2011-10-17 17:23
[startTime] => 1318872200.5365
[_errors:protected] => Array
(
)
)
so how echo $app display all the site contents, it doesn't contains any HTML contents in the object.
Thank you very much
It declares the magic method __toString() in the class.
If this function is declared in a class, the return value of it will be used when the object is casted to a string.
Simple example: http://codepad.org/UmZUQA3v
$app is an object, and print_r accesses its values in different ways from echo. When echo is called, it also implicitly calls the magic __toString method. That has been defined such that it returns a string with the page contents, given the values stored inside of the object. print_r will give you those values, but not the __toString representation.

PHP - Converting object array to usable object

I have an array which contains status objects, these status objects contain an array of like objects and also contain comment objects
My question is that now I have the objects in my array, how do I pull them back out? This is so I can save them to a db later on.
Thanks for your help
Andy
e.g.
Array
(
[0] => cStatus Object
(
[statusId:cStatus:private] => 123123123
[message:cStatus:private] => powpowpow
[updated_time:cStatus:private] => 2011-01-27T15:52:48+0000
[likes:cStatus:private] => Array
(
)
[comments:cStatus:private] => Comment Object
(
[commentId:Comment:private] => 123123123
[created_time:Comment:private] => 2011-01-30T20:18:50+0000
[message:Comment:private] => Kazam
[name:Comment:private] => Blue man
[createdBy:Comment:private] => 124124
[likes:Comment:private] => Array
(
)
)
)
[1] => cStatus Object
(
[statusId:cStatus:private] => 5125125
[message:cStatus:private] => Gawdam fruit and fibre is tasty :D
[updated_time:cStatus:private] => 2011-01-25T20:21:56+0000
[likes:cStatus:private] => Array
(
[0] => Like Object
(
[likeId:Like:private] => 120409086
[name:Like:private] => Jt
)
)
[comments:cStatus:private] => Array
(
)
)
[2] => cStatus Object
(
[statusId:cStatus:private] => 5215215
[message:cStatus:private] => Dear 2
[updated_time:cStatus:private] => 2011-01-18T08:28:50+0000
[likes:cStatus:private] => Array
(
[0] => Like Object
(
[likeId:Like:private] => 2456
[name:Like:private] => Edw2r
)
[1] => Like Object
(
[likeId:Like:private] => 2452412
[name:Like:private] => aw1
)
[2] => Like Object
(
[likeId:Like:private] => 12412411
[name:Like:private] => wqw
)
)
[comments:cStatus:private] => Array
(
)
)
)
You can use foreach and access properties of individual objects to be saved. I assume you are using getter and setter methods since all your properties are private. Using foreach provides the "as" keyword to make an alias for each individual object instance as the loop executes among them.
<?foreach($obj as $status){
$status_text = $status->getMessage();
//save this to database using your favored method;
$comments = $status->getComments();
//nest the foreach for all the comments to save them as well, if you like
foreach($comments as $comment){
//Save $comment here as well
}
}
?>
This is especially handy for complex nested objects like yours, since public methods and properties can be accessed by the individual iterator for easy action, like saving to the database.

foreach loops & stdclass objects

I've seen similar questions on here but I can't seem to apply the solutions to my problem. I have a variable called $results which I got from an API. I'll change the proper nouns so as to protect my work's customers:
stdClass Object
(
[out] => stdClass Object
(
[count] => 2
[transactions] => stdClass Object
(
[RealTimeCommissionDataV2] => Array
(
[0] => stdClass Object
(
[adId] => 12345678
[advertiserId] => 123456789
[advertiserName] => Chuck E. Cheese, inc.
[commissionAmount] => 50
[country] => US
[details] => stdClass Object
(
)
[eventDate] => 2009-11-16T09:44:25-08:00
[orderId] => X-XXXXXXXXXX
[saleAmount] => 0
[sid] => 123456789
[websiteId] => 2211944
)
[1] => stdClass Object
(
[adId] => 987654321
[advertiserId] => 12345
[advertiserName] => Chorizon Wireless.
[commissionAmount] => 50
[country] => US
[details] => stdClass Object
(
)
[eventDate] => 2009-11-16T09:58:40-08:00
[orderId] => X-CXXXXXX
[saleAmount] => 0
[sid] => 61-122112
[websiteId] => 1111922
)
)
)
)
)
I shortened it to two entries here but the number of entries will vary, it's the result of a check for transactions in the past hour, there may sometimes be only one and sometimes as many as a dozen.
I want to assign these entries to variables like websiteId1 websiteId2 etc. I know I need to do a foreach loop but can't seem to figure it out. How can I write it so that I get the "[details]" as well?
foreach ($results->out->transactions->RealTimeCommissionDataV2 AS $commissionData) {
// you can access the commissionData objects now, i.e.:
$commissionData->adId;
$commissionData->details;
}
<?
foreach ($result->out->transactions->RealTimeCommissionDataV2 as $item)
{
// do somthing with each item.
print_r($item);
// or the details array
$num_details = sizeof($item->details)
}
I think this is what you want.
EDIT
Updated based on some notes in the documentation. Specifically, these two
a numerically indexed array will not
produce results unless you use
EXTR_PREFIX_ALL or
EXTR_PREFIX_INVALID.
Prefixes are automatically separated
from the array key by an underscore
character.
echo extract( $results->out->transactions->RealTimeCommissionDataV2, EXTR_PREFIX_ALL, 'websiteId' );
// test the extract
print_r( $websiteId_0 );

Categories