peterhuynh Posted February 21, 2015 Share Posted February 21, 2015 (edited) Ahh! A little bit of help or a hint? This is the result: {"ltc_btc":{"asks":[[0.00757,109.7613],[0.00758,83.72171025]],"bids":[[0.00754,0.782],[0.00753,283.68702632],[0.00752,608.06368173],[0.00751,1058.66307725],[0.0075,692.06028608]]}} I want to sort "asks" and "bids". This is where I'm having a problem. This is the code I'm using: $array = $array->ltc_btc; rsort($array['bids']); sort($array['asks']); $btce_bids = $array->bids; $btce_bids = $array['bids'][0][0]; $btce_bids_quantity = $array->bids; $btce_bids_quantity = $array['bids'][0][1]; $btce_asks = $array->asks; $btce_asks = $array['asks'][0][0]; $btce_asks_quantity = $array->asks; $btce_asks_quantity = $array['asks'][0][1]; After it sorts it, it select some stuff out of the array. (I think this part is no problem but just putting it up here). Edited February 21, 2015 by peterhuynh Quote Link to comment Share on other sites More sharing options...
rwhite35 Posted February 21, 2015 Share Posted February 21, 2015 Take a look at usort (user defined sort). Because you're working with an object, you'll probably need to create an algorithm for sorting the bid/ask. Plus you're overloading the variables, I'm assuming that is your intention. so $btce_bids is equal to $array['bid'][0][0] and overwrites $array->bids. This looks like something Bitcoin. Is it? Quote Link to comment Share on other sites More sharing options...
Barand Posted February 21, 2015 Share Posted February 21, 2015 How many times do we have to answer this question for you? http://forums.phpfreaks.com/topic/294604-sorting-multidimensional-array/?do=findComment&comment=1505682 Quote Link to comment Share on other sites More sharing options...
peterhuynh Posted February 21, 2015 Author Share Posted February 21, 2015 How many times do we have to answer this question for you? http://forums.phpfreaks.com/topic/294604-sorting-multidimensional-array/?do=findComment&comment=1505682 Hi, The problem I'm having is getting to the 'bids' section of the array. Whereas in the link, the 'bids' section is immediately available. Here, the 'bids' section is nested in a set called 'ltc_btc'. This is where the confusion is derived from. Thanks for the help. Quote Link to comment Share on other sites More sharing options...
peterhuynh Posted February 21, 2015 Author Share Posted February 21, 2015 Take a look at usort (user defined sort). Because you're working with an object, you'll probably need to create an algorithm for sorting the bid/ask. Plus you're overloading the variables, I'm assuming that is your intention. so $btce_bids is equal to $array['bid'][0][0] and overwrites $array->bids. This looks like something Bitcoin. Is it? I don't think that is my intention. I'm VERY new to php, so I'm just doing things as I go along to see whatever works. Can you tell me a bit more about overloading variables? Thanks, Quote Link to comment Share on other sites More sharing options...
Barand Posted February 21, 2015 Share Posted February 21, 2015 Hi, The problem I'm having is getting to the 'bids' section of the array. Whereas in the link, the 'bids' section is immediately available. Here, the 'bids' section is nested in a set called 'ltc_btc'. This is where the confusion is derived from. Thanks for the help. In which case you would use $asks = $array['ltc_btc']['asks']; Quote Link to comment Share on other sites More sharing options...
peterhuynh Posted February 21, 2015 Author Share Posted February 21, 2015 In which case you would use $asks = $array['ltc_btc']['asks']; Yup, I tried that before I posted. I tried to sort $ask, and I get an error. When I use print_r to show the contents of $ask, I get nothing. So I'm still not sure what the problem is. Quote Link to comment Share on other sites More sharing options...
Barand Posted February 22, 2015 Share Posted February 22, 2015 $data = '{"ltc_btc":{"asks":[[0.00757,109.7613],[0.00758,83.72171025]],"bids":[[0.00754,0.782],[0.00753,283.68702632],[0.00752,608.06368173],[0.00751,1058.66307725],[0.0075,692.06028608]]}}'; $arr = json_decode($data, 1); $asks = $arr['ltc_btc']['asks']; sort($asks); echo '<pre>',print_r($asks, true),'</pre>'; result Array ( [0] => Array ( [0] => 0.00757 [1] => 109.7613 ) [1] => Array ( [0] => 0.00758 [1] => 83.72171025 ) ) Quote Link to comment Share on other sites More sharing options...
peterhuynh Posted February 22, 2015 Author Share Posted February 22, 2015 $data = '{"ltc_btc":{"asks":[[0.00757,109.7613],[0.00758,83.72171025]],"bids":[[0.00754,0.782],[0.00753,283.68702632],[0.00752,608.06368173],[0.00751,1058.66307725],[0.0075,692.06028608]]}}'; $arr = json_decode($data, 1); $asks = $arr['ltc_btc']['asks']; sort($asks); echo '<pre>',print_r($asks, true),'</pre>'; result Array ( [0] => Array ( [0] => 0.00757 [1] => 109.7613 ) [1] => Array ( [0] => 0.00758 [1] => 83.72171025 ) ) Here is my script: $ch = curl_init('https://btc-e.com/api/3/depth/ltc_btc'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); $result = curl_exec($ch); $array = json_decode($result, 1); $asks = $array['ltc_btc']['asks']; sort($asks); echo '<pre>', print_r($asks, true), '</pre>'; ?> And this is the result I get: "Warning: sort() expects parameter 1 to be array, null given in /home/a2926161/public_html/btce_ltcbtc_orderbook.php on line 10" Quote Link to comment Share on other sites More sharing options...
Barand Posted February 22, 2015 Share Posted February 22, 2015 Put a check in there $ch = curl_init('https://btc-e.com/api/3/depth/ltc_btc'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); $result = curl_exec($ch); if (!$result) { exit("cURL failed"); } $array = json_decode($result, 1); $asks = $array['ltc_btc']['asks']; sort($asks); echo '<pre>', print_r($asks, true), '</pre>'; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.