删除数组php上的唯一索引

时间:2015-11-13 09:20:17

标签: php json

当我使用

时,我正试图在php上打印一个数组
echo json_encode($array);

它告诉我这个:

{
    "1": {
        "x": "145",
        "y": "20"
    },
    "2": {
        "x": "145",
        "y": "40"
    }
}

但我想要这个:

{
    {
         "x":"145",
         "y":"20"
    },
    {
        "x":"145",
        "y":"40"
    }
}

怎么做?

3 个答案:

答案 0 :(得分:6)

只需使用array_values,如

echo json_encode(array_values($array));

答案 1 :(得分:0)

为了达到这个目的,你需要像这样构建你的数组;

$arr = array();
$arr[] = array("x"=>145, "y"=>20);
$arr[] = array("x"=>145, "y"=>20);

[{"x":145,"y":20},{"x":145,"y":20}]

这将为您提供以下json_encode

$Articles =  DB::table('articles')->get();

//for test purpose here i can run a for-loop and print its values

foreach ($Articles as $Article) {
    echo "Article id : ".$Article->id";
    //here i cant print $Article->score
}

$Articles = $this->likesScoreFunction1($Articles);
$Articles = $this->scorefunction2($Articles);
$Articles = $this->scorefunction3($Articles);

答案 2 :(得分:0)

$newArray = array();
foreach ($array as $key => $val)
{
    $newArray[] = $val;
}

print_r(json_encode($newArray));

**Result**: [{"x":"145","y":"20"},{"x":"145","y":"40"}]