0

Here is my code:

<?php
$num = array("00000001","1","00001232","2","13454234","3");

$longnum = array();
$shortnum = array();

foreach($num as $key => $value)
{
    if($key % 2 == 0)
    {
        $longnum[] += $value;
    }
    else
    {
        $shortnum[] += $value;
    }
}

Expected output:

Long num: Array ( [0] => 00000001 [1] => 00001232 [2] => 13454234 )
Short num: Array ( [0] => 1 [1] => 2 [2] => 3 ) 

But my output:

Long num: Array ( [0] => 1 [1] => 1232 [2] => 13454234 ) 
Short num: Array ( [0] => 1 [1] => 2 [2] => 3 )

I want to get the long num same as the expected output. However, my output doesn't included the zero inside an array. How should I do?

5
  • 3
    Drop the += in here and change to = like this $longnum[] = $value; You are forcing PHP to typecast the string to a number through the math function. Commented Sep 20, 2014 at 3:54
  • use Type Casting value or it will be treated as int by intelligent guess $longnum[] = (string)$value; Commented Sep 20, 2014 at 3:56
  • Hi, Fluffeh. You solved my problem. Commented Sep 20, 2014 at 3:56
  • @Fluffeh why not put in the answer? Commented Sep 20, 2014 at 3:56
  • @Brian It seemed too easy hehe. Posted it witha little more explanation. Commented Sep 20, 2014 at 3:59

3 Answers 3

1

You should Type Cast value or it will be treated as int by intelligent guess.

$longnum[] = (string)$value;

Quote from manual:

PHP does not require (or support) explicit type definition in variable declaration; a variable's type is determined by the context in which the variable is used. That is to say, if a string value is assigned to variable $var, $var becomes a string. If an integer value is then assigned to $var, it becomes an integer.

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

Comments

1

The code you have in your question uses PHPs ability to typecast automatically. When you try to do math operations on strings, PHP is kind enough to try to convert them to numbers on your behalf.

By using a += operator, you are forcing PHP to do this as you append to an array.

foreach($num as $key => $value)
{
    if($key % 2 == 0)
    {
        $longnum[] = $value;
    }
    else
    {
        $shortnum[] = $value;
    }
}

By changing your code to this, you will get the expected results.

Additionally, by using [] you are automatically adding a new element to an array - so a += would actually be pointless - there is no value to add to - unless you were doing it specifically to force PHP typecasting to a number, in which case, there would be better ways to do it.

Comments

0

You should just assign(=) to array. Remove += instead. Try this

$num = array("00000001","1","00001232","2","13454234","3");

$longnum = array();
$shortnum = array();

foreach($num as $key => $value)
{
    if($key % 2 == 0)
    {
         $longnum[] = $value;
    }
    else
    {
        $shortnum[] = $value;
    }
}

Comments

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.