Convert Array to String

3

Why when I convert the array to a string it does not convert all the keys in the array right:

Code:

<?php

        $bMsg3 = array(
            $at1 = 0,   /* Inteiro */
            $at2 = "",  /* String */
            $at3 = 0.0,  /* Float */
            $at4 = 0  /* Float */
        );

        $bMsg3[$at1] = 400;
        $bMsg3[$at2] = "Hello World!";
        $bMsg3[$at3] = 3.14;
        $bMsg3[$at4] = 200.0;


        // Converte para string
        $str = implode(':', $bMsg3);

        print "String: $str";

        ?>

The result is: String: 200 :: 0: 0: Hello World!

In what should be: 400: Hello World!: 3.14: 200.0

Does anyone know why?

    
asked by anonymous 28.07.2015 / 14:00

3 answers

2

The result is not expected because you are making multiple assignments in the same key.

Basing all variables has a value of zero, except $at2 . In assignments you pass the value of zero practice every time except $at2

    $bMsg3 = array($at1 = 0, $at2 = "", $at3 = 0.0, $at4 = 0);


    $bMsg3[$at1] = 400;
    $bMsg3[$at2] = "Hello World!";
    $bMsg3[$at3] = 3.14;
    $bMsg3[$at4] = 200.0;

    //As atribuições acima são equivalentes a:
    $bMsg3[0] = 400;
    $bMsg3[""] = "Hello World!";
    $bMsg3[0] = 3.14;
    $bMsg3[0] = 200.0;

Step-by-step example - ideone

About defining keys / indexes and curious results

The manual describes the behavior of how to set the keys of an array:

  • Keys must be strings or integers only.
  • Strings that contain integer values will be converted to integer SINCE they are valid. The manual suggests the example that "8" (string) will be converted to 8 (int) and 08 (int) not because it is not a valid decimal. It is not evident that numbers beginning with zero are octal, if valid represent another decimal value. If you need to keep the leading zero in some index number the way to ensure this is to put the value in quotation marks as a string.

Maybe that's why programmers confuse both Halloween and Christmas:).

Ex: 1

$arr = array(031 => 'dezembro');
Saída: 
Array ( [25] => dezembro ) 

Ex: 2

$arr = array('031' => 'dezembro');
Saída:
Array( [031] => dezembro )
  • Floats are converted to integers, meaning that only the whole part of the number will be converted to a key.

  • Booleans are converted to integers 0 to false and 1 to true.

  • Nulls are converted to an empty string.

  • Arrays and objects can not be used as keys, this will throw a warning: Illegal offset type.

  • If multiple elements use the same key, only the last value will be considered as the others are overwritten.

Example about items 3 and 7.

$arr = [0.1 => 'a', 0.2 =>'b', 0.3 =>'c'];
echo '<pre>';
print_r($arr);

Output:

Array
(
    [0] => c
)

Manual - arrays

    
28.07.2015 / 14:25
4

Do this =

    $bMsg3[] = 400;
    $bMsg3[] = "Hello World!";
    $bMsg3[] = 3.14;
    $bMsg3[] = 200.0; 

    // Converte para string
    $str = implode(':', $bMsg3);

    print "String: $str";
    ?>

Result = String: 400: Hello World!: 3.14: 200

    
28.07.2015 / 14:12
3

In php, the implode function does not work for keys, but only for array values.

Example:

$array = ['site' => 'stackoverlow', 'linguagem' => 'portugues']

implode(',', $array); // string: stackoverflow, portugues

How do you assign an array or group variables?

Now, the first part of your code seems to have an error, since you can not assign it like this:

$bMsg3 = array(
            $at1 = 0,   /* Inteiro */
            $at2 = "",  /* String */
            $at3 = 0.0,  /* Float */
            $at4 = 0  /* Float */
        );

But only this way (with => ):

$bMsg3 = array(
            $at1 => 0,   /* Inteiro */
            $at2 => "",  /* String */
            $at3 => 0.0,  /* Float */
            $at4 => 0  /* Float */
        );

Unless you wanted to make an assignment to the values of the bulk keys. Here it should look like this:

 $at1 = 0;
 $at2 = "";
 $at3 = 0.0;
 $at4 = 0;

Or so:

list($at1, $at2, $at3, $at4) = array(0, "", 0.0, 0);

Important note!

In PHP, the accepted values for indexes of arrays are just integers and strings. Values like float are not accepted.

I saw this in the ZEND test:

$array[1.0] = 1;
$array[1.1] = 1.1;

count($array); // retorna 1

Check out IDEONE

What can be done is to create a representation for the value float through a string.

So:

$array['0.0'] = 0;
    
28.07.2015 / 14:04