-2

私は配列を持っています:

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で配列を返すにはどうすればよいですか?

例: id = 40; 戻る:

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

ありがとう。

4

6 に答える 6

0

Try:

 $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;
}
于 2013-09-17T12:38:20.710 に答える
0

これは、インターフェイス \Iteratorを実装するクラスのスニペットです。配列$this->dataに置き換えられた場合 (おそらく 3 番目の引数から)、任意の配列で動作するように編集できます。

/**
 * 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 );
于 2013-09-17T12:36:21.687 に答える
-1

これを試して

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

ここで静的な例を示しました。

于 2013-09-17T12:26:02.477 に答える