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,694
  • 12
  • 53
  • 70
sachin tendulkar
  • 123
  • 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,694
  • 12
  • 53
  • 70
0
$string = implode(',', $variables['course']);
Dexa
  • 1,641
  • 10
  • 25
  • It throws this :- Warning: implode(): Invalid arguments passed in C:\wamp\www\PhpSample\index.php. But works, I wanted it without this warning. – sachin tendulkar Sep 15 '14 at 12:12
  • Make sure this is the only implode and you don't have leftovers from your previous example. For provided array this is correct solution. – Dexa Sep 15 '14 at 12:18
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,267
  • 3
  • 15
  • 38
  • It throws this :- Warning: implode(): Invalid arguments passed in C:\wamp\www\PhpSample\index.php. But works, I wanted it without this warning. – sachin tendulkar Sep 15 '14 at 12:15
  • First of all tell me that are you using the same array because this code is working fine here http://www.compileonline.com/execute_php_online.php. Please test on this site, there might be a problem with your php version – Utkarsh Dixit Sep 15 '14 at 12:18