Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP Activerecord对象数组,下一步是什么?_Php_Arrays_Object_Activerecord_Phpactiverecord - Fatal编程技术网

PHP Activerecord对象数组,下一步是什么?

PHP Activerecord对象数组,下一步是什么?,php,arrays,object,activerecord,phpactiverecord,Php,Arrays,Object,Activerecord,Phpactiverecord,假设数据库中三个表之间存在以下关联: //working with three tables a client 'has one' business //and a business has many business hours. 以下内容将为我们提供一个Activerecord对象数组: $this->client->business->businesshours 我们必须从数组中提取一个对象以获取其列值: $this->client->bu

假设数据库中三个表之间存在以下关联:

    //working with three tables a client 'has one' business
    //and a business has many business hours.
以下内容将为我们提供一个Activerecord对象数组:

$this->client->business->businesshours
我们必须从数组中提取一个对象以获取其列值:

$this->client->business->businesshours[0]->start_time

由于我是新手,除了使用foreach()循环外,还有哪些方法可以从对象数组中提取/排序/使用信息?是否有方法对对象的数组进行排序,根据列值提取信息,是否有任何最佳做法?

没有特定于库的方法对某些
businesshour
对象进行排序或从结果数组中提取。如果要操作返回的对象数组,需要在结果数组上使用标准PHP数组函数,如
array\u map

如果知道结果数组中返回对象的排序顺序或条件,则应在关联声明中指定这些条件,以便不返回不需要的对象

由于您没有发布任何代码,因此您只需根据本例推断您自己的情况:

static $has_many = array(
  array(
    'businesshours',
    'conditions' => array('hour BETWEEN ? AND ?' => array(9, 17)),
    'order' => 'hour ASC'
  )
);
此关联声明将仅返回介于9和17之间的
businesshour
对象,并按升序执行。因此,如您所见,如果您仅将关联约束到所需的记录,那么一旦收到结果数组,就不需要对其进行排序或解析

有时,使用
array\u map
仅从结果数组中获取某些对象非常有用:

// get $result array
$new = array_map(function($obj) { if ($obj->hour > 9){ return $obj; } }, $result);