Jump to content

[SOLVED] For loop not exacly working. PLEASE HELP


Paldo

Recommended Posts

Hi guys!

I have this for loop that should store individual numbers from a string to an array.

Instead of that it stores only the first value to all array components.

Please have a look I'm sure you will see what's wrong...  thanks

<html>
<body>

<?php

$cislovsringu = $_POST[cislo];

echo $cislovsringu;

echo '<br>';

echo $cislovsringu{3}; 
echo $cislovsringu{1}; 
echo $cislovsringu{4}; 

echo '<br>';

$dlzka = strlen($cislovsringu);

echo $dlzka;
echo '<br>';
echo '<br>';

$poleHodnot = array( "1" => "jednotky" , "2" => "desiatky" , "3" => "stovky" , "4" => "tisicky" , "5" => "desatisicky" , "6" => "stotisicky" , "7" => "miliony" , "8" => "desatmiliony" , "9" => "stomiliony" , "10" => "miliardy" );

$limit = $dlzka + 1;

for ($i = $dlzka ; $i > -1; $i-- ) {

$hlp = 0;
$hlp2 = 0;
$j = 0;
$p  = 0;
$hlp = $i - $dlzka;
$hlp2 = $hlp * (-1);
$j = hlp2;
$p = $cislovsringu[$j];
$polePoctu[$poleHodnot[$i]] =  (int)$p ;

}

echo 
print_r($polePoctu);

//1 364 720 434

?>

</body>
</html>

Link to comment
Share on other sites

Far too ambiguous. What data does $cislovsringu hold? I know it's a string, but can I get an example string? Can you provide an example of what it should do and what it's doing now?

 

$cislovsringu = $_POST[cislo];

Put quotes around cislo. Nothing's wrong with that except PHP throws notices about it.

 

$help = 0;
$hlp2 = 0;
$j = 0;
$p  = 0;

You don't need those. Just remove them.

Link to comment
Share on other sites

I'll do more than that.

I transleted all the variable names to english + I deleted some stuf that is more like the developing help for me.

<html>
<body>
<?php

$numberInStringFormat = $_POST[number];

echo '<br>';
echo '<br>';

$lenght = strlen($numberInStringFormat);

$ArrayForValueAssigment = array( "1" => "ones" , "2" => "tens" , "3" => "houndrets" , "4" => "tousends" , "5" => "tentousends" , "6" => "houndrettousends" , "7" => "milions" , "8" => "tenmilions" , "9" => "houndretmilions" , "10" => "bilions" );

for ($i = $lenght ; $i > -1; $i-- ) {


$p = $numberInStringFormat[$i];
echo $p;
$ArrayForKeepingHowMuchOfEachValues[$ArrayForValueAssigment[$i]] =  (int)$p ;

}

print_r($ArrayForKeepingHowMuchOfEachValues);

?>
</body>
</html>

 

Imput is for example: 33456

The output should be : Array ( [tentousends] => 3 [tousends] => 3 [houndrets] => 4 [tens] => 5 [ones] => 6 )

But the output is: Array ( [tentousends] => 0 [tousends] => 3 [houndrets] => 3 [tens] => 4 [ones] => 5[] => 6 )

Link to comment
Share on other sites

$_POST is an array. You should use $_POST['number'] instead of $_POST[number]. If you put

error_reporting(E_ALL);

at the top, I'm sure it'll spit out some notices about number not define as a constant but PHP assumes you meant 'number'. Still notices aren't good.

 

In your for loop, change $i = $lenght to $i = $lenght - 1. That should do it.

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.