0

It is a many to many relations between items and images tables.Now I want to get the 'file_name' of images and to display the image on the page.How to do it?

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

the output of above statement is such an 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
    }
  }
}
samurai
  • 15
  • 2
  • Take a look here: http://stackoverflow.com/questions/1494953/is-there-a-function-to-extract-a-column-from-an-array-in-php – adanot Jul 31 '14 at 03:50

3 Answers3

0

You can use array_column which Return the values from a single column in the input array

Avinash Babu
  • 6,171
  • 3
  • 21
  • 26
0

try

echo $result['images']['image_name'];
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
hellojebus
  • 3,267
  • 1
  • 21
  • 23
0

You can just echo $result['images']['file_name'], not echo $result['images']['image_name'] upstairs

iatboy
  • 1,295
  • 1
  • 12
  • 19