Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to implode multiple values?

Tags:

php

How to implode multiple values?

I have following implode method:

$a = array("".$_POST['questionid']."","$qid");
$b = array("".$_POST['AnswerID']."","$ans");
$c = array("".$_POST['timetaken']."","$time");

$comma = implode(",",$a);
echo "$comma";

it gives:1,2 and print a,b value

    $comma = implode(",",$a);
    echo "$comma";
    $comma1 = implode(",",$b);
    echo "$comma1";

it gives:1,34,2 how do i print 1,2,3,4

like image 637
Naveen mutharasi Avatar asked Jan 29 '26 02:01

Naveen mutharasi


2 Answers

Why don't you add , by your self to variable $b

Change from

$comma1 = implode(",",$b);

into

$b[0] = ",".$b[0];
$comma1 = implode(",",$b);
echo $comma1;

2nd method: - Secondly you can marge arrays by using array_merge() then you can implode it via ,

$comma = implode(",", array_merge($a, $b));
echo $comma;
like image 199
Hassaan Avatar answered Jan 30 '26 14:01

Hassaan


You can use array_merge() to first merge all arrays and then implode them. Like this :

$comma = implode(",", array_merge($a, $b));
like image 40
Nijraj Gelani Avatar answered Jan 30 '26 15:01

Nijraj Gelani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!