0

我有一个数组代码,例如:

$responeArray = array();
$_counter = 0;
foreach ($xmlResp->readCalls->classify as $readCalls) {
    $ClassificationClass = array();
    foreach ($readCalls->classification->{'class'} as $classes) {
        $ClassificationClass[] = implode(" ", array('p' => (string)$classes['p']));
    }
    $responeArray[] = $ClassificationClass;
    $_counter++;
}
return $responeArray;

这将给出类似的结果

Array
(
    [0] => Array
        (
            [0] => 0.999999
            [1] => 5.65423e-007
            [2] => 2.3301e-008
        )

    [1] => Array
        (
            [0] => 0.333333
            [1] => 0.333333
            [2] => 0.333333
        )

    [2] => Array
        (
            [0] => 1.19172e-007
            [1] => 0.999993
            [2] => 6.75659e-006
        )
)

我的目的是得到类似的结果

0.999999 5.65423e-007 2.3301e-008
...
1.19172e-007 0.999993 6.75659e-006

我试过用这个

$responeArray = array();
$_counter = 0;
foreach ($xmlResp->readCalls->classify as $readCalls) {
    $ClassificationClass = array();
    foreach ($readCalls->classification->{'class'} as $classes) {
        $ClassificationClass[] = implode(" ", array('p' => (string)$classes['p']));
    }
    $responeArray[] = $ClassificationClass;
    $nilaineg = $ClassificationClass[0];
    $nilainet = $ClassificationClass[1];
    $nilaipos = $ClassificationClass[2];
    $_counter++;
}
return $nilaineg . ' ' . $nilainet . ' ' . $nilaipos;

但上面的代码只是输出:

1.19172e-007 0.999993 6.75659e-006

我错过了什么吗?

4

2 回答 2

0

我希望这能帮到您

    $array = array
(
    array(
        0.999999,
        5.65423e-007,
        2.3301e-008
    ),

    array
    (
        0.333333,
        0.333333,
        0.333333
    ),

    array
    (
        1.19172e-007,
        0.999993,
        6.75659e-006
    )
);

foreach($array as $item)
    print_r(implode(" ", $item)."\n");
于 2013-08-24T08:26:53.560 回答
0

您只从整个循环中获取一个变量。当您每次都覆盖最后一组行时。更改代码从循环内删除return并放置一个echo,如下所示:

$responeArray = array();
$_counter = 0;
foreach ($xmlResp->readCalls->classify as $readCalls) {
    $ClassificationClass = array();
    foreach ($readCalls->classification->{'class'} as $classes) {
        $ClassificationClass[] = implode(" ", array('p' => (string)$classes['p']));
    }
    $responeArray[] = $ClassificationClass;
    $nilaineg = $ClassificationClass[0];
    $nilainet = $ClassificationClass[1];
    $nilaipos = $ClassificationClass[2];
    $_counter++;

    /**
     *  Echo from inside the loop
    **/
    echo $nilaineg . ' ' . $nilainet . ' ' . $nilaipos . '<br />';
}

或者,将它们放入另一个数组中,稍后您可以在其中循环它们,这将保留return您之前包含的原始语句。

foreach ($xmlResp->readCalls->classify as $readCalls) {
   //Code..
   $ReturnArray[] = $nilaineg . ' ' . $nilainet . ' ' . $nilaipos;
}
return $ReturnArray;

然后稍后,您可以使用:

print_r( $ReturnedArray );

Array =>
    (
        [0] => 0.999999 5.65423e-007 2.3301e-008
        [1] => 0.333333 0.333333 0.333333
        //etc
    )
于 2013-08-24T08:41:02.970 回答