如何从php中的数组中获取值

问题描述 投票:-3回答:2

这是一个php数组,在某些对象中我只需要从这个标准的PHP数组中获取联系人值。只接触值而不是名称值,因此如何从该对象获取所有联系人的值。我只想要联系人密码的联系人价值。

stdClass Object
(
    [contactlist] => Array
        (
            [0] => {
    contact = 4155553695;
    name = "Kate Bell";
}
            [1] => {
    contact = 4085553514;
    name = "Daniel Higgins";
}
            [2] => {
    contact = 8885551212;
    name = "John Appleseed";
}
            [3] => {
    contact = 5555228243;
    name = "Anna Haro";
}
            [4] => {
    contact = 7075551854;
    name = "Hank Zakroff";
}
            [5] => {
    contact = 5556106679;
    name = "David Taylor";
}
            [6] => {
    contact = 542222222222;
    name = Deepak;
}
        )

)
php arrays regex preg-match
2个回答
2
投票

您可以尝试使用此UPDATED解决方案

$result = array();
foreach ($object->contactlist as $k=>$v){
   preg_match('/(\d+)/s', $v, $contact);
   $result[] = $contact[0];
}
var_dump($result);

1
投票

您需要使用正则表达式来提取值。请尝试以下方法:

foreach ($obj->contactlist as $contactInfo) {
    preg_match('~^\s*contact\s*=\s*(\d+);$~m', $contactInfo, $matches);
    $contact = $matches[1];
    echo $contact . '<br />';
}

它将匹配格式为“contact = 123;”的字符串。并提取匹配的数字。

在这里阅读更多:http://php.net/preg_match

preg_match - 执行正则表达式匹配

用法:int preg_match(string $ pattern,string $ subject [,array&$ matches [,int $ flags = 0 [,int $ offset = 0]]])

© www.soinside.com 2019 - 2024. All rights reserved.