0
{
  "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
    }
  }
}

how to get loop value amount..this array has random value 1400151861513776..change everytime..

i use php code.. json_decode..

4
  • forEach Maybe? Commented Apr 29, 2019 at 3:15
  • Please use json_decode($yourJson, true) and then loop through this array using foreach($array as $key => $value) where $key will containt that 1400151861513776 numbers. Commented Apr 29, 2019 at 3:31
  • What is it you want to do? Commented Apr 29, 2019 at 3:52
  • how to get key only? Commented Apr 29, 2019 at 13:07

2 Answers 2

1
  1. You can use json_decode to convert JSON to an array. PHP json_decode()

    $jsonToArray = json_decode($json,true); // $json has the `JSON`
    
  2. If you need the key with the amount you can use array_walk PHP array_walk()

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

Output:-

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

OR

If you don't need key, only the amount you can use array_map PHP array_map()

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

Output :-

 Array
(
  [0] => 138959.22155687
  [1] => 4115.53246448
   .......
   .......
)
Sign up to request clarification or add additional context in comments.

1 Comment

how to get key only?
1

Follow the steps mentioned to get the required data.

Step 1: Make your data a valid JSON string

$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
    }
  }
}';

Here I have wrapped your data in single quote to make it a valid JSON string

Step 2: Use json_decode function to decode the JSON string

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

When using the json_decode function, please make sure to set $assoc flag to true. Else it will return an object instead of an associative array.

Step 3: Select the data on which you need to loop on

$selected_data = $decoded_data["return"];

In this case its the return key in the decoded JSON.

Step 4: Loop over the selected data to obtain the key and values

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

$key will be holding the random value like 1400151861513776 and $value will be holding the data inside that key

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.