drgzmmy6379 2014-02-05 02:56
浏览 55
已采纳

匹配包含数组数组中特定字符的字符串

I want have an array that is created from a database to populate a table

array (size=2)
  number of friends => 3
  friends details => array (size=3) 
    0 => 
        array (size=4)
          'bithday' => string '2000-02-04' (length=10)
          'email' => string 'someone@example.com' (length=11)
          'town' => string 'OXFORD' (length=6)
          'status' => string 'FRIENDS' (length=5)

    2 => 
        array (size=4)
          'bithday' => string '2000-02-04'(length=10)
          'email' => string 'someone@example.com' (length=11)
          'town' => string 'OXFORD' (length=6)
          'status' => string 'NOT FRIENDS' (length=9)
    3 => 
        array (size=4)
          'bithday' => string '2000-02-04'(length=10)
          'email' => string 'someone@example.com' (length=11)
          'town' => string 'CAMBRIDGE' (length=8)
          'status' => string 'FRIENDS' (length=5)

I have (unsuccessfully) being trying to do a str_replace on the email string if the correct conditions are met, so I want to be able to replace the email address of all my friends in oxford with a button that allows me to email them with one press

so someone@example.com becomes

      <button action=invitePal>Invite your pal!</button>

But I only want this to happen if the array says that they meet both criteria to change it, so if I am having a party in Oxford, the invite button only appears on the entries for 'friends' in Oxford.

展开全部

  • 写回答

1条回答 默认 最新

  • doumu9799 2014-02-05 03:02
    关注

    A pretty standard method for this would be to loop though like so:

    foreach($people as $index => $person) {
    
        if($person['status'] === 'FRIENDS' && $person['town'] === 'OXFORD') {
    
            // Change the original string, echo it out, or do whatever.
            // If you need the email in this string, you can reference it via:
            // $person['email']
            $people[$index]['email'] = '<button action=invitePal>Invite your pal!</button>';
    
        }
    
    }
    

    An alternative method would be to use array_map and an anonymous function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部