试图将PHP中的变量转换为数组

时间:2018-01-28 20:00:46

标签: php multidimensional-array

我有一个我从API调用中检索到的变量,并且正在尝试将输出转换为数组。以下是输出的示例:

{"id":"4335416","linkId":4335416,"title":"Website 1","slashtag":"1","destination":"http://website1.com","createdAt":"2017-09-20T12:10:56.000Z","updatedAt":"2017-12-01T13:59:35.000Z","status":"active","clicks":722,"lastClickDate":"2018-01-28T15:47:21.000Z","lastClickAt":"2018-01-28T15:47:21.000Z","isPublic":false,"shortUrl":"url.st/1","domainId":"abc123","domainName":"url.st","domain":{"id":"abc123","ref":"/domains/abc123","fullName":"url.st","active":true},"https":false,"favourite":false,"creator":{"id":"321cba","fullName":"user123","avatarUrl":"https://s.gravatar.com/avatar/"}},
{"id":"4335402","linkId":4335402,"title":"Website 2","slashtag":"2","destination":"http://website2.com","createdAt":"2017-09-20T12:09:29.000Z","updatedAt":"2017-12-01T13:58:56.000Z","status":"active","clicks":234,"lastClickDate":"2018-01-28T13:44:29.000Z","lastClickAt":"2018-01-28T13:44:29.000Z","isPublic":false,"shortUrl":"url.st/2","domainId":"abc123","domainName":"url.st","domain":{"id":"abc123","ref":"/domains/abc123","fullName":"url.st","active":true},"https":false,"favourite":false,"creator":{"id":"321cba","fullName":"user123","avatarUrl":"https://s.gravatar.com/avatar/"}},
{"id":"4335375","linkId":4335375,"title":"Website 3","slashtag":"3","destination":"http://website3.com","createdAt":"2017-09-20T12:07:23.000Z","updatedAt":"2017-12-20T18:43:17.000Z","status":"active","clicks":111,"lastClickDate":null,"lastClickAt":null,"isPublic":false,"shortUrl":"url.st/3","domainId":"abc123","domainName":"url.st","domain":{"id":"abc123","ref":"/domains/abc123","fullName":"url.st","active":true},"https":false,"favourite":false,"creator":{"id":"321cba","fullName":"user123","avatarUrl":"https://s.gravatar.com/avatar/"}}

我想在数组中创建一个数组,如下所示:

$myArray = array( array( id => "4335416", 
                      linkId => 4335416,
                      title => "Website 1",
                      slashtag => "1",
                      destination => "http://website1.com",
                      ...
                    ),
               array( id => "4335402", 
                      linkId => 4335402,
                      title => "Website 2",
                      slashtag => "2",
                      destination => "http://website2.com",
                      ...
                    )
               array( id => "4335375", 
                      linkId => 4335375,
                      title => "Website 3",
                      slashtag => "3",
                      destination => "http://website3.com",
                      ...
                    )
             );

完全被这个困扰了......虽然我的脑袋一整天都在努力做事:(

非常感谢:)

4 个答案:

答案 0 :(得分:1)

您可以将其转换为数组:

json_decode($myArray, true);

第二个参数true将使它成为一个关联数组,其中包含来自JSON数据的命名键。

json_decode documentation

答案 1 :(得分:0)

您正在寻找json_decode('APIcallValue', true)

PHP manual

答案 2 :(得分:0)

此API响应似乎是JSON。

尝试做:

$myArray = json_decode($api_response, true);

其中$ api_response是API响应变量

答案 3 :(得分:0)

感谢那些家伙......

json_decode($api_response, true)

工作完美。当你知道所有不同的命令时,这有多简单! LMAO。应该刚刚在这里发布了......节省了几个小时...

再次感谢