在数组中搜索给定值的索引

时间:2013-09-13 03:05:34

标签: php arrays

如何获得给定正值的数组索引或下面的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; 
      }

2 个答案:

答案 0 :(得分:0)

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

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

$results = array();

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

示例数组的结果将是值为[1,6]

的数组

答案 1 :(得分:0)

试试这个:

DEMO HERE: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(),然后再调用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; 
}