DEV Community

Marcos
Marcos

Posted on • Edited on

3 2

Array en PHP

php con código de fondo

Declarar un array vacío


//Sintaxis con array() 
$lista_de_series=array();

//Sintaxis short array valida a partir de PHP 5.4
$lista_de_series=[];
Enter fullscreen mode Exit fullscreen mode

Declarar un array con elementos

//Sintaxis con array() 
$lista_de_series=array('El juego del calamar','Cobra kay','Love, Death and Robots','Sweet Tooth');

//Sintaxis short array
$lista_de_series=['El juego del calamar','Cobra kay','Love, Death and Robots','Sweet Tooth'];
Enter fullscreen mode Exit fullscreen mode

Ver ejemplo 1

Acceder a un elemento de un array simple


$indice=3;

echo $lista_de_series[$indice];

Enter fullscreen mode Exit fullscreen mode

Declarar un array asociativo con elementos

//Sintaxis con array() 
$jugador=array('nombre'=>'Lionel',
               'apellido'=>'Messi',
               'nacionalidad'=>'Argentina',
               'edad'=>34);

//Sintaxis short array
$jugador=['nombre'=>'Lionel',
               'apellido'=>'Messi',
               'nacionalidad'=>'Argentina',
               'edad'=>34];
Enter fullscreen mode Exit fullscreen mode

Acceder a un elemento de un array asociativo con claves

//Sintaxis con array() 
echo $jugador['nombre'];

Enter fullscreen mode Exit fullscreen mode

Ver ejemplo 2

Acceder a un elemento de un array asociativo con claves

//Sintaxis con array() 
echo $jugador['nombre'];

Enter fullscreen mode Exit fullscreen mode

Ver ejemplo 2

Agregar elementos a un array

PHP nos proporciona la funcion array_push() para agregar elementos a un array

Sintaxis: array_push($array, $elemento);

También es posible agregar mas de un elemento
array_push($array, $elemento1,$elemento2,$elemento3,$elemento4);


$lista_de_series=array('El juego del calamar','Cobra kay','Love, Death and Robots','Sweet Tooth');

array_push($lista_de_series,'La casa de papel');

array_push($lista_de_series,'Sweet Home','De yakuza a amo de casa');

FIN
DEL
POST

Enter fullscreen mode Exit fullscreen mode

Ver ejemplo 3

Imagine monitoring actually built for developers

Billboard image

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay