简体   繁体   English

如何在另一个数组中定义没有键的数组?

[英]How can I define an array without a key within another array?

The title sounds a bit confusing, although I'm sure there's a simple solution.标题听起来有点令人困惑,尽管我确信有一个简单的解决方案。 I'm using json_encode and am working on a web API.我正在使用 json_encode 并且正在开发 Web API。 The web API will echo an array that is json_encoded. Web API 将回显一个 json_encoded 数组。 I want it to look like this:我希望它看起来像这样:

{
    {
        "id": "test",
        "reason": "test reason"
    },
    {
        {
        "id": "test2",
        "reason": "test reason2"
    }
}

I've tried this:我试过这个:

$array = array();

$array[""] = array();
$array[""]["id"] = "test";
$array[""]["reason"] = "test reason";

$array[""] = array();
$array[""]["id"] = "test";
$array[""]["reason"] = "test reason";

This still has a key though ("") which is annoying.这仍然有一个键(“”),这很烦人。 I don't want a key.我不要钥匙。 How can I fix this?我怎样才能解决这个问题? Thanks!谢谢! :) :)

{
    {
        "id": "test",
        "reason": "test reason"
    },
    {
        {
        "id": "test2",
        "reason": "test reason2"
    }
}

This cannot be done for good reason : in javascript, the curly brackets indicate an object .这样做是有充分理由的:在 javascript 中,大括号表示一个对象 Within an object, each property must have a key.在一个对象中,每个属性都必须有一个键。 The brackets on the other end indicate a numerically-indexed Array .另一端的括号表示一个数字索引的Array

The Array type in php may be confusing because it's an all-in-one array, you can mix and match the keys as you see fit. php 中的Array类型可能会令人困惑,因为它是一个多合一的数组,您可以根据需要混合和匹配键。

So this, instead, is valid JSON and might be what you really need (if you don't care about the keys, you'll need to loop over the values):因此,这是有效的 JSON,并且可能是您真正需要的(如果您不关心键,则需要遍历值):

[
    {
        "id": "test",
        "reason": "test reason"
    },
    {
        {
        "id": "test2",
        "reason": "test reason2"
    }
]

EDIT编辑

You may create such an array in php through json_encode() like this :您可以通过json_encode()在 php 中创建这样的数组,如下所示:

$array = [["id"=> "test","reason"=> "test reason"],["id"=> "test","reason"=> "test reason"]];
echo json_encode($array);

I think this is more in line with your original code, you nearly had it nailed我认为这更符合您的原始代码,您差点就搞定了

<?php
$array = array();

// create 1st detail entry
$arrayDetail = array();
$arrayDetail["id"] = "test";
$arrayDetail["reason"] = "test reason";

$array[] = $arrayDetail; // add detail to array

// create 2nd detail entry
$arrayDetail = array();
$arrayDetail["id"] = "test";
$arrayDetail["reason"] = "test reason";

$array[] = $arrayDetail; // add detail to array

// show the output
var_dump(json_encode($array, JSON_PRETTY_PRINT)); 

You may get such format as below with array_push method,您可能会使用array_push方法获得如下格式,

$array = array();
array_push($array, array('id' => 'test', 'reason' => 'test reason' ) );
echo json_encode($array);

The resut will be a valid json format as结果将是有效的 json 格式,如

[{"id":"test","reason":"test reason"}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM