搜索数组值

时间:2013-08-03 10:28:31

标签: php

大家好,我有以下数组。我需要基于Id值的所有值。我怎么能这样做。

Array
(
[0] => Array
    (
        [Id] => 2
        [Description] => Get first Description
        [Code] => GF
        [Value] => 1.0
    )

[1] => Array
    (
        [Id] => 3
        [Description] => Get second Description
        [Code] => GF
        [Value] => 1.0
    )

[2] => Array
    (
        [Id] => 4
        [Description] => Get third Description
        [Code] => GF
        [Value] => 1.0
    )

2 个答案:

答案 0 :(得分:0)

尝试

foreach($my_arr as $arr) {
   $new_arr[$arr['ID']] = $arr['Value'];
}
print_r($new_arr);

答案 1 :(得分:0)

<强>被修改

$array = array(
    array(
        'Id' => '1',
        'Description' => 'Get first Description',
        'Code' => 'GF',
        'Value' => 'Test 1'
    ),
    array(
        'Id' => '2',
        'Description' => 'Get second Description',
        'Code' => 'GF',
        'Value' => 'Test 2'
    ),
    array(
        'Id' => '3',
        'Description' => 'Get third  Description',
        'Code' => 'GF',
        'Value' => 'Test 3'
    )
);

function getArrayValue( $array , $id )
{
    foreach( $array as $a )
    {
        if( $a['Id'] == $id )
        {
            return $a;
        }
    }
    return 'U/N';
}

print_r( getArrayValue( $array , '2' ) );