仅从php

时间:2018-05-01 06:55:46

标签: php arrays foreach

我在foreach中打印一个数组,它给了我重复的值。我只想要第一组数组,因为剩下的是重复的数组。有人可以帮忙吗?

foreach($data as $data1){ print_r($new_campaign);}

Array
(
    [0] => Array
        (
            [test camp] => 79.15057915057915
            [campaign  ak] => -
            [camp] => -
        )

    [1] => Array
        (
            [test camp] => 78.52522639068565
            [campaign  ak] => -
            [camp] => -
        )

    [2] => Array
        (
            [test camp] => -
            [campaign  ak] => NA
            [camp] => 63.74807987711214
        )

)
Array
(
    [0] => Array
        (
            [test camp] => 79.15057915057915
            [campaign  ak] => -
            [camp] => -
        )

    [1] => Array
        (
            [test camp] => 78.52522639068565
            [campaign  ak] => -
            [camp] => -
        )

    [2] => Array
        (
            [test camp] => -
            [campaign  ak] => NA
            [camp] => 63.74807987711214
        )

)
Array
(
    [0] => Array
        (
            [test camp] => 79.15057915057915
            [campaign  ak] => -
            [camp] => -
        )

    [1] => Array
        (
            [test camp] => 78.52522639068565
            [campaign  ak] => -
            [camp] => -
        )

    [2] => Array
        (
            [test camp] => -
            [campaign  ak] => NA
            [camp] => 63.74807987711214
        )

)

2 个答案:

答案 0 :(得分:0)

尝试使用数组变量的正确索引:

echo $array[0]['test camp'];

答案 1 :(得分:0)

你在foreach循环中调用$new_campaign数组,所以重复它。在外部循环中调用它将返回1个数组,

print_r($new_campaign);

Array
(
    [0] => Array
        (
            [test camp] => 79.15057915057915
            [campaign  ak] => -
            [camp] => -
        )

    [1] => Array
        (
            [test camp] => 78.52522639068565
            [campaign  ak] => -
            [camp] => -
        )

    [2] => Array
        (
            [test camp] => -
            [campaign  ak] => NA
            [camp] => 63.74807987711214
        )

)