I have a class with a property that is an array:
class NewObject {
public $Props = array();
}
$obj = new NewObject();
$obj->Props[0] = 'a';
$obj->Props[1] = 'b';
Now I want to change the values of Props, not directly, but with a variable 'propertyname':
This DOES work for single string properties but not for arrays, because the key N is interpreted as the Nth letter of the STRING 'Props' instead of the Nth value in the array!
$propertyname = 'Props';
$obj->$propertyname[0] ='c'; //doesnt work as expected, it tries to set $obj->P now, it seems
$obj->$propertyname[1] ='d';
Any way to solve this ?
$obj->{$propertyname}[0] ='c';
Related
I want get value of laravel model my this
$key = "name";
$this->$key; //returns the name
but, i want get multilevel values, like this
$key = "role->name";
$this->$key; //returns nothing
what i can do?
It looks like you're trying to access the property of a property. Don't put both property names into the same variable. You can use two variables instead:
$first_level_property = "role";
$second_level_property = "name";
$this->$first_level_property->$second_level_property
More examples of how to access property values.
How can I build a composed variable while creating a variable in PHP?
(Sorry I'm not sure how to call the different elements)
This is what I'm trying to do:
$language = 'name_'.$this->session->userdata('site_lang');
for ($i=1;$i<=3;$i++) {
$data = $arraydata->$language_.$i; // problem is here
}
I would like $language_.$i to be equivalent to name_english_1, next loop name_english_2... The same way I built $language
If you want to use an expression in a computed property, you have to put the expression in braces. Also, you need to put the underscore in quotes.
$data = $arraydata->{$language."_".$i};
However, I suggest you redesign your data structure. Instead of having separate name_LANG_i properties, make a single name property whose value is a multi-dimensional array.
$lang = $this->session->userdata('site_lang');
for ($i=1;$i<=3;$i++) {
$data = $arraydata->name[$lang][$i];
// do something with $data
}
Whenever you find yourself using variable variables or variable properties, it's almost always a sign that you should be using an array instead.
First construct the field name and then use it for accessing the field value from the object $arraydata. So your code should be like this:
$language = 'name_'.$this->session->userdata('site_lang');
for ($i = 1; $i <= 3; $i++) {
$var = "{$language}_{$i}";
$data = $arraydata->$var;
// echo $data;
}
If I have an stdObject say, $a.
Sure there's no problem to assign a new property, $a,
$a->new_property = $xyz;
But then I want to remove it, so unset is of no help here.
So,
$a->new_property = null;
is kind of it. But is there a more 'elegant' way?
unset($a->new_property);
This works for array elements, variables, and object attributes.
Example:
$a = new stdClass();
$a->new_property = 'foo';
var_export($a); // -> stdClass::__set_state(array('new_property' => 'foo'))
unset($a->new_property);
var_export($a); // -> stdClass::__set_state(array())
This also works specially if you are looping over an object.
unset($object[$key])
Update
Newer versions of PHP throw fatal error Fatal error: Cannot use object of type Object as array as mentioned by #CXJ . In that case you can use brackets instead
unset($object->{$key})
This also works if you are looping over an object.
unset($object->$key);
No need to use brackets.
This code is working fine for me in a loop
$remove = array(
"market_value",
"sector_id"
);
foreach($remove as $key){
unset($obj_name->$key);
}
Set an element to null just set the value of the element to null the element still exists
unset an element means remove the element
it works for array, stdClass objects user defined classes and also for any variable
<?php
$a = new stdClass();
$a->one = 1;
$a->two = 2;
var_export($a);
unset($a->one);
var_export($a);
class myClass
{
public $one = 1;
public $two = 2;
}
$instance = new myClass();
var_export($instance);
unset($instance->one);
var_export($instance);
$anyvariable = 'anyValue';
var_export($anyvariable);
unset($anyvariable);
var_export($anyvariable);
I want to unset first value of member array variable of a class but I'm not able to:
<?php
class A
{
public function fun()
{
$this->arr[0] = "hello";
}
public $arr;
}
$a = new A();
$a->fun();
$var ="arr";
unset($a->$var[0]); //does not unset "hello" value
print_r($a);
I could not find any solution after searching in Google. How can I remove the first value dynamically?
Try the following:
unset($a->{$var}[0]);
The problem with your code is, PHP tries to access the member variable $var[0] (which is null) and not $var.
You can try with array_shift:
array_shift($a->{$var});
This function uses reference to the value and removes (and returns) value from the beginning of your array.
<?php
class A
{
public function fun()
{
$this->arr[0] = "hello";
}
public $arr;
}
$a = new A();
$a->fun();
// no need to take $var here
// you can directly access $arr property wihth object of class
/*$var ="arr";*/
// check the difference here
unset($a->arr[0]); //unset "hello" value
print_r($a);
?>
TRY THIS
since $arr is a member of the class A and declared public, you can directly use
$a = new A();
$a->fun();
unset $a->arr[0];
But you will be surprised that for numeric indexed arrays, unset may bring problems.
suppose your array is like that;
$arr = ["zero","one","two","three","four"];
unset($arr[2]); // now you removed "two"
echo $arr[3]; // echoes three
Now array is ["zero","one", undefined ,"three","four"];
$arr[2] does not exist, it is undefined, and the rest is not reindexed...
for numeric indexed arrays using the method below is better:
$arr = ["zero","one","two","three","four"];
array_splice($arr,2,1); // now you removed "two" and reindexed the array
echo $arr[3]; // echoes four...
Now array is ["zero","one","three","four"];
If I have an stdObject say, $a.
Sure there's no problem to assign a new property, $a,
$a->new_property = $xyz;
But then I want to remove it, so unset is of no help here.
So,
$a->new_property = null;
is kind of it. But is there a more 'elegant' way?
unset($a->new_property);
This works for array elements, variables, and object attributes.
Example:
$a = new stdClass();
$a->new_property = 'foo';
var_export($a); // -> stdClass::__set_state(array('new_property' => 'foo'))
unset($a->new_property);
var_export($a); // -> stdClass::__set_state(array())
This also works specially if you are looping over an object.
unset($object[$key])
Update
Newer versions of PHP throw fatal error Fatal error: Cannot use object of type Object as array as mentioned by #CXJ . In that case you can use brackets instead
unset($object->{$key})
This also works if you are looping over an object.
unset($object->$key);
No need to use brackets.
This code is working fine for me in a loop
$remove = array(
"market_value",
"sector_id"
);
foreach($remove as $key){
unset($obj_name->$key);
}
Set an element to null just set the value of the element to null the element still exists
unset an element means remove the element
it works for array, stdClass objects user defined classes and also for any variable
<?php
$a = new stdClass();
$a->one = 1;
$a->two = 2;
var_export($a);
unset($a->one);
var_export($a);
class myClass
{
public $one = 1;
public $two = 2;
}
$instance = new myClass();
var_export($instance);
unset($instance->one);
var_export($instance);
$anyvariable = 'anyValue';
var_export($anyvariable);
unset($anyvariable);
var_export($anyvariable);