Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2006
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Replace value in array?

    I have the following array
    PHP Code:
    $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.
    PHP Code:
    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:
    PHP Code:
    $a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse"); 
    I want to move horse or cat so it is the first value
    PHP Code:
    $a=array("c"=>"Horse","a"=>"Dog","b"=>"Cat");
    $a=array("b"=>"Cat","a"=>"Dog","c"=>"Horse"); 

    Please help.

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,859
    Thanks
    22
    Thanked 157 Times in 148 Posts
    I want to be able to replace "Dog" to "<b>DOG</b>"
    PHP Code:
    $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...
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    If you want to make all of the items bold use foreach:
    Code:
    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().


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •