1

I'm trying to translate a javascript function into php but having some problems with my arrays. I need to iterate over the array elements, multiplying them all by a certain amount, but it's not changing the values. Pretty sure it's because my syntax $coordinates_p[i][0] *= $scale; isn't correct, but I'm not sure what it should be!

Test code:

<?php

print "Starting.<br/>";

$scale = 100;

$coordinates_p = array();

$i = 0;
$x_coordinate = 1;
$y_coordinate = 2;
while ($i <= 1) {
    $coordinates_p[$i] = array(0 => $x_coordinate, 1 => $y_coordinate);
    $x_coordinate += 1;
    $y_coordinate += 2;
    $i++;
}

print "Unscaled: ";
print_r ($coordinates_p);
print "<br/>";

$i = 0;
while (isset($coordinates_p[i])) {
    $coordinates_p[i][0] *= $scale;
    $coordinates_p[i][1] *= $scale;
    $i++;
}

print "Scaled: ";
print_r ($coordinates_p);
print "<br/>";

print "Finished.";

?>
0

3 Answers 3

6

Your code just needs to change from

$coordinates_p[i][0] *= $scale;
$coordinates_p[i][1] *= $scale;

to

$coordinates_p[$i][0] *= $scale;
$coordinates_p[$i][1] *= $scale;
Sign up to request clarification or add additional context in comments.

3 Comments

Keep on missing those! But unfortunately it still doesn't work.
@Mike then you also missed the one in the isset above those two. Always enable error_reporting(-1) on development servers and make sure you set your display_errors directive in php.ini and PHP will tell you about these things.
My bad, there was another one I'd missed. Working fine now!
1

Your error is in

while (isset($coordinates_p[i])) {
    $coordinates_p[i][0] *= $scale;
    $coordinates_p[i][1] *= $scale;
    $i++;
}

it should use $i not i.

like so:

while (isset($coordinates_p[$i])) {
    $coordinates_p[$i][0] *= $scale;
    $coordinates_p[$i][1] *= $scale;
    $i++;
}

2 Comments

Yes, thanks. That will teach me to cut and paste from javascript!
jon_darkstar's answer here is also a good note - I was just fixing your code. Using the foreach approach would be "better" php. Even a for loop (bounded) would be a better approach than a potentially infinite while loop.
1

Depends on how "deeply" you want to translate

Shallow - put a $ in front of every variable

Deeper - put $ in front of variables, change those while loops to foreach, change print to echo

//before
$i = 0;
while (isset($coordinates_p[i])) {
    $coordinates_p[i][0] *= $scale;
    $coordinates_p[i][1] *= $scale;
    $i++;
}

//Better PHP form
foreach($coordinates_p as $current)
{
   $current[0] *= $scale;
   $current[1] *= $scale;
}

They'll each run, but you're not really USING php if you do those while loops. For a more extreme example, post code with lots of while loops up with a "python" tag and ask if it can be simplified.

foreach loops and echo are idiomatic php, while loops and print only works.

1 Comment

Ok, thanks. Never really learnt any programming language properly, just dabble around and try and get the end result I need. Would probably be worth investing some time in it though, I seem to be doing more and more recently. Thanks for the tip!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.