i am using a zend model which returns me an object in form of $row with all values
but i am not able to get value from this array . is this posible to get values without foreach
this is the array returned
Zend_Db_Table_Row Object
(
[_data:protected] => Array
(
[user_id] => 2
[udid] => 34
[firstname] => a
[lastname] => a
[email] => jusic.sl#gmail.com
[username] => abc
[password] => c91718531fd9f8b89c4e
[created_date] => 2010-02-11
[updated_datetime] => 2012-06-25 12:48:17
[lastlogin_datetime] =>
[group_id] => 2
[status] => Active
)
)
i need to get the user_id,firstname,email from this array
any help will be appreciated .
i have tried like
$forgotpassword = $userModel->forgotpassword ( $post ); // which contains this array
$id = $forgotpassword['_data:protected']['id']; exit; // but doesnt seem to work
You cannot access _data directly. It's protected.
From the ZF Reference Guide on Naming Conventions:
[…] variables that are declared with the "private" or "protected" modifier, the first character of the variable name must be a single underscore.
You can do either do (due to __get/__set)
echo $forgotpassword->user_id;
or (due to ArrayAccess)
echo $forgotpassword['user_id'];
or (if you want an array)
$array = $forgotpassword->toArray();
echo $array['user_id'];
Please see the Reference Guide and the code
http://framework.zend.com/manual/en/zend.db.table.row.html
http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Db/Table/Row/Abstract.php
Related
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']);
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;
array(
[key] => Array
(
[229] => Tuple Object
(
[ID] => 1
[NAME] => abc
)
[233] => Tuple Object
(
[ID] => 2
[NAME] => abcd
)
...
...
...
)
[moreinfo] = 'xyz'
)
issue is when i am using json_encode tuple object is getting lost as all data types like id,name are private.
Following Solution i have found
change private variables to public, still i can access the variables as array instead of objects :-(
write a wrapper which convert object to array...... an extra effort
can any one suggest a solution in which i can use objects.
I can't seem to get specific data from an array inside an object.
$this->fields->adres gets the address correctly, but i can't get a level deeper.
I've tried:
$this->fields->province
$this->fields->province->0
$this->fields->province[0]
And: (edit)
$this->fields["province"][0]
$this->fields['province'][0]
$this->data->fields['province'][0]
But it does not return anything while it should return "Flevoland".
First part of the object print_r($this, TRUE) below:
RSMembershipModelSubscribe Object
(
[_id] => 2
[_extras] => Array
(
)
[_data] => stdClass Object
(
[username] => testzz
[name] => testzz
[email] => xxxx#example.com
[fields] => Array
(
[province] => Array
(
[0] => Flevoland
)
[plaats] => tesdt
[adres] => test
You can also use type casting.
$fields = (array) $this->data->fields;
echo $fields['province'][0];
As you can see by your output, object members are likely to be private (if you follow conventions, anyway you must prepend an underscore while calling them), so you're calling them the wrong way;
This code works:
$this->_data->fields['province'][0];
You can see it in action here;
I created a similar object, and using
$membership = new RSMembershipModelSubscribe();
echo $membership->_data->fields['province'][0];
outputs "Flevoland" as expected.
As fields is already an array, try this:
$this->fields['province'][0]
This assuming the [_data] object is $this.
Fields and province are both arrays, you should be trying $this->fields["province"][0]
$this->_data->fields['province'][0]
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.