0

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
7
  • 1
    As many have suggested, use &. Just don't forget to unset($r) right after the loop, or you might accidentally change the value of array's last accessed element. Commented Feb 15, 2011 at 13:58
  • 2
    I'm all for brightening production code with foul language, in fact I consider it an essential part of the development process - however, I think SO should be kept clean (I started messing with programming at 6) so I flagged your post. Sorry!! :) Commented Feb 15, 2011 at 13:59
  • You've changed the question so half the answers don't make sense anymore. Commented Feb 15, 2011 at 14:16
  • 1
    Please avoid the use of profanity. Commented Feb 15, 2011 at 14:18
  • 1
    @Mike B, It's not true. I didn't changed anything. Look at the change log. Commented Feb 15, 2011 at 14:27

5 Answers 5

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
Sign up to request clarification or add additional context in comments.

Comments

3
foreach ($posts as &$r)
{
 $r['message'] = str_replace('TEXT', 'NEXT', $r['message']);
}
answered Feb 15, 2011 at 13:52

1 Comment

@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.
2
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

Comments

2
 foreach ($posts as $key => $r)
{
 $posts[$key]['message'] = str_replace('TEXT', 'NEXT', $r['message']);
}
answered Feb 15, 2011 at 13:58

Comments

1

@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

5 Comments

Wow...down-vote for supplying another working solution with no explanation. Thanks, random coward.
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.)
@Cody Gray: I doubt it, otherwise everyone else would be down-voted as well (including the OP's post).
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)
@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. :-)

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.