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

时间:2014-02-05 10:56:25

标签: php arrays str-replace

我想要一个从数据库创建的数组来填充表

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)

如果符合正确的条件,我(尝试在电子邮件字符串上)尝试执行str_replace是不成功的,所以我希望能够通过一个允许我发送电子邮件的按钮替换牛津大学所有朋友的电子邮件地址一次按下

所以someone@example.com成为

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

但我只是希望如果阵列说它们符合两个标准来改变它,就会发生这种情况,所以如果我在牛津举办派对,邀请按钮只出现在牛津的“朋友”条目中。

1 个答案:

答案 0 :(得分:1)

一个非常标准的方法是像这样循环:

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>';

    }

}

另一种方法是使用array_mapanonymous function

相关问题