...

View Full Version : Replace value in array?



jacksont123
03-02-2007, 02:43 AM
I have the following array

$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
I want to be able to replace "Dog" to "<b>DOG</b>"

I tried str_replace but it didn't work.

str_replace("dog", "<b>DOG</b>", $a);



Also, is it possible to make any value in an array the first one?

I have this array:

$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");

I want to move horse or cat so it is the first value

$a=array("c"=>"Horse","a"=>"Dog","b"=>"Cat");
$a=array("b"=>"Cat","a"=>"Dog","c"=>"Horse");


Please help.

chump2877
03-02-2007, 05:53 AM
I want to be able to replace "Dog" to "<b>DOG</b>"


$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
$a['a'] = "<b>DOG</b>";


Also, is it possible to make any value in an array the first one?

With an associative array like this one, it shouldn;t matter which element comes first because each element has a unique, string index...if you want to reference a specific array element, simply reference it's key...

Inigoesdr
03-02-2007, 08:36 PM
If you want to make all of the items bold use foreach:
foreach($a as $k => $v)
$a[$k] = '<b>' . $v . '</b>';
Or for one specific value try array_search().

Also check out the sort functions for ordering: sort(), arsort(), asort(), ksort(), krsort(), natsort(), natcasesort(), rsort(), usort(), array_multisort(), and uksort().



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum