I'm trying to compare two objects like this:
if($obj1==$obj2){
...
But my comparison always return false!
$obj1 is a param of my function (called through a Webservice) of type "MySoapObject".
$obj2 is an object of type "MySoapObject" loaded from my database.
In my test print_r of my both objects give me this:
stdClass Object
(
[establishment] => stdClass Object
(
[f_establishment_id] => 4
[f_name] => MyEstablishment
[f_display_name] => v
[f_main_web_link] =>
[f_contact] =>
[f_country_code] =>
[f_city] =>
[f_zip_code] =>
[f_postal_address] =>
[f_default_language] =>
[f_timezone] =>
[f_cloud_synchronisation_action] => TO_SYNC
)
[site_list] => Array
(
[0] => stdClass Object
(
[f_site_id] => Site1
[f_status] => TO_DISABLE
[f_name] => Site1
)
)
)
By the way, if I try this:
if(print_r($obj1)==print_r($obj2)){
...
The result is true!
Have you an idea of where could be the problem? How to solve it?
Thanks for your help.
for object comparison you can use :
if($obj1===$obj2){
}
check the documentation
http://php.net/manual/en/language.operators.comparison.php
Related
CoinGate\Merchant\Order Object
(
[order:CoinGate\Merchant\Order:private] => Array
(
[id] => 97977
[status] => new
[do_not_convert] =>
[price_currency] => USD
[price_amount] => 1200.0
[lightning_network] =>
[receive_currency] => EUR
[receive_amount] =>
[created_at] => 2018-07-03T05:53:43+00:00
[order_id] => 459469
[payment_url] => https://sandbox.coingate.com/invoice/94423345-1a1a-4895-a08e-98793777b0d0
[token] => x5Yrx5mmku8nkyK2ShVvbCuiJfasoxsNBtxZ27Ra
)
)
This is response of request but i am facing to get values from such array. I need to get payment_url from such array.
In a Order.php file there's a magic __get method. So, you should use it to get the property you need:
// suppose $response is the value you `var_dump`ed in a question.
echo $response->payment_url; // same for other properties: $response->status
get_object_vars function in PHP convert object to array. Consider you are having this object in variable $x, then you should do like:
$y = get_object_vars($x);
echo $y['payment_url'];
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']);
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.
I have an array $presettings
print_r($presettings); outputs:
Array (
[0] => stdClass Object (
[uuid] => xxx-1ef8-aac6-xxx-xxx
[name] => etime
[owner] => eder112T Resident
[online] => 1
[channel] => 63b525ae-xxx-3555-1c74-xxx
[owner_uuid] => a371751c-eb77-xxx-899c-xxx
[simname] => Plainfield
[slurl] => xxx://xxx/xx/xx/243/24/xx/?title=xx
[design] => 2
[msg_oftheday] => two
[machine_name] => one
[autopay] =>
[autolog_leave] =>
[autolog_offline] =>
[allow_activation] =>
)
)
and now i want to get a special key:
echo "test output : "$presettings['machine_name']." testend";
outputs "" (nothing).
my method look like this
function preloadSettingsFromMYSQL($ownername,$prim_uuid)
{
$result = $this->instance->get_rows("SELECT * FROM etime_rims where owner='".$ownername."' AND uuid='".$prim_uuid."'");
return $result;
}
$result is an object array, also tried it with $presettings->machine_name, did not work too.
where is the error?
thank you.
If you look closely in the print_r result, you can see that there is a 0 there, meaning that those values aren't directly in $presettings, but actually in the first element of $presettings.
Just try:
$presettings[0]->machine_name
You object is multi dimensional so just add a level
$presettings[0]->machine_name;
As side note you have an object here and not array do don't try to access alues with scopes.
In your array, you have stdClass Object at index 0. You're looking to access the object's variables, which is a slightly different syntax than arrays:
echo $presettings[0]->machine_name;
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.