搜索Php数组以获取字符串并提取相邻文本

时间:2014-04-10 16:58:17

标签: php mysql sql

我需要从mysql数据库生成一个包含姓名,电子邮件和电话号码的表。我正在使用php来创建表格,除了如何获得一个人的电话号码(如果有的话),我已经弄明白了。这很棘手,因为与名称和电子邮件不同,如果存在信息,则没有特殊字段(不是每个人都给出了电话号码)。如果电话号码存在,则它包含在“params”字段中,其中包含一堆其他数据。我的代码使用以下语句生成表:

while($row = mysql_fetch_array($result))

为了显示我需要筛选的数据,我执行了一个“print_r命令,结果是:

  

Array ( [0] => Manager [usertype] => Manager [1] => 167 [id] => 167 [2] => Sam Spade [Name] => Sam Spade [3] => sm@aol.com [Email] => sm@aol.com [4] => 167 [user_id] => 167 [5] => {"birth_year":"1911","ride_note":"on","membership_visible":"on","volunteer":["","House Events","House Socials"],"ride_catagory":["10-12","13-15"],"phone":"540-123-4567"} [params] => {"birth_year":"1911","ride_note":"on","membership_visible":"on","volunteer":["","House Events","House Socials"],"ride_catagory":["10-12","13-15"],"phone":"540-123-4567"} )

如何提取电话号码540-123-4567? (如果没有给出电话号码,那么“电话”这个词也不会出现。)

谢谢

我认为我们已经接近了。让我公开一下我到目前为止的更多代码:

$sql = 'SELECT  c.usertype, c.id, c.`name` as Name, c.`email` as Email, j.user_id, j.`params`  
                FROM j25_akeebasubs_users j, j25_users c WHERE j.params LIKE "%membership_visible%"
                and c.id = j.user_id and usertype != "Registered" order by c.name;';

        $result = mysql_query($sql) or die ('Error: '.mysql_error ());


        while($row = mysql_fetch_array($result))
        {

            $arr = "{$arr}\n
                    <table border=1 cellspacing=1 cellpadding=2>
                    <col width=200px />
                    <col width=300px />
                    <col width=150px />
                    <tr>
                        <td>{$row['Name']}</td>
                        <td>{$row['Email']}</td>
                        <td>{$row[5]}</td>
                    </tr>
                    </table>
                    \n</BODY>\n</HTML>\n";
        }

            return ($arr);
        } 

姓名和电子邮件工作正常。对于最后一个元素$ row [5],我自然会得到所有存在的数据,我只需要电话号码。

3 个答案:

答案 0 :(得分:1)

假设Marc B是正确的并且它在你的params字段中是有效的JSON,你应该能够做到这样的事情:

while( $row = mysql_fetch_array($result)) {

   $user_params = json_decode($row['params'], true);


        $arr = "{$arr}\n
                <table border=1 cellspacing=1 cellpadding=2>
                <col width=200px />
                <col width=300px />
                <col width=150px />
                <tr>
                    <td>{$row['Name']}</td>
                    <td>{$row['Email']}</td>
                    <td>{$user_params['phone']}</td>
                </tr>
                </table>
                \n</BODY>\n</HTML>\n";

}

return $arr;

请参阅PHP文档了解json_decode

答案 1 :(得分:0)

它是一个json字符串,使用json_decode将其解码为数组,然后您可以检查手机变量isset是否可以像普通数组一样访问它。

答案 2 :(得分:0)

编辑:根据您发布的其他代码,该行是从mysql返回的数组,但params字段是json。以下代码已更新:

好像你有一个数组,你可以从中查找东西。我不确定之前的代码是什么。如果它&#39;已经是一个很好的数组,如果没有使用json_decode()将字符串转换为数组。然后:

function getPhone($row) {
    $params = json_decode($row[5], true);
    return $params["phone"];
}

我可能会指出 mysql已被弃用,您应该使用mysqli(或PDO)。此外,如果您的查询发生更改,硬编码数组索引[5]将使您遇到麻烦,因此,良好的做法是返回一个关联数组并命名字段而不是对它们进行编号。请参阅http://us3.php.net/mysqli_fetch_assoc