TheStalker Posted August 10, 2009 Share Posted August 10, 2009 Hi all, I have two dynamic vars as below $var1 = "car1"; $var = "car4"; I also have any array of cars 1-5 $cars=array("car1","car2","car3","car4","car5"); What i want to be able to do is echo out in a list the cars that are not in my vars So from the example i gave i would like to echo car2 car3 car5 Is there an easy way this can be done ? Ive tried a few dif ways but with no luck so far. Like i said the vars could change so could be any of the cars. any helpwould be graet thanks Quote Link to comment https://forums.phpfreaks.com/topic/169592-easy-way-to-echo-part-of-an-array/ Share on other sites More sharing options...
KevinM1 Posted August 10, 2009 Share Posted August 10, 2009 Hi all, I have two dynamic vars as below $var1 = "car1"; $var = "car4"; I also have any array of cars 1-5 $cars=array("car1","car2","car3","car4","car5"); What i want to be able to do is echo out in a list the cars that are not in my vars So from the example i gave i would like to echo car2 car3 car5 Is there an easy way this can be done ? Ive tried a few dif ways but with no luck so far. Like i said the vars could change so could be any of the cars. any helpwould be graet thanks Try: for($i = 0; $i < count($cars); ++$i) { if($cars[$i] != $var || $cars[$i] != $var1) { echo $cars[$i]; } } Quote Link to comment https://forums.phpfreaks.com/topic/169592-easy-way-to-echo-part-of-an-array/#findComment-894723 Share on other sites More sharing options...
sasa Posted August 10, 2009 Share Posted August 10, 2009 <?php $var1 = "car1"; $var = "car4"; $cars=array("car1","car2","car3","car4","car5"); $out = array_diff($cars, array($var, $var1)); print_r($out); ?> Quote Link to comment https://forums.phpfreaks.com/topic/169592-easy-way-to-echo-part-of-an-array/#findComment-894859 Share on other sites More sharing options...
TheStalker Posted August 10, 2009 Author Share Posted August 10, 2009 Hi all, I have two dynamic vars as below $var1 = "car1"; $var = "car4"; I also have any array of cars 1-5 $cars=array("car1","car2","car3","car4","car5"); What i want to be able to do is echo out in a list the cars that are not in my vars So from the example i gave i would like to echo car2 car3 car5 Is there an easy way this can be done ? Ive tried a few dif ways but with no luck so far. Like i said the vars could change so could be any of the cars. any helpwould be graet thanks Try: for($i = 0; $i < count($cars); ++$i) { if($cars[$i] != $var || $cars[$i] != $var1) { echo $cars[$i]; } } hi this seems to just echo all 5 cars Quote Link to comment https://forums.phpfreaks.com/topic/169592-easy-way-to-echo-part-of-an-array/#findComment-894918 Share on other sites More sharing options...
TheStalker Posted August 10, 2009 Author Share Posted August 10, 2009 <?php $var1 = "car1"; $var = "car4"; $cars=array("car1","car2","car3","car4","car5"); $out = array_diff($cars, array($var, $var1)); print_r($out); ?> this giveme this result Array ( [1] => car2 [2] => car3 [4] => car5 ) Quote Link to comment https://forums.phpfreaks.com/topic/169592-easy-way-to-echo-part-of-an-array/#findComment-894919 Share on other sites More sharing options...
KevinM1 Posted August 10, 2009 Share Posted August 10, 2009 The following works: <?php $cars = array("car1", "car2", "car3", "car4", "car5"); $firstTest = "car2"; $secondTest = "car5"; for($i = 0; $i < count($cars); ++$i) { if ($cars[$i] != $firstTest && $cars[$i] != $secondTest) { echo $cars[$i]; } } ?> It outputs: car1car3car4 Quote Link to comment https://forums.phpfreaks.com/topic/169592-easy-way-to-echo-part-of-an-array/#findComment-894931 Share on other sites More sharing options...
TheStalker Posted August 10, 2009 Author Share Posted August 10, 2009 ok thanks for that, now what im trying do is do the same thing from a query on a database. Im having a bit of trouble of working out howto get the cars into the vars tho i know i have to loop round but cant work out how it should be done my db might look somthing like this: date -car 30/01/2010 - car1 27/03/2010 -car3 27/03/2010 - car4 04/05/2010 - car1 06/06/2010 -car5 06/06/2010 - car4 $sql = mysql_query("SELECT * FROM bookings WHERE datebooking ='2010-03-27' "); $i=0; while($row=mysql_fetch_array($sql)) { $i++; $car.$i == $row['car']; //echo $row['car']; } Quote Link to comment https://forums.phpfreaks.com/topic/169592-easy-way-to-echo-part-of-an-array/#findComment-894961 Share on other sites More sharing options...
Catfish Posted August 10, 2009 Share Posted August 10, 2009 Use the vars that hold the car names to improve upon the MYSQL query to filter out the database results you don't want. $var1 = 'car1'; $var2 = 'car2'; $query = "SELECT * FROM bookings WHERE datebooking ='2010-03-27' AND carName != '".$var1."' AND carName != '".$var2"'"; $sql = mysql_query($query); For this solution, it would be better to stored the filtered car names in an array so a foreach loop can be used to add the exceptions to the MYSQL query. That way your code will dynamically generate the query string no matter how many or little cars are being filtered. Quote Link to comment https://forums.phpfreaks.com/topic/169592-easy-way-to-echo-part-of-an-array/#findComment-895002 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.