按ID查找数组

时间:2013-09-17 12:21:43

标签: php arrays multidimensional-array

我有一个数组:

Array
(
    [0] => Array
        (
            [id] => 1
            [email] => email1@account.com
            [fullname] => name1
            [phone] => phone
        )
    [1] => Array
        (
            [id] => 2
            [email] => email2@account.com
            [fullname] => name2
            [phone] => phone
        )
    [2] => Array
        (
            [id] => 40
            [email] => email@account.com
            [fullname] => namex
            [phone] => phone
        )
)

如何使用php返回数组?

例如:id = 40;返回:

[id] => 40
[email] => email@account.com
[fullname] => namex
[phone] => phone 

感谢。

6 个答案:

答案 0 :(得分:3)

$return = 0;
foreach($array as $row) {
   if (40 == $row['id']) {
      $return = $row;
      break;
   }
}

var_dump($return);

答案 1 :(得分:3)

为此创建函数,如下所示,

function arraysearch($array, $id)
{
   foreach($array as $key => $value)
   {
      if ( $value['id'] == $id ) {
         return $key;
      }
   }
   return false;
}

并按以下方式调用,

arraysearch($yourarray, 40);

答案 2 :(得分:0)

做这样的事情,它只是一个伪代码。

$matchId    = 40;
foreach( $array as $key => $each ){
    if( $each['id'] == $matchId ){
        $result = $array[$key];
    }
}
return $result;

答案 3 :(得分:0)

这是来自实现interface \Iterator的类的片段,如果$this->data被数组替换(可能来自第三个参数),则可以对其进行编辑以使用任何数组:

/**
 * Find an item by one of it's properties from the internal data array.
 *
 * @param string $property
 * @param mixed $value
 * @return mixed Return NULL if no matching item was found; return FALSE if no such property exists.
 */
public function getByProperty( $property, $value )
{
    reset( $this->data );
    $anArrayEntryFromData = current( $this->data );
    if( !isset( $anArrayEntryFromData[$property] ) )
    {
        /* there is no such $property */
        throw new \UnexpectedValueException( sprintf( '%s::%s() – There is no property "%s" in the internal data array for this collection. The available properties for this collection are: %s', get_called_class(), __FUNCTION__, $property, implode( ', ', array_keys( $anArrayEntryFromData ) ) ) );
    }

    foreach( $this->data as $valueArray )
    {
        if( is_scalar( $value ) && !is_numeric( $value ) )
        {
            if( strtolower( $valueArray[$property] ) == strtolower( $value ) )
            {
                return $valueArray[$property];
            }
        }
        else
        {
            if( $valueArray[$property] == $value )
            {
                return $valueArray[$property];
            }
        }
    }

    return null;
}

用法:

$iterator->getByProperty( 'id', 40 );

答案 4 :(得分:0)

尝试:

 $array = array (
    "0" => array
        (
            "id" => "1",
            "email" => "email1@account.com",
            "fullname" => "name1",
            "phone" => "phone"
        ),
    "1" => array
        (
            "id" => 2,
            "email" => "email2@account.com",
            "fullname" => "name2",
            "phone" => "phone"
        ),
    "2" => array
        (
            "id" => 40,
            "email" => "email@account.com",
            "fullname" => "namex",
            "phone" => "phone"
        )
);


$id = 40;
print_r(getArray($array, $id));

function getArray($array, $id)
{
    $result = array();
    foreach( $array as $key => $value ){
        if( $value['id'] == $id ){
            $result = $value;
                    break;
        }
    }
    return $result;
}

答案 5 :(得分:-1)

试试这个

$data=your array;
foreach($data as $key=>$each)
{
    if($each['id']=="40")
    {
        return $data[$key];
    }
}

我在这里给出了一个静态的例子。

相关问题