scripterdx Posted November 18, 2009 Share Posted November 18, 2009 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/182030-edit-an-array/ Share on other sites More sharing options...
mikesta707 Posted November 18, 2009 Share Posted November 18, 2009 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/182030-edit-an-array/#findComment-960144 Share on other sites More sharing options...
scripterdx Posted November 18, 2009 Author Share Posted November 18, 2009 i want to limit the amount of keys on the array, let´s say 10. so when i reach that amount wont let me add more items Quote Link to comment https://forums.phpfreaks.com/topic/182030-edit-an-array/#findComment-960157 Share on other sites More sharing options...
Daniel0 Posted November 18, 2009 Share Posted November 18, 2009 Just check the length and don't add more if the length is 10. Quote Link to comment https://forums.phpfreaks.com/topic/182030-edit-an-array/#findComment-960158 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.