2014-02-09 89 views
3

有沒有一種更簡單的方法來自內部數組的所有項目?從嵌套數組輸出項目

$user = array(
    'name' => 'Joe Blogs', 
    'age' => 25, 
    'validate' => 'true', 
    'children' => array(
      'Jack', 
      'Jill', 
      'Mark') 
); 

echo "The user's details are: " . $user['children'][0] .' '. $user['children'][1] .' and '. $user['children'][2]; 
+0

更容易? 「修復」他們如何? –

+0

爲了澄清,你想輸出一個字符串數組爲'item1,item2,item3,和item4'?也就是說,用逗號分隔空格,在最後一項之前加上「和」? – Brad

+0

那麼它的只有3個項目不是4,只需要一個更短的方式來顯示這些句子中的項目。 – user3035660

回答

1

利用implode()

<?php 
$user = array(
    'name' => 'Joe Blogs', 
    'age' => 25, 
    'validate' => 'true', 
    'children' => array(
     'Jack', 
     'Jill', 
     'Mark') 
); 

echo "The user details are ".implode(',',$user['children']); 

OUTPUT :

The user details are Jack,Jill,Mark