2

I am new to PHP.

I want to convert this array [["14785"],["125478"]] to string like this 14785,125478.

What is the way to do this?

2
  • 9
    Possible duplicate of Array to String PHP? Commented Dec 7, 2015 at 11:11
  • Show your real array structure.. Commented Dec 7, 2015 at 11:12

3 Answers 3

6

try this,

Using implode() - Demo

$array = ["14785"],["125478"]
$str = implode(',',$array);
echo $str;

Using join() - Demo

$array =  ["14785","125478"];
$str = join(",",$array);
echo $str;

EDIT

For multi-dimention array, - Demo

$arr = [["14785"],["125478"]];
$str = implode(',', array_map(function($el){ return $el[0]; }, $arr));

echo $str;
Sign up to request clarification or add additional context in comments.

7 Comments

When I use implode function I get an error like Array to string conversion. my code is an array inside array.I have edited it properly now.
can you format the array which you have added in proper way??
This is my code [["14785"],["125478"]] I want like this 14785 so that I can compare it with another value.
Glad I could help you:)
if its single dimention, use first method, if its multi dimention, use array_map() method.
|
5

use php's implode function

$arr =  ["14785","125478"];
echo implode(",",$arr);

6 Comments

Without knowing the array structure it may not work, but for normal arrays this is the way..
join is faster than implode: dev.airve.com/demo/speed_tests/php/join_vs_implode.php echo join(",",$arr);
@MyWay - join() is simply an alias for implode() so why do you believe that it's faster based on that arbitrary test page that you've linked..... the executed code is the same
Mentioned what I came across
The link you came across is a pretty arbitrary tester that isn't consistent from one run to the next, so it can't be taken as absolute gospel truth.... it's meaningless
|
0

Using implode()

 <?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.