0

如何获取给定正值或以下 1 的数组的索引是我的数组。

Array( [0] => stdClass Object
    ( [result] => )

[1] => stdClass Object
    ( [result] => 1 )

[2] => stdClass Object
    ( [result] => )

[3] => stdClass Object
    ( [result] => )

[4] => stdClass Object
    ( [result] => )

[5] => stdClass Object
    ( [result] => )

[6] => stdClass Object
    ( [result] => 1 )
 )

上面的数组结果来自这个函数,它的作用是验证提交给 PQ 的电子邮件地址,如果电子邮件有效并已注册,它将返回 1 或 true,现在我需要隔离这个有效且已注册的电子邮件,因为我需要向其余电子邮件发送邀请。

  function get_email_verification(){
         $api_request_url = "http://api.payquicker.com/api/IsActiveAccount";
        foreach ($this->get_payquicker_emails() as $email) {
                $param = "email=".$email;
                $result_request[] =  $this->do_request($api_request_url, $param, 'GET', 'json');

        }
        if (empty($result_request)) {
            echo "All payee emails are already registered to Payquicker";
        }
       return $result_request; 
      }
4

2 回答 2

0

您的示例数组显示了结果值 > 1 的多个项目,所以我假设您要提取所有这些项目的索引?

您可以遍历数组,检查每个项目的结果是否 > 0,如果确实将其索引添加到新数组。

$results = array();

foreach($your_array as $key=>$item) {
   if($item['result'] > 0){
      $results[] = $key;
   }
}

您的示例数组的结果将是一个包含值的数组[1,6]

于 2013-09-13T03:13:19.113 回答
0

尝试这个:

在这里演示:http ://codepad.org/Hk5ufuG8

$data = Array();
$data[0] = (object)Array("result" => '');
$data[1] = (object)Array("result" => 1);
$data[2] = (object)Array("result" => '');
$data[3] = (object)Array("result" => '');
$data[4] = (object)Array("result" => '');
$data[5] = (object)Array("result" => '');
$data[6] = (object)Array("result" => 1);

function searchArray($array)
{
    $our_keys = array();
    foreach($array as $key => $obj)
    {
        if($obj->result > 0)
        {
            $our_keys[] = $key;

        }
    }
    return $our_keys;
}

$keys = searchArray($data);
print_r($keys);

更新 - 试试这个你的 get_email_verification() 功能。它将电子邮件存储到一个名为 $emails 的数组中,像以前一样处理它们,但是我们没有返回上述数组,而是立即调用 searchArray() 并针对 $emails 数组处理结果并返回该列表。

function get_email_verification()
{
    $api_request_url = "http://api.payquicker.com/api/IsActiveAccount";
    $emails = $this->get_payquicker_emails();
    foreach ($emails as $email)
    {
        $param = "email=".$email;
        $result_request[] =  $this->do_request($api_request_url, $param, 'GET', 'json');
    }
    if (empty($result_request))
    {
        echo "All payee emails are already registered to Payquicker";
    }

    $email_keys = searchArray($result_request);
    $results = Array();
    foreach($email_keys as $key)
    {
        $results[] = $emails[$key];
    }

    return $results; 
}
于 2013-09-13T03:29:02.517 回答