I have some codes like blow,
class Tool{
function getData(){
$res = array('name'=>'jay','age'=>22,'job'=>'developer','ID'=>1233211234567);
$res = (object)$res;
var_dump(111,$res);
$resLog = $res;
$this->resFilter($resLog);
var_dump(222,$res);
//...log code
}
function resFilter($res){
unset($res->ID);
}
}
echo '<pre>';
$t = new Tool;
$t->getData();
die;
and I doubt why after call function resFilter in function getData the var_dump(222,$res); will be this :
int(111)
object(stdClass)#2 (4) {
["name"]=>
string(3) "jay"
["age"]=>
int(22)
["job"]=>
string(9) "developer"
["ID"]=>
int(1233211234567)
}
int(222)
object(stdClass)#2 (3) {
["name"]=>
string(3) "jay"
["age"]=>
int(22)
["job"]=>
string(9) "developer"
}
so you can see in second part of var_dump have no ID field? Who can help me , and tell me why?
update:
thanks for your answer, and I try this $resLog = clone($res); will work fine.
Best explanation is here, also with an example like yours :
http://php.net/manual/en/language.oop5.references.php
Long story short : When you apply the resFilter method you send the $resLog object as input and that object has the same identifier like $res because you made the $resLog = $res; It is rather the same thing if you had a normal variable but sent it with reference in a method.
When an object is sent by argument, returned or assigned to another
variable, the different variables are not aliases: they hold a copy of
the identifier, which points to the same object.
PHP implements shallow copies unless instructed otherwise. So when you copy the object you do not actually clone it completely, you work with a reference instead. Therefore the $this->resFilter($resLog); call will delete the ID propety in the referenced object too.
Related
I have a simple answer I just can't solution to:
var_dump(obj) =
object(stdClass)#15 (3) {
["properties"]=>
object(stdClass)#14 (2) {
["user_name"]=>
string(4) "somename"
["email_address"]=>
string(12) "test#test.com"
["arrays"]=>
object(stdClass)#17 (1) {
["sites[]"]=>
object(stdClass)#18 (4) {
["0"]=>
int(1)
["1"]=>
int(1)
["2"]=>
int(0)
["3"]=>
int(0)
}
}
}
How to call the 'sites[]' object in my 'obj'?
I tried the following:
obj->sites[]
obj->{'sites[]'}
Both options aren't working...
It might be better to clean up the code that generates that object, but you should be able to access the sites[] object via:
$sites = $obj->arrays->{'sites[]'};
However $sites will still be an object, so you would need to access its elements in a similarly awkward way:
echo $sites->{'0'};
It would be better to cast it to an array at that point:
$sites = (array) $obj->arrays->{'sites[]'};
Then you can access as an array:
echo $sites[0];
EDIT, seams you cannot access array elements indexed by a numerical string.
A better option (as discovered by a SO question i just posted about this) would be to use get_object_vars:
$sites = get_object_vars($obj->arrays->{'sites[]'});
Then you can access as an array:
echo $sites[0];
I have an array with the following contents:
$tester
array(1) {
[0]=>
object(CategoryItem)#79 (17) {
["type"]=>
string(0) ""
["addedInVersion"]=>
string(4) "0.02"
["lastUpdatedInVersion"]=>
string(4) "0.02"
["AToZ"]=>
bool(false)
["name"]=>
string(22) "Page Name"
["scopeNotes"]=>
string(0) ""
["historyNotes"]=>
string(13) "Added in 0.02"
["broaderItems"]=>
array(0) {
}
I want to echo out the name, if this case Page Name and then use this in an if statement.
I have but this errors, I also tried $tester->CategoryItem->name but no joy.
Is there anything obvious I am missing?
You need to access it like this:
$name = $tester[0]->name;
echo $name;
you have some leaks in your php OOP understanding, you should fix them by following some tutorials like these ones:
http://www.killerphp.com/tutorials/object-oriented-php/
http://code.tutsplus.com/tutorials/object-oriented-php-for-beginners--net-12762
http://www.tutorialspoint.com/php/php_object_oriented.htm
Now to answer your question, your code should be this:
$the_name = $tester[0]->name;
if($the_name == 'whatever value you want') {
echo $the_name;
}
first of all, your initial variable is a array, therefor, $tester[0], then, this position is an object of the class CategoryItem so you use scope: $tester[0]->value
As for the last of your values in the class properties, broaderItems, this is again an array, so to access one of his values, you will have to call it like:
$tester[0]->broaderItems[0]; //or whatever the keys you will have here
Hope this helps!
:D
I have been trying to figure this out for a while now. Please any advice would be appreciated. I just need to access the array for ["Item"]. How do I gain access to this?
array(1) {
[0]=>
object(SimpleXMLElement)#16 (2) {
["#attributes"]=>
array(2) {
["Name"]=>
string(10) "AuthorList"
["Type"]=>
string(4) "List"
}
["Item"]=>
array(3) {
[0]=>
string(9) "Smith, Joe"
[1]=>
string(10) "Peter, Ann"
[2]=>
string(18) "Magoo, Mr"
}
}
}
Assuming this structure is in a variable called var1, you should be able to access Item using:
$var1[0]->Item // returns the array
I try to explain it.
Like you see, your first array index is an object.
If you see something like this in your var_dump than you can access it through deferencing the object.
It's the same like you would create an object and would like to access a public variable:
$var1 = new Object();
// when your Object variables are public so you could access them by deference the Object
echo $var1->myVariable; // will echo the public variable "myVariable"
So the answer from adam is the right one :)
The PHP code I am interating through is as follows for the Update process:
$data = $_POST;
foreach ($data['answers'] as &$d):
if(!isset($d['default'])):
$d['default'] = "false";
endif;
endforeach;
And when I var_dump it after that iteration, I get the following:
array(2) {
["question"]=>
string(20) "Which did you like?"
["answers"]=>
array(6) {
[0]=>
array(2) {
["default"]=>
string(4) "true"
["option"]=>
string(5) "First"
}
[1]=>
&array(2) {
["option"]=>
string(5) "Second"
["default"]=>
string(5) "false"
}
}
}
As you can see, the second array has "&array" keyword, I am assuming that's implying a reference. My question is, can I serialize this array and save it into MYSQL DB? I was getting some data error on the display page after, so I want to make sure if this has anything to do with this.
UPDATE
Error message I get on the display page is that
Undefined index: option
From the docs:
serialize() handles all types, except the resource-type. You can even serialize() arrays that contain references to itself. Circular references inside the array/object you are serializing will also be stored. Any other reference will be lost.
About yout error message, post the line youre calling the "option" index, so we can see what is going wrong...
And as Sammitch said, you can use serialize() to store this data in your DB.
I have one object being the result of a database query, looking something like this (var_dump output):
object(stdClass)#17 (6) {
["id"]=>
string(1) "1"
["title"]=>
string(20) "Some Title"
["description"]=>
string(41) "Some really good stuff. Description here."
["slug"]=>
string(19) "some-slug-url"
["picture_url"]=>
NULL
["price"]=>
string(4) "5.99"
}
I just need all property values "transferred" to a different class which has the same property names. Is there a simple way to do this?
Take a look at PHP's get_object_vars()-function to achieve the desired effect without tons of assignment statements:
foreach (get_object_vars($sourceObject) as $name => $value) {
$destinationObject->$name = $value;
}
You should make sure that you add sufficient error-checking to this, depending on your needs.
The simple and "failsafe" solution
$target = new MyClass;
$target->id = $source->id;
$target->title = $source->title;
// and so on
Its a little bit more to code, but the benefits are
If some property of $source change, you will notice it (because its missing in $target)
You can name the properties of $target completely independent from $source
You see, what happens