98

If I have a array with objects:

$a = array($objA, $objB);

(each object has a __toString()-method)

How can I cast all array elements to string so that array $a contains no more objects but their string representation? Is there a one-liner or do I have to manually loop through the array?

4
  • have you looked at php.net/array_map ? Commented Jan 25, 2010 at 10:07
  • see - stackoverflow.com/questions/12682232/… Commented Mar 24, 2017 at 11:47
  • @RohitSuthar : your linked answer creates an array out of a string. This question was about converting an array of objects to an array of their string representation. Commented Mar 29, 2017 at 7:52
  • In case you want to debug, you might want to see stackoverflow.com/questions/139474/… Commented Sep 6, 2022 at 9:32

7 Answers 7

237

A one-liner:

$a = array_map('strval', $a);
// strval is a callback function

See PHP DOCS:

array_map

strval

Sign up to request clarification or add additional context in comments.

2 Comments

Can also be called like return array_map(fn ($item) => strval($item), $items); if you'd rather not reference the function 'strval' as a string.
in my case (laravel eloquent query results), I have got an array of stdClass objects with objects with key=> value like {'id' => 123}, I use : implode(', ', array_map(function($id_object){return $id_object->id;}, $my_array)); similar at @Jan Jaso solution
4

Alix Axel has the nicest answer. You can also apply anything to the array though with array_map like...

//All your objects to string.
$a = array_map(function($o){return (string)$o;}, $a);
//All your objects to string with exclamation marks!!!
$a = array_map(function($o){return (string)$o."!!!";}, $a);

Enjoy

Comments

3

Not tested, but something like this should do it?

foreach($a as $key => $value) {
    $new_arr[$key]=$value->__toString();
}
$a=$new_arr;

2 Comments

read the question, it says "is there a one-liner or do I have to manually loop..." :)
Yes, and as I suggested in the comment to Alix's post I would have offered his solution had I have known about it.
2

Are you looking for implode?

$array = array('lastname', 'email', 'phone');

$comma_separated = implode(",", $array);

echo $comma_separated; // lastname,email,phone

1 Comment

No, because my array consists of objects, not strings. And the result should be an array and not an imploded string.
-2

I can't test it right now, but can you check what happens when you implode() such an array? The _toString should be invoked.

6 Comments

It does. Simple implode($array) will do.
@Gordon: It'll merge all the strings in one though, I think the OP wants to keep the __toString() generated strings in the corresponding array elements.
Right, I want the array to be still intact and only the elements in it casted to string.
@Alix Oh, I see. Yes. Then implode won't do.
@nikc: Not if the generated __toString() contains ,.
|
-2
$str1 = "pankaj";
$str2 = array("sam",'pankaj',"hello");
function Search($x ,$y){
    $search = $x;
    $arr = $y;
 foreach($y as $key => $value){
    $str = array_search($x, $y);
    if($str == $key){
    echo $key ."=>".$value;
    echo "<br>".gettype($value);
  }
 }
}
Search($str1 ,$str2);

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-6

Is there any reason why you can't do the following?

$a = array(
    (string) $objA,
    (string) $objB,
);

1 Comment

Yes, because actually I don't know how many elements there are in the array. The example above was just reduced to two elements to make it more clear.

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.