dotxxh0998 2015-12-14 15:34
浏览 98
已采纳

Php Json获取数值数组索引?

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 ?

  • 写回答

1条回答 默认 最新

  • dounianxie2058 2015-12-14 15:54
    关注

    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];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致