【问题标题】:how to get loop JSON value amount with random value array use PHP如何使用 PHP 使用随机值数组获取循环 JSON 值数量
【发布时间】:2019-04-29 03:13:21
【问题描述】:
{
  "success": 1,
  "return": {
    "1400151861513776": {
      "pair": "edc_btc",
      "type": "buy",
      "amount": 138959.22155687,
      "rate": 0.00000085,
      "timestamp_created": "1556464987",
      "status": 0
    },
    "1400151861456538": {
      "pair": "edc_btc",
      "type": "buy",
      "amount": 4115.53246448,
      "rate": 0.00000085,
      "timestamp_created": "1556463520",
      "status": 0
    },
    "1400151861402138": {
      "pair": "edc_btc",
      "type": "buy",
      "amount": 252.29423504,
      "rate": 0.00000085,
      "timestamp_created": "1556462106",
      "status": 0
    },
    "1400151861205651": {
      "pair": "edc_btc",
      "type": "buy",
      "amount": 5735.02289537,
      "rate": 0.00000085,
      "timestamp_created": "1556457111",
      "status": 0
    },
    "1400151861064946": {
      "pair": "edc_btc",
      "type": "buy",
      "amount": 608.2294235,
      "rate": 0.00000085,
      "timestamp_created": "1556453555",
      "status": 0
    },
    "1400151860984352": {
      "pair": "edc_btc",
      "type": "buy",
      "amount": 13553.51532229,
      "rate": 0.00000085,
      "timestamp_created": "1556451515",
      "status": 0
    },
    "1400151860967764": {
      "pair": "edc_btc",
      "type": "buy",
      "amount": 49475.62404601,
      "rate": 0.00000085,
      "timestamp_created": "1556451103",
      "status": 0
    },
    "1400151860901030": {
      "pair": "edc_btc",
      "type": "buy",
      "amount": 21474.82564282,
      "rate": 0.00000085,
      "timestamp_created": "1556449399",
      "status": 0
    },
    "1400151860889146": {
      "pair": "edc_btc",
      "type": "buy",
      "amount": 2657.50733826,
      "rate": 0.00000085,
      "timestamp_created": "1556449090",
      "status": 0
    },
    "1400151860484795": {
      "pair": "edc_btc",
      "type": "buy",
      "amount": 71933.21911691,
      "rate": 0.00000085,
      "timestamp_created": "1556438570",
      "status": 0
    },
    "2400151859280443": {
      "pair": "edc_btc",
      "type": "sell",
      "amount": 266054.68380596,
      "rate": 0.00000088,
      "timestamp_created": "1556408217",
      "status": 0
    },
    "2400151857916444": {
      "pair": "edc_btc",
      "type": "sell",
      "amount": 400000,
      "rate": 0.0000009,
      "timestamp_created": "1556374931",
      "status": 0
    },
    "2400151857916059": {
      "pair": "edc_btc",
      "type": "sell",
      "amount": 400000,
      "rate": 0.00000089,
      "timestamp_created": "1556374923",
      "status": 0
    }
  }
}

如何获取循环值数量..这个数组有随机值1400151861513776..每次都改变..

我使用 php 代码..json_decode..

【问题讨论】:

  • forEach 也许?
  • 请使用json_decode($yourJson, true),然后使用foreach($array as $key => $value) 循环遍历此数组,其中$key 将包含1400151861513776 数字。
  • 你想做什么?
  • 如何只获取密钥?

标签: php arrays json random


【解决方案1】:
  1. 您可以使用json_decode 将 JSON 转换为数组。 PHP json_decode()

    $jsonToArray = json_decode($json,true); // $json has the `JSON`
    
  2. 如果您需要keyamount,您可以使用array_walk PHP array_walk()

    $jsonToArray = json_decode($json,true);
    $res = [];
    array_walk($jsonToArray['return'], function($v, $k) use (&$res){
      $res[$k] = $v['amount'];
    });
    

输出:-

Array
(
  [1400151861513776] => 138959.22155687
  [1400151861456538] => 4115.53246448
   .......
   .......
  [2400151857916444] => 400000
  [2400151857916059] => 400000
)

如果你不需要key,只有amount你可以使用array_mapPHP array_map()

$jsonToArray = json_decode($json,true);
$res = [];
array_map(function($v) use(&$res){
  $res[] = $v['amount'];
}, $jsonToArray['return']);

输出:-

 Array
(
  [0] => 138959.22155687
  [1] => 4115.53246448
   .......
   .......
)

【讨论】:

  • 如何只获取密钥?
【解决方案2】:

按照上述步骤获取所需数据。

第 1 步:使您的数据成为有效的 JSON 字符串

$json_string = '{
  "success": 1,
  "return": {
    "1400151861513776": {
      "pair": "edc_btc",
      "type": "buy",
      "amount": 138959.22155687,
      "rate": 0.00000085,
      "timestamp_created": "1556464987",
      "status": 0
    },
    "1400151861456538": {
      "pair": "edc_btc",
      "type": "buy",
      "amount": 4115.53246448,
      "rate": 0.00000085,
      "timestamp_created": "1556463520",
      "status": 0
    }
  }
}';

在这里,我将您的数据用单引号括起来,使其成为有效的 JSON 字符串

第 2 步:使用 json_decode 函数解码 JSON 字符串

$decoded_data = json_decode($json_string, $assoc=true);

使用json_decode功能时,请务必将$assoc标志设置为true。否则它将返回一个对象而不是关联数组。

第 3 步:选择需要循环的数据

$selected_data = $decoded_data["return"];

在这种情况下,它是解码后的 JSON 中的 return 键。

第 4 步:循环选择数据以获取键和值

foreach($selected_data as $key=>$value) {
  var_dump($key); # random value like 1400151861513776
}

$key 将保存像 1400151861513776 这样的随机值,而 $value 将保存该键内的数据

【讨论】:

    猜你喜欢
    • 2021-11-02
    • 2012-07-11
    • 1970-01-01
    • 2017-06-26
    • 2011-12-13
    • 2013-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多