drgzmmy6379 2014-02-05 10: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 11: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.

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

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助