masgas Posted April 16, 2008 Share Posted April 16, 2008 Hi all! I'm trying to do something and I don't get it right... The idea is to have a first array with 4 different values, and each time the user clicks a form button: connect with the db, select one new value, delete the first from the existing array and insert the new value in the array... I've tried this code but no good result //connect with DB //pull new value results in => $tipoAvo = $row['tipoAvo']; /*Pre declared array $avos[0]="A320"; $avos[1]="B739"; $avos[2]="A319"; $avos[3]="B737"; Pre declared array */ //code $sum=sizeof($avos); if($sum<=4){ array_push($avos, $tipoAvo); reset($avos); $firstinarray = current($avos); $avos=array_diff($avos, array($firstinarray)); while (list($index, $values)= each ($avos)){ echo "<br>".$index." - ".$values;} } but it prints the same array, only inserting in the last line the new value: $tipoAvo. I guess that having the array predeclared is producing this result... but I don't get any ideas of how to avoid this... I would like to add it and delete the first, and so on each time the user cliks the form... so I have a 4 valued array that changes constantly... Thank you for your help, and ideas... Quote Link to comment https://forums.phpfreaks.com/topic/101363-query-db-and-insert-in-array/ Share on other sites More sharing options...
ucffool Posted April 17, 2008 Share Posted April 17, 2008 Which value do you want removed? Using array_splice($array, offset [, length] [, replacement]) you can easily pull off an entry in an array and replace with some other value. Are you trying to remove the [0] and then put in a new value at the end of the array? Quote Link to comment https://forums.phpfreaks.com/topic/101363-query-db-and-insert-in-array/#findComment-519976 Share on other sites More sharing options...
masgas Posted April 20, 2008 Author Share Posted April 20, 2008 Hi thank you for your reply! I think that's exactly what I need... I will read about the array_splice function and try to get the solution... the idea is having an array of four values all the time, insert one in the end and delete the first one... alway pulling the values from a db... Quote Link to comment https://forums.phpfreaks.com/topic/101363-query-db-and-insert-in-array/#findComment-521924 Share on other sites More sharing options...
ucffool Posted April 20, 2008 Share Posted April 20, 2008 If it is always first and last, Then it is even simpler by doing: $array = ('one', 'two', 'three', 'four'); $firstvalue = array_shift($array); // Remove first entry in the array and return it, reindex array $array[] = 'newvalue'; /* The array would now look like: Array { [0] => two [1] => three [2] => four [3] => newvalue } */ Quote Link to comment https://forums.phpfreaks.com/topic/101363-query-db-and-insert-in-array/#findComment-521954 Share on other sites More sharing options...
masgas Posted April 20, 2008 Author Share Posted April 20, 2008 great! now the question is: wouldn't the array be again= $array = ('one', 'two', 'three', 'four'); next time I load the page? how could I keep it: Array { [0] => two [1] => three [2] => four [3] => newvalue } Quote Link to comment https://forums.phpfreaks.com/topic/101363-query-db-and-insert-in-array/#findComment-521992 Share on other sites More sharing options...
ucffool Posted April 20, 2008 Share Posted April 20, 2008 store the $array in a session, back in the database, pass it through GET... really depends on how you want to use it. Quote Link to comment https://forums.phpfreaks.com/topic/101363-query-db-and-insert-in-array/#findComment-522023 Share on other sites More sharing options...
masgas Posted April 20, 2008 Author Share Posted April 20, 2008 really good idea!! now I will think which is the best option to keep the array and give it a try! you've been of great help! thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/101363-query-db-and-insert-in-array/#findComment-522092 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.