93

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?

Alix Axel
  • 151,645
  • 95
  • 393
  • 500
acme
  • 14,654
  • 7
  • 75
  • 109
  • have you looked at http://php.net/array_map ? – Kemo Jan 25 '10 at 10:07
  • see - http://stackoverflow.com/questions/12682232/converting-array-values-to-string/42998701#42998701 – Rohit Suthar Mar 24 '17 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. – acme Mar 29 '17 at 07:52
  • In case you want to debug, you might want to see https://stackoverflow.com/questions/139474/how-can-i-capture-the-result-of-var-dump-to-a-string – Kosmas Sep 06 '22 at 09:32

7 Answers7

223

A one-liner:

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

See PHP DOCS:

array_map

strval

Dharman
  • 30,962
  • 25
  • 85
  • 135
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
  • 1
    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. – Tasik Apr 11 '22 at 22:18
3

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

Jan Jaso
  • 31
  • 1
2

Are you looking for implode?

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

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

echo $comma_separated; // lastname,email,phone
YOU
  • 120,166
  • 34
  • 186
  • 219
  • 1
    No, because my array consists of objects, not strings. And the result should be an array and not an imploded string. – acme Apr 04 '12 at 08:08
2

Not tested, but something like this should do it?

foreach($a as $key => $value) {
    $new_arr[$key]=$value->__toString();
}
$a=$new_arr;
Ben Everard
  • 13,652
  • 14
  • 67
  • 96
  • read the question, it says "is there a one-liner or do I have to manually loop..." :) – Kemo Jan 25 '10 at 10:10
  • 1
    Yes, and as I suggested in the comment to Alix's post I would have offered his solution had I have known about it. – Ben Everard Jan 25 '10 at 10:26
-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.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
-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
    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](/help/how-to-answer). – Community Sep 15 '22 at 07:22
-6

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

$a = array(
    (string) $objA,
    (string) $objB,
);
Martin Bean
  • 38,379
  • 25
  • 128
  • 201
  • 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. – acme Jan 26 '10 at 10:03