How to cast array elements to strings in PHP?
Asked Answered
R

7

94

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?

Reba answered 25/1, 2010 at 10:4 Comment(4)
have you looked at php.net/array_map ?Algetic
see - #12682732Cupid
@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.Reba
In case you want to debug, you might want to see #139974Fluffy
P
228

A one-liner:

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

See PHP DOCS:

array_map

strval

Pernambuco answered 25/1, 2010 at 10:8 Comment(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.Panne
M
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

Mescal answered 2/9, 2016 at 6:40 Comment(0)
W
2

Are you looking for implode?

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

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

echo $comma_separated; // lastname,email,phone
Whitby answered 25/1, 2010 at 10:6 Comment(1)
No, because my array consists of objects, not strings. And the result should be an array and not an imploded string.Reba
B
2

Not tested, but something like this should do it?

foreach($a as $key => $value) {
    $new_arr[$key]=$value->__toString();
}
$a=$new_arr;
Brownson answered 25/1, 2010 at 10:9 Comment(2)
read the question, it says "is there a one-liner or do I have to manually loop..." :)Algetic
Yes, and as I suggested in the comment to Alix's post I would have offered his solution had I have known about it.Brownson
F
-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.

Fronia answered 25/1, 2010 at 10:7 Comment(6)
It does. Simple implode($array) will do.Backwater
@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.Pernambuco
Right, I want the array to be still intact and only the elements in it casted to string.Reba
@Alix Oh, I see. Yes. Then implode won't do.Backwater
explode(',', implode(',' $array)) then, perhaps? Although, the proposed array_map would be more elegant, this approach could be done like this, couldn't it?Wandering
@nikc: Not if the generated __toString() contains ,.Pernambuco
S
-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);
Stephenstephenie answered 11/9, 2022 at 8:23 Comment(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.Rebarbative
C
-6

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

$a = array(
    (string) $objA,
    (string) $objB,
);
Carola answered 25/1, 2010 at 10:54 Comment(1)
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.Reba

© 2022 - 2024 — McMap. All rights reserved.