parseresponse({“eurusd”:{ “id”: “eurusd”, “category”: “Forex”, “price”: 1.3161, “name”: “EUR/USD”, “buy”: 1.3162, “sell”: 1.3159, “change”: 0.00, “date”:1328288216000}});for some reason I can”t replace it to Array when I using:

var_dump(json_decode($content));and I trying with php function also:

function object2array($object) {if (is_object($object)) foreach ($object as $key => $value) $array<$key> = $value; else $array = $object;return $array;}any idea?..

Đang xem: String object to array with php

*

You”re trying to parse JSONP response as JSON, you should remove wrapping function first.

Xem thêm: Bí Quyết Nuôi Gà Con Nhanh Lớn Chi Tiết Nhất, Hướng Dẫn Cách Nuôi Gà Con Mới Nở

$response = “parseresponse({“eurusd”:{ “id”: “eurusd”, “category”: “Forex”, “price”: 1.3161, “name”: “EUR/USD”, “buy”: 1.3162, “sell”: 1.3159, “change”: 0.00, “date”:1328288216000}});”;$json = preg_replace(“/^parseresponse((.*));/”, “$1”, $response);$data = json_decode($json, true);print_r($data);

*

You could try something like this:

$content = “{“eurusd”:{ “id”: “eurusd”, “category”: “Forex”, “price”: 1.3161, “name”: “EUR/USD”, “buy”: 1.3162, “sell”: 1.3159, “change”: 0.00, “date”:1328288216000}}”;function toArray($data) { if (is_object($data)) $data = get_object_vars($data); return is_array($data) ? array_map(__FUNCTION__, $data) : $data; }$newData = toArray (json_decode($content));print_r($newData);output will be:

Array( => Array ( => eurusd => Forex => 1.3161 => EUR/USD => 1.3162 => 1.3159 => 0 => 1328288216000 )

)

*

Thanks for contributing an answer to Stack Overflow!

Please be sure to answer the question. Provide details and share your research!

But avoid

Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.

Xem thêm: 10 Cách Cắm Hoa Đẹp Và Đơn Giản Đẹp Nhất, 4 Cách Cắm Hoa Đơn Giản Cho Người Mới Bắt Đầu

To learn more, see our tips on writing great answers.

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged php arrays object or ask your own question.

*

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev2021.3.17.38821

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *