4

i have an array like in PHP:

$a = array('110','111','121');

i want to convert it to :

$b = " '110' , '111' , '121' ";

is there any function in PHP that does it? i know it could be done with a loop on array and put value in $b, but i want a less more code solution.

thank you.

asked May 15, 2011 at 9:36
1

5 Answers 5

8

You do need all those spaces and quotes? You can still use implode, although array_reduce might be nicer

$a = array(1, 2, 3, 4);
$x = "'".implode("' , '", $a)."'";

array_reduce:

$x = array_reduce($a, function($b, $c){return ($b===null?'':$b.' , ')."'".$c."'";});

Advantage of array_reduce, is that you will get NULL for an empty array, instead of ''. Note that you cannot use this inline function construct in php versions prior to 5.3. You'll need to make the callback a separate function and pass its name as a string to array_reduce.

answered May 15, 2011 at 9:41
3
  • Getting NULL over getting an empty string isnt exactly an advantage. You have to check the return type instead of just being able to work with it. Having functions return a single type is always the better choice. Commented May 15, 2011 at 9:54
  • You're not getting an empty string, you're getting a string containing two quotes. :) So'll you'd probably still have to check for that. If so, checking for null is more logical and convenient than checking for some obscure content like two single quotes. Of course, you could always choose to implode first and add the quotes afterwards. Or you could typecast the result of array_reduce to string which will convert your NULL to an empty string. Commented May 15, 2011 at 9:59
  • you are getting the quoted string because your implementation is flawed. functions and methods should not return null. let them return a null object - or in this case, an empty string. when you return null, you are effectively returning a status message the developer has to check on. Commented May 15, 2011 at 10:14
4
answered May 15, 2011 at 9:37
3

well this is a different approach

$arraystring = print_r($your_array, true); 

and if you want to print it somewhere else formated then

$arraystring = '<pre>'.print_r($your_array, true).'</pre>';

or you could mix many arrays and vars if you do this

ob_start();
print_r($var1);
print_r($arr1);
echo "blah blah";
print_r($var2);
print_r($var1);
$your_string_var = ob_get_clean();
answered Jun 13, 2012 at 13:22
2

Use the implode function.

$b = " '" . implode("' , '", $a) . "' ";
answered May 15, 2011 at 9:38
0

Very nice outup gives

$arraystring = json_encode($your_array);
answered Jul 20, 2013 at 21:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.