janders Posted April 28, 2008 Share Posted April 28, 2008 Hey Everyone, I'm new to this form but have a good amount of PHP knowledge. I am trying to do something really simple but running into a problem: I have an array that looks like this: Array ( [0] => 6.99 [rate] => 6.99 [1] => 6.89033 [semi_annual_rate] => 6.89033 ) I do the following to get values: $rate=$rate['rate']; $semi_annual_rate=$rate['semi_annual_rate']; echo $rate 6.99; // Returns 6.99 which is correct echo $semi_annual_rate; // Returns 6 when it should be returning 6.89033 Does anyone know why I can't get $semi_annual_rate to return 6.89033? I tried number format and a couple other functions but nothing seemed to work. Any help would be greatly appreciated! Link to comment https://forums.phpfreaks.com/topic/103264-solved-losings-decimals-from-array/ Share on other sites More sharing options...
jonsjava Posted April 28, 2008 Share Posted April 28, 2008 try this: Array ( * => 6.99 [rate] => "6.99" [1] => "6.89033" [semi_annual_rate] => "6.89033" ) Link to comment https://forums.phpfreaks.com/topic/103264-solved-losings-decimals-from-array/#findComment-528886 Share on other sites More sharing options...
jonsjava Posted April 28, 2008 Share Posted April 28, 2008 um...more to the point: <?php $array1 = array(rate=>"6.99", 1=>"6.89033", semi_annual_rate=>"6.89033"); print $array1['semi_annual_rate']; Link to comment https://forums.phpfreaks.com/topic/103264-solved-losings-decimals-from-array/#findComment-528891 Share on other sites More sharing options...
kenrbnsn Posted April 28, 2008 Share Posted April 28, 2008 You are reusing the variable $rate after you define it as an array. Use a different variable: <?php $rate = Array ( [0] => 6.99 [rate] => 6.99 [1] => 6.89033 [semi_annual_rate] => 6.89033 ); $rate1=$rate['rate']; $semi_annual_rate=$rate['semi_annual_rate']; echo $rate1 . '<br>'; echo $semi_annual_rate; ?> But, why use another variable at all when you can just reference the array index. <?php echo $rate['rate'] . '<br>'; echo $rate['semi_annual_rate']; ?> Ken Link to comment https://forums.phpfreaks.com/topic/103264-solved-losings-decimals-from-array/#findComment-528908 Share on other sites More sharing options...
janders Posted April 28, 2008 Author Share Posted April 28, 2008 Thanks everyone for your help! I've never used forms for help before but I like it already! It ended up being a stupid mistake pointed out by kenrbnsn where I was reusing the variable $rate. Thanks also to jonsjava for your input, what you suggested would work if I wasn't reusing that variable. Thanks again and have a good one! Link to comment https://forums.phpfreaks.com/topic/103264-solved-losings-decimals-from-array/#findComment-528925 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.