如何破坏二维数组

时间:2014-09-15 12:04:23

标签: php arrays

我有一个二维数组,如下所示: -

$variables = array(
           "firstname" => "Sachin",
           "lastname" => "Tendulkar",
           "course" => array(
                         0 => "PHP",
                         1 => "HTML",
                         2 => "CSS",
                         3 => "Javascript"
                 )
          );

我想破坏" $变量"数组,只获取"课程"中存在的值列表在用逗号分隔的变量中。是否可以使用array_column()?这样的事情不起作用: -

$string = implode("," , array_column($variables,'course');
echo $string; //gives no output
var_dump($string); //gives string '' (length=0)

3 个答案:

答案 0 :(得分:1)

不只是直接访问它们:

$variables = array(
    "firstname" => "Sachin",
    "lastname" => "Tendulkar",
    "course" => array(
         0 => "PHP",
         1 => "HTML",
         2 => "CSS",
         3 => "Javascript"
     )
);

$courses = implode(', ', $variables['course']); // point it directly on the desired array
echo $courses; // PHP, HTML, CSS, Javascript

答案 1 :(得分:0)

$string = implode(',', $variables['course']);

答案 2 :(得分:0)

只需将其指向您想要内爆的数组即可完全内爆。以下是您的代码应如何显示

$variables = array(
           "firstname" => "Sachin",
           "lastname" => "Tendulkar",
           "course" => array(
                         0 => "PHP",
                         1 => "HTML",
                         2 => "CSS",
                         3 => "Javascript"
                 )
          );


$string = implode(',', $variables['course']);

echo $string; //gives output
var_dump($string); //gives string 

希望这有助于你