0

I have this code:

{"DT_RowId":"row_8","siparisler":{"id":"8","tarih":"14-12-2015","uid":"118","mid":"4","satis_fiyati":"5","adet":"7","odeme":"1","olusturan":"ares","sonduzenleyen":""},"urunler":{"urun_adi":"BANNER 100 AH","stok_kodu":"10010"},"musteriler":{"unvan":"3A Otomotiv San. Tic. Ltd. \u015eti."},"kdvsiz":"4.23728813559","kdv":"0.762711864407","top":"35"}

How can get siparisler id with array index?

$result2 = json_decode ($val,true);
return $result2[1][0];

I want to get that with array index because my JSON names are always different.

or how can i get second array name for if control ?

5
  • json_decode return StdClass Commented Dec 14, 2015 at 15:38
  • result2["siparisler"]["id"] Commented Dec 14, 2015 at 15:41
  • Yes Andrew but my siparisler name always differnt i need get with array index Commented Dec 14, 2015 at 15:42
  • or i need second array name for if control ? Commented Dec 14, 2015 at 15:44
  • 1
    or maybe use a static index name before json_encode Commented Dec 14, 2015 at 15:45

1 Answer 1

2

you could use array_values() to normalize the arrays:

return array_values(array_values(json_decode($val, true))[1])[0];

array_values() returns all the values from the array and indexes the array numerically.

The same split into multiple statements to be more clear:

$result = json_decode($val, true);
$result = array_values($result)[1];
$result = array_values($result)[0];
return $result;

Or for PHP 5.3 and below:

$result = json_decode($val, true);
$result = array_values($result);
$result = array_values($result[1]);
return $result[0];
Sign up to request clarification or add additional context in comments.

7 Comments

This code right ? coz I keep getting error ? Thank you
are you using an outdated PHP version? Array dereferencing (func()[]) was introduced in 5.4
5.3 is no longer supported. Please consider updating to at least 5.5
Nonetheless, I updated the answer with a solution for 5.3
Thank you, you are awesome.
|

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.