2

I am trying to left rotate an array in PHP 2 times. It works correctly but there is a blank space in the array from some unknown reason. This is my code:

$a_temp = fgets($handle);
$a = explode(" ",$a_temp);
for($i = 0; $i < 2; $i++){
    print_r($a);
    array_unshift($a, array_pop($a));
    print_r($a);
}

The file has something like this:

1 2 3

Now the output I get is:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3

)
Array
(
    [0] => 3

    [1] => 1
    [2] => 2
)
Array
(
    [0] => 3

    [1] => 1
    [2] => 2
)
Array
(
    [0] => 2
    [1] => 3

    [2] => 1
)

As you can see, every time a rotate is performed, it introduces a blank space in it and during printing the array, it appears as a new line character. Any ideas what I am doing wrong?

6
  • 2
    Make sure your element is "3" and not, say, "3\n". (Notice how the first print_r has that extra space too, right before the closing paren. That hints the problem is in the input.) Commented Jan 26, 2017 at 15:11
  • Did you split a string contaning "123\n" into an array ? Commented Jan 26, 2017 at 15:12
  • 1
    I ran your code here and it worked fine. But I started with $a = [1, 2, 3];. Don't know what you started with because you did not include it in your question Commented Jan 26, 2017 at 15:12
  • 1
    Ran your code with $a = array(1,2,3); look fine with me no new lines. Commented Jan 26, 2017 at 15:14
  • Check it out yourself : eval.in/724856 works fine. Make sure you've got no stray newlines in your array. Commented Jan 26, 2017 at 15:21

5 Answers 5

3

As pointed out by @cHao ans @Kulvar, your "3" element is in fact "3\n" because your line returned by fgets ends with a \n which is normal.

Replace $a = explode(" ",$a_temp); with $a = explode(" ",trim($a_temp)); and you're fixed. Works with any string, numeric or not

Sign up to request clarification or add additional context in comments.

Comments

1

It seems you have a non-integer value in there. Use array_map() to cast all values to integers.

$a_temp = fgets($handle);
$a = explode(" ",$a_temp);
$a = array_map("intval", $a); //Cast all values to integers
for($i = 0; $i < 2; $i++){
    print_r($a);
    array_unshift($a, array_pop($a));
    print_r($a);
}

https://eval.in/724912

4 Comments

Yes casting fixed it! Thanks!
yes casting fixed it but that's not the correct answer. What if your file contains a b c instead of 1 2 3 ? This answer will not work.
No its guaranteed to be an int and there is one more answer that works on string as he trims it.
@KiJéy yep, good point. I'd recommend OP accept your answer instead if it's strings.
1

Combine the array_push() and array_shift() function. In this solution you would have to input a number of rotations, or you change the $k variable in the for-loop to your desired amount of rotations.

for($i=0; $i<$k; $i++){

    //remove first element
    $firstElement = array_shift($a);

    //push first element to the end
    array_push($a, $firstElement);

}

echo implode(" ", $a);

Comments

1

The first method is the optimised code. But other two are solution but not optimise as the first one.

function rotate_left_method_1( $a, $d ) {
    $first  = array_slice( $a, 0, $d );
    $second = array_slice( $a, $d, null );

    return array_merge( $second, $first );
}

function rotate_left_method_2( $a, $d ) {
    $swap = 1;
    foreach ( $a as $v ) {
        if ( $d >= $swap ) {
            array_shift( $a );
            array_push( $a, $v );
            $swap ++;
        }
    }
    return $a;
}

function rotate_left_method_3( $a, $d ) {
    if ( is_array( $a ) ) {
        for ( $swap = 0; $swap < $d; $swap ++ ) {
            $temp = $a[0];
            array_shift( $a );
            array_push( $a, $temp );
        }
    }

    return $a;
}




var_dump( rotate_left_method_1( array( 1, 2, 3, 4, 5 ), 4 ) );
var_dump( rotate_left_method_2( array( 1, 2, 3, 4, 5 ), 4 ) );
var_dump( rotate_left_method_3( array( 1, 2, 3, 4, 5 ), 4 ) );

Comments

0
function rotateLeft($d, $arr) {
    $remaining = array_slice($arr, $d);
    array_splice($arr, $d);
    return array_merge($remaining,$arr);
}
$d =4; $arr = [1,2,3,4,5];
$result = rotateLeft($d, $arr);
var_dump($result);
output [5, 1, 2, 3, 4]

2 Comments

Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you’ve made.
array_shift() modifies the original array and can be pretty slow because it needs to completely reindex the array

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.