Php array String

3

Hi, I have this code in php to visualize the values of the array but I get an error of Array to string conversion and I do not know why.

Code:

<?php   
    echo "<h3>Diferentes formas de array</h3>";
    echo "Funcion array()";
    $colores = array(
                0=>'azul',
                1=>'rojo',
                2=>'verde');
    echo "Resultado de la funcion es: ".$colores;
    $colores2 = array("azul","rojo","verde");
    echo "Valor del array: ".$colores2;
?>
    
asked by Mario Guiber 26.09.2017 в 20:22
source

1 answer

2

The message says it all: You can not convert a array to a string .

To do this, you can use the implode

function
  

implode ()
string implode ( string $glue , array $pieces )
string implode ( array $pieces )   
  Join elements of an array in a string with glue (glue).

In your case:

<?php   
  echo "<h3>Diferentes formas de array</h3>";
  echo "Funcion array()";
  $colores = array(
               0=>'azul',
               1=>'rojo',
               2=>'verde');

  echo "Resultado de la funcion es: ".implode( $colores);
  $colores2 = array("azul","rojo","verde");
  echo "Valor del array: ".implode( $colores2 );
?>
    
answered by 26.09.2017 / 20:27
source