I have the following array in php
$criteria = array();
$criteria['x'] = array();
$criteria['y'] = array();
$criteria['z'] = array();
I also have a class defined in php with property xyz
$screenerItem = new \StdClass();
$screenerItem->xyz =0;
How can i make my array $criteria as a part of my screenerItem class property?
I tried this but didnt work,can someone help me out to know what is wrong with this syntax:
$screenerItem->$criteria = array();
$screenerItem->$criteria['x'] = array();
$screenerItem->$criteria['y'] = array();
$screenerItem->$criteria['z'] = array();
As @Nahid suggested you can fill your class poperty in constructor. Another way (if your class property is public) is:
$criteria_array = array(/* your array here */);
$screenerItem->criteria = $criteria_array;
// Notice that I don't use $ in front of criteria as it's class property.
And of course your attemp is legal too:
// Once again no $ before criteria
$screenerItem->criteria = array();
$screenerItem->criteria['x'] = array();
Try this
class MyClass{
public $criteria=array();
function __construct(){
$this->criteria=array(
'x'=>array(),
'y'=>array(),
'z'=>array()
);
}
}
$screenItem=new MyClass;
or You may use this convenction
class MyClass{
public $criteria=array();
function __construct($array){
$this->criteria=$array;
}
}
$data=array(
'x'=>array(),
'y'=>array(),
'z'=>array()
);
$screenItem=new MyClass($data);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.