0

I have a 2-d array as shown below :-

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

I want to implode the "$variables" array and get only the list of values present in "course" in a variable separated by comma. Is it possible using array_column() ? Something like this is not working :-

$string = implode("," , array_column($variables,'course');
echo $string; //gives no output
var_dump($string); //gives string '' (length=0)
Kevin
  • 41,329
  • 12
  • 52
  • 68
sachin tendulkar
  • 125
  • 1
  • 1
  • 7

3 Answers3

1

No just directly access them instead:

$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
Kevin
  • 41,329
  • 12
  • 52
  • 68
0
$string = implode(',', $variables['course']);
Dexa
  • 1,641
  • 10
  • 24
0

Just simply implode it by pointing it to the array in which you want to implode. Here is how your code should look like

$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 

Hope this helps you

Utkarsh Dixit
  • 4,153
  • 3
  • 14
  • 36