2

I am trying to access decoded json elements that is in just one array, example:

 [
    {
    timestamp: 1509523044,
    tid: 83450451,
    price: "6381.0",
    amount: "1.0",
    type: "sell"
    },
    {
    timestamp: 1509523044,
    tid: 83450448,
    price: "6380.0",
    amount: "1.12894377",
    type: "buy"
    }
 ]

I have tried

$json = json_decode($result); 

echo $json[0]->price;
echo $json->[0]price;
echo $json->[0]->price;

Keep getting errors such as:

Fatal error: Cannot use object of type stdClass as array in

How can i acess each indivudal element with no array name? Thanks

5
  • Do you use valid JSON? Property names should be with double quotes Commented Nov 1, 2017 at 8:16
  • Hi Jigar, I am pulling it from rest api Commented Nov 1, 2017 at 8:20
  • var_dump($json) and what is the output? Commented Nov 1, 2017 at 8:21
  • This is not a valid json Commented Nov 1, 2017 at 8:29
  • In my case I was able to access what I needed with empty string as the key. $neededData = $myArray[''] Commented Mar 13, 2023 at 17:06

3 Answers 3

4

Your json is malformed: in order to have a valid json you should have something like this:

<?php
$a = '[{
    "timestamp": 1509523044,
    "tid": 83450451,
    "price": "6381.0",
    "amount": "1.0",
    "type": "sell"
},
{
    "timestamp": 1509523044,
    "tid": 83450448,
    "price": "6380.0",
    "amount": "1.12894377",
    "type": "buy"
}
]';
$b = json_decode($a, true);
var_dump($b);

This will return the array you need

Sign up to request clarification or add additional context in comments.

3 Comments

It has to be mentioned, that the first approach from Rand ($json[0]->price), would only work with json_decode($a, false);. And for a good reason, as stdObject are all fine to use in code, this represents the json better than faltten it to arrays.
Well, it depends on his needs. In the above code he's trying to decode a malformed json and then use it as an array. I have provided the answer to that specific scenario.
Regardless, that its malformed, the outer thing is an array, thus accessing with [0], but the inner one is a object, thus acessing with ->price.
1

When you use json_decode you get an object. If you need an array you have to set the second parameter to true.

$json = json_decode($result, true); 

Otherwise you have an object and you have to access all like an object. You can check that with var_dump($json);. Generally it's better to work with the object version instead of the array version. But sometimes you need arrays.

Edit:

what i've written in the comment your json is not valid but with the original one you gave me it's working well.

var_dump(json_decode(file_get_contents('https://api.bitfinex.com/v1/trades/BTCUSD'), true));

with your example input it's working.

7 Comments

If I try that I get Notice: Undefined offset: 0 echo $json[0]['price']; or am I doing it incorrectly?
I've tested you json i think that is not valid and you get "NULL" on your var_dump.
Hi Stony, I am getting it from api.bitfinex.com/v1/trades/BTCUSD
This is not the json you have on the description. The names are inside double-quotes.
@FabianN. no ;) that is my answer ;) and i don't teach people how to use it. I've written that you get an object if you read my answer ;) so i've written "If you need an array" ... so please don't tell me how to write my answers thx ;)
|
0

Because it's not a correct json you have to make it correct before decoding.

Here I use regex to replace the keys to "keys".

$str = ' [
{
timestamp: 1509523044,
tid: 83450451,
price: "6381.0",
amount: "1.0",
type: "sell"
},
{
timestamp: 1509523044,
tid: 83450448,
price: "6380.0",
amount: "1.12894377",
type: "buy"
}
]';


$json = json_decode(preg_replace('/(\w+):/', '"$1":', $str));
Echo $json[0]->price;

https://3v4l.org/MMTvm

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.