Insertar y extraer elementos de un array con PHP
En PHP para insertar o extraer elementos a un array lo podemos hacer de varias formas. En este artículo vamos a explicar como funcionan las funciones que nos facilita la librería: array_push() y array_pop().
La función array_push() inserta uno o más elementos al final de un array. Su estructura es la siguiente:
int array_push ( array &$array , mixed $var [, mixed $... ] )
La variable $array es el vector de entrada y $var el valor a insertar.
Array_push() trata array como si fuera una pila y empuja la variable que se le pasa al final del array. El tamaño del array será incrementado por el número de variables insertados.
Si se utiliza array_push() para añadir un solo elemento en el array, es mejor utilizar $array[] = ya que de esta forma no existe la sobrecarga de llamar a una función.
La función devuelve el nuevo número de elementos en un array.
La función array_pop() extrae el último elemento del final del array. Su estructura es la siguiente:
array_pop ( array &$array )
array_pop() extrae y devuelve el último valor del array, acortando el array con un elemento menos. Si el array está vacío (o no es un array), se devolverá NULL. Además se producirá un Warning cuando se llame a un elemento que no es un array.
La función devuelve el último valor del array. Si el array está vacío (o no es un array), se devolverá NULL.
Vamos a ver un ejemplo de aplicación:
<?php
//Array inicial
$data = array("Lunes", "Martes", "Miércoles", "Jueves");
//Añadimos los valores Viernes y Sábado al array
array_push($data, "Viernes", "Sábado");
//Eliminamos el último elemento del array
$last_element = array_pop($data);
?>
Muchas gracias por compartir tus conocimientos
Links…
[…]Sites of interest we have a link to[…]……
Enjoyed studying this, very good stuff, regards .
First off I want to say great blog! I had a quick question in which I’d like to ask if you don’t mind. I was interested to know how you center yourself and clear your mind prior to writing. I have had a difficult time clearing my thoughts in getting my thoughts out there. I do enjoy writing however it just seems like the first 10 to 15 minutes are generally lost just trying to figure out how to begin. Any recommendations or hints? Kudos!
Yay google is my queen assisted me to find this outstanding internet site ! . «A tough lesson in life that one has to learn is that not everybody wishes you well.» by Dan Rather.
This is my first time visit at here and i am in fact pleassant to read everthing at single place.
Actually no matter if someone doesn’t understand afterward its up to other users that they will help, so here it takes place.
Keep up the superb piece of work, I read few posts on this internet site and I think that your website is rattling interesting and holds circles of wonderful info .
That is very fascinating, You are a very skilled blogger. I have joined your rss feed and sit up for in search of more of your magnificent post. Additionally, I’ve shared your web site in my social networks!
Thank you!