如何从查找结果中获取列?

时间:2014-07-31 03:45:06

标签: php

项目和图像表之间存在多对多关系。现在我想获取图像的'file_name'并在页面上显示图像。如何做?

$items = Model_Item::find($id, array('related' => array('images')));
$result = $items->to_array();

上述语句的输出是这样一个数组:

array(18) {
  ["id"]=>
  string(2) "27"
  ["product_code"]=>
  string(3) "666"
  ["item_color_id"]=>
  string(1) "7"
  ["item_size_type_id"]=>
  string(1) "1"
  ["classification_id"]=>
  string(1) "1"
  ["name"]=>
  string(5) "66666"
  ["display_name"]=>
  string(8) "goods_18"
  ["usual_price"]=>
  string(5) "88888"
  ["selling_price"]=>
  string(3) "888"
  ["point_ratio"]=>
  string(2) "88"
  ["description"]=>
  string(3) "888"
  ["publish_start_datetime"]=>
  string(19) "0000-00-00 00:00:00"
  ["publish_end_datetime"]=>
  string(19) "0000-00-00 00:00:00"
  ["publish_flag"]=>
  string(1) "8"
  ["created_at"]=>
  string(19) "0000-00-00 00:00:00"
  ["updated_at"]=>
  string(19) "0000-00-00 00:00:00"
  ["deleted_at"]=>
  NULL
  ["images"]=>
  array(1) {
    [6]=>
    array(6) {
      ["id"]=>
      string(1) "6"
      ["file_name"]=>
      string(36) "ea4c6011154b543e710b8809ed39b9db.jpg"
      ["content_type"]=>
      string(10) "image/jpeg"
      ["created_at"]=>
      string(19) "0000-00-00 00:00:00"
      ["updated_at"]=>
      string(19) "0000-00-00 00:00:00"
      ["deleted_at"]=>
      NULL
    }
  }
}

3 个答案:

答案 0 :(得分:0)

您可以使用array_column返回输入数组中单个列的值

答案 1 :(得分:0)

尝试

echo $result['images']['image_name'];

答案 2 :(得分:0)

你可以echo $result['images']['file_name'],而不是楼上的echo $result['images']['image_name']