-2

我在 PHP 中有一组数组,如下所示

Array ( 
[0] => Array 
  ( 
  [SEQ] => 2 
  [CODE] => cd1 
  [DESC] => desc1 
  ) 
[1] => Array 
  ( 
  [SEQ] => 7 
  [CODE] => cd2 
  [DESC] => desc2 
  ) 
)

我想将其转换为如下所示。它应该提取 SEQ 值和 CODE 值并将它们分配为相应的键和值

Array(
"2" => cd1,
"7" => cd2
)
4

2 回答 2

6
$array = array(
    array('SEQ' => 2, 'CODE' => 'cd1', 'DESC' => 'desc1'),
    array('SEQ' => 7, 'CODE' => 'cd2', 'DESC' => 'desc2'),
    array('SEQ' => 10, 'CODE' => 'cd3', 'DESC' => 'desc3')
);

$result = array();
foreach ($array as $row) {
    if (isset($row['SEQ']) && isset($row['CODE'])) {
        $result[$row['SEQ']] = $row['CODE'];
    }
}

print_r($result);

输出:

Array
(
    [2] => cd1
    [7] => cd2
    [10] => cd3
)
于 2013-06-15T07:04:42.940 回答
0

假设有问题的数组是 $ret 这应该对你有用

$ret = array(); 
foreach($array as $val){
    $ret[$val["SEQ"]] = $val["CODE"];
}
于 2013-06-15T07:00:34.930 回答