Re: Replacing an element in an array



laredotornado@xxxxxxxxxxx wrote:
Hi,

I'm using PHP 5. If I have a non-asociative array, that is an array
where the key is the numerical index of the element ...

// For example
$arr = array("Denver", "Chicago", "Seattle", "Austin");

What is the easiest way to replace all instances of an element in the
array with a new element?

Thanks, - Dave

Maybe something like this?

<?php

$arr = array("Denver", "Chicago", "Seattle", "Austin");

$keys = array_keys($arr, "Denver");

if ($keys !== false)
foreach($keys as $key)
$arr[$key] = "San Francisco";

print_r($arr);

?>

Replaces each instance of "Denver" with "San Francisco".

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.



Relevant Pages

  • Re: Need help on PHP for MPE/ix
    ... Here is the syntax of the dbupdate intrinsic in PHP: ... assoc array of item values) ... Please let me know if you have any more suggesstions to solve this problem. ... Pavan Kumar Rati wrote: ...
    (comp.sys.hp.mpe)
  • Re: Using a RegEx as a "variable" WITHIN an array?
    ... believe the PHP one is also based upon. ... combination of 1 or more numerics in this instance would give you your ... add this to your array - you could then do a pattern match on the array ... > Dim selCriteria as String ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Need help on PHP for MPE/ix
    ... If one item is an array, it must be declared as such in the list ... So, please show us the declaration of the record buffer, and its ... Of Pavan Kumar Rati ... Need help on PHP for MPE/ix ...
    (comp.sys.hp.mpe)
  • Re: attempting to return values from array from w/in a function
    ... What I get is an array w/ only the most recent array entry. ... I am new to PHP & do not have formal scripting ... you end up comparing strings, but the test you mean probably is simply: ... case 1: {codeblock} ...
    (comp.lang.php)
  • Re: need to send email to 1000 addresses (currently in MS Excel)- whats the best way to do that, cre
    ... > coding each line as an array then looping through the array with mail. ... >> don't post a question that doesn't have anything to remotely do with PHP, ... one huge string) and then it splits that string into an array of arrays. ... CC it, or even better, BCC it so that those being emailed can't ...
    (comp.lang.php)