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

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据