PDA

View Full Version : Using sscanf, sprintf on arrays - trivial?


tatlar
01-13-2004, 01:24 PM
I could use some guidance here. I think I have been looking at it too long and can't see what I am doing wrong. I think it is VERY simple... :D

I have an array (called $r1) with a series of values. I print this array to the screen with the following PHP and count the number of values in the array:


reset($r1);
while (list($key, $val) = each($r1)) {
echo "$key => $val<br>\n";
}
echo count( $r1 ) ;


It gives a count of 100 - all well and good.
The first value of the array is 200.

Now I want to convert all the values in the array using either sscanf or sprintf (I still don't really understand the difference between them!) and put all these converted values into a new array with the following PHP:


foreach ( $r1 as $range ) {
$range = sprintf( "%0.3f", ( $range ) ) ;
echo "Value => $range<br>\n";
}


So all the values in the original array are now converted and in the array called $range. With the echo function I print them all out just fine to the screen. However, after this when I try to count $range I get an answer of 1!!!

Echoing out the first array value of $range gives a value of 2!!!

It seems to me that the complete array has been converted into one long string. I did not want this to happen and am assuming it is because I am using sprintf? Can anyone help me out or tell me what I am doing wrong?

Thanks!

tatlar
01-13-2004, 01:30 PM
Can someone move this to the PHP forum? I misposted it.

:!

illogique
01-13-2004, 01:45 PM
foreach ( $r1 as $range ) {
$range = sprintf( "%0.3f", ( $range ) ) ;
echo "Value => $range<br>\n";
}

should be


foreach ($r1 as $key => $range) {
$r1[$key]=sprintf("%0.3f",$range);
echo 'Value => ',$r1[$key],"<br>\n";
}

tatlar
01-13-2004, 01:54 PM
This does not change the result. I still get a count of 1 for the array.

Correct me if I am wrong but the code you wrote was just another way of doing the same thing I did.

Cheers.

tatlar
01-13-2004, 01:59 PM
Apologies illogique!

It did work after all.

Thanks very much!

:thumbup:

illogique
01-13-2004, 02:01 PM
you still have to count $r1 and not $range because $range will always be the last value of the array $r1...

$range is not an array, it's only a string!


edit:
sorry didn't see your last post

tatlar
01-14-2004, 11:10 AM
Okay - this worked when printing out the array to the screen, but what I actually want to do is use the new (value converted) array.

Like you noted - this does not create a new array with the converted values - if I print_r $range I get 1 value.

What I want is all the values of the array $r1 to be converted with the sprintf function and inserted into an array called $range.

Does this make sense?

Should I be using each() and not foreach()??

Thanks.

illogique
01-14-2004, 11:39 AM
foreach ($r1 as $temp) {
$range[] = sprintf("%0.3f",$temp) ;
echo 'Value => ',$temp,"<br>\n";
}


if you want to keep the same key for the new array


foreach ($r1 as $key => $temp) {
$range[$key] = sprintf("%0.3f",$temp);
echo 'Value => ',$temp,"<br>\n";
}

illogique
01-14-2004, 11:43 AM
i just found out that if you don't want to output each value, you could use array_map like this

<?php
function convert($n) {
return(sprintf("%0.3f",$n));
}
$range = array_map("convert", $r1);
?>

tatlar
01-14-2004, 11:48 AM
Sweet. I will check that out. Thanks for the prompt reply! I owe you.

:cheers: