Take out variable value from string

Discussion in 'PHP' started by Omzy, Jan 7, 2009.

  1. #1
    Lets say I have an array with some values, eg:

    $category="Wedding";
    
    $cats=array(
    'wedding-cakes'=>array('main', 'Wedding Cakes'),
    'wedding-venues'=>array('main', 'Wedding Venues'),
    'other-cakes=>array('sub', 'Other Cakes'),
    'other-venues'=>array('sub', 'Other Venues'),
    );
    Code (markup):
    Now when I output this array, if it finds the value of $category in $cats[1], it does not print out that bit, eg in the above the two 'Wedding' entries should only output 'Cakes' and 'Venues' respectively as $cats[1] contains the string "Wedding".
     
    Omzy, Jan 7, 2009 IP
  2. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    str_replace() lets you provide an array as the replace subject too and will return the new array.

    $new_cats = str_replace($category, "", $cats);
     
    zerxer, Jan 7, 2009 IP