Jump to content

selecting something inside an array


peterhuynh

Recommended Posts

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 by peterhuynh
Link to comment
Share on other sites

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? 

Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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,

Link to comment
Share on other sites

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'];
Link to comment
Share on other sites

$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
        )

)
Link to comment
Share on other sites

$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"

Link to comment
Share on other sites

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>';
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.