Jump to content

EDIT AN ARRAY-


scripterdx

Recommended Posts

<?php 
include('config.php');
//agarra productos a la lista.

$usuario=$_GET['id'];
$producto=$_GET['prod'];
$sql= "SELECT lista FROM usuarios WHERE id=$usuario";
$consulta = mysql_query($sql);
$row = mysql_fetch_assoc($consulta);

if($lista = $row['lista']){
  $lista = explode(",",$lista);
} else {
  $lista = array();
}

if(!in_array($producto,$lista)){
$lista[] = $producto;
$lista = implode(",",$lista);
mysql_query("UPDATE usuarios SET lista = '$lista' WHERE id=$usuario");
echo "Producto agregado";
}else{
echo "Ya existe";
}
?>

 

what this code does is to add items to an array, works pefectly, now i´m wondering un a way to limit the amount of items on the array and to add the posibility to eliminate a "item" from the array

 

regards

Link to comment
https://forums.phpfreaks.com/topic/182030-edit-an-array/
Share on other sites

What do yo umean unlimit the amount of items on the array? How are they limited?

 

you can remove elements from an array like so

<?php
$arr = array();

for ($i = 0; $i < 10; $i++){
$arr[] = $i;
}

$key = array_keys($arr, 3);//gets an array of the keys with the value of 0
unset($arr[$key[0]]);//this will unset the first entry in the array with the value we searched for
print_r($arr);
?>

 

This outputs

Array ( [0] => 0 [1] => 1 [2] => 2 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 )

Link to comment
https://forums.phpfreaks.com/topic/182030-edit-an-array/#findComment-960144
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.