Nere
Nere

Reputation: 4097

How to remove square bracket from an array?

I have an array..let say:

$array = [$a,$b,$c,$d];

How I can remove [ and ]?

The expected result would be:

$a,$b,$c,$d

I used some array function e.g array_slice but it does not fill my requirement. Any ideas?

Note: I need to pass all array elements to function as argument.

e.g: function example($a,$b,$c)

Upvotes: 1

Views: 5424

Answers (1)

iam-decoder
iam-decoder

Reputation: 2564

it sounds like you're after a string representation of the array, try using join() or implode() like this:

<?php
$array = [$a,$b,$c,$d];
$str = join(",", $array); // OR $str = implode(",", $array);
echo $str;

EDIT

after reading your question a little more carefully, you're trying to pass the array into a function call, to do that you need to use call_user_func_array():

<?php
function function_name($p1, $p2, $p3, $p4){
    //do something here
}
$array = [$a,$b,$c,$d];
call_user_func_array('function_name', $array);

Upvotes: 3

Related Questions