PHP保存数组结果在变量中


PHP save array results in variable

让我说我有一个这样的数组:

$testarray = array('abc'=>'123',
              'def'=>'456',
              'ghi'=>'789'
);

对于一个操作,我需要变量中数组的单个值。我想循环通过数组,并有这样的东西:

new cmdOption("cn", $arrayvaluefirstcolumn, "User")
new cmdOption("mod", "text=".$arraysecondcolumn, "expression")

因此,我希望在第一个循环中,"abc"作为$arrayvaluefirstcolumn,"123"作为$arrayvaluesecondcolumn。在第二个循环中,我希望"def"为$arrayvaluefirstcolumn,"456"为$arrayvaluesecondcolumn,依此类推

我不知道如何在数组中循环以获得所需的结果并将其存储在变量中。这可能吗?你能给我一些建议吗?

您唯一需要做的就是循环遍历您的数组。

$testarray = array(
    'abc' => '123',
    'def' => '456',
    'ghi'=>'789'
);
foreach ($testArray as $key => $value) {
    new cmdOption("cn", $key, "User");
    new cmdOption("mod", "text=".$value, "expression");
}
foreach($testarray as $key => $value)
{
    //returns 'abc', 'def', 'ghi'
    $arrayvaluefirstcolumn = $key;
    //returns '123', '456', '789'
    $arrayvaluesecondcolumn = $value;
}