I have an array as follows
$posts = array(
0 => array('user' => 'Charlie', 'message' => 'First message'),
1 => array('user' => 'Charlie', 'message' => 'Second message'),
2 => array('user' => 'Charlie', 'message' => 'Third message TEXT!'),
3 => array('user' => 'Charlie', 'message' => 'Fourth message')
);
and I'd like to replace "TEXT" with "NEXT" if it's inside the message. How could i do this?
I tried with
foreach ($posts as $r)
{
$r['message'] = str_replace('TEXT', 'NEXT', $r['message']);
}
But seems not to be working.
Joel Etherton
37.5k10 gold badges92 silver badges106 bronze badges
asked Feb 15, 2011 at 13:50
sh03
76.5k39 gold badges177 silver badges283 bronze badges
5 Answers 5
That's because foreach by default uses a copy of the array elements, rather than the elements themselves. You can use & to change this to a reference:
foreach ($posts as &$r) {
answered Feb 15, 2011 at 13:53
lonesomeday
239k54 gold badges330 silver badges329 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
foreach ($posts as &$r)
{
$r['message'] = str_replace('TEXT', 'NEXT', $r['message']);
}
answered Feb 15, 2011 at 13:52
soju
25.4k3 gold badges70 silver badges71 bronze badges
1 Comment
Felix Kling
@Charlie Pigarelli:
& means you have a reference to the element. Otherwise you only loop over a copy of the array. See the documentation: php.net/manual/en/control-structures.foreach.php . This works, if it does not work for you, you have another error.foreach ($posts as &$r)
{
$r['message'] = str_replace('TEXT', 'NEXT', $r['message']);
}
The & will cause $r to be a reference to the value which allows you to change it inside the original array. In your code, you're modifying a copy.
answered Feb 15, 2011 at 13:53
GolezTrol
116k19 gold badges186 silver badges215 bronze badges
Comments
foreach ($posts as $key => $r)
{
$posts[$key]['message'] = str_replace('TEXT', 'NEXT', $r['message']);
}
answered Feb 15, 2011 at 13:58
Luca Filosofi
31.2k9 gold badges72 silver badges77 bronze badges
Comments
@Charlie Pigarelli: Try --
for ($i = 0; $i < count($posts); $i++)
{
$posts[$i]['message'] = str_replace('TEXT', 'NEXT', $posts[$i]['message']);
}
answered Feb 15, 2011 at 13:55
stealthyninja
10.4k11 gold badges57 silver badges61 bronze badges
5 Comments
stealthyninja
Wow...down-vote for supplying another working solution with no explanation. Thanks, random coward.
Cody Gray
I think people are downvoting/flagging your post because of your use of profanity. Whether or not you care is up to you. (I was not one of those people.)
stealthyninja
@Cody Gray: I doubt it, otherwise everyone else would be down-voted as well (including the OP's post).
binaryLV
stealthyninja, is it really working in all cases? And what if
$posts in reality have different keys, e.g., 0, 1, 2, 7 (rather than 0, 1, 2, 3)? And why would count($posts) have to be called 100 times if $posts would consist of 100 elements? While your solution would work in many cases, it's far from being convenient way of looping through an array. (and no, I'm not the one who down-voted)stealthyninja
@binaryLV: If you had been, I would've accepted that explanation. I just get annoyed when people seem to down-vote for petty reasons (the swearing thing for one -- it's what the flag link is for, I thought). Anyway, thanks for a great comment. :-)
lang-php
&. Just don't forget tounset($r)right after the loop, or you might accidentally change the value of array's last accessed element.