pandyboy Posted July 7, 2010 Share Posted July 7, 2010 Hi I shall try and explain what I want to do.... I want an array of things - for example ("dog","collar","lead","cat","mouse","trap") "collar" requires "dog", "lead" requires "collar", "trap" requires "mouse" If I have a second array which is ("collar", "lead", "cat", "trap") I want to be able to check this array against the first and remove all elements that can't exist without another (so "collar" will be removed because there is no "dog" and "trap" will be removed because there is no "mouse") however, in this situation, because "collar" will be removed, "lead" should also then be removed. I can of course do this by looping through in order and rechecking the array each time, but if the array is not in a logical order then this method won't work reliably. Any suggestions how I could do this recursively? In brief: Main Array=("dog","collar","lead","cat","mouse","trap") 2nd Array=("collar","lead","cat","trap") Final array should be ("cat") Quote Link to comment https://forums.phpfreaks.com/topic/207004-removing-items-from-an-array-recursively/ Share on other sites More sharing options...
bh Posted July 7, 2010 Share Posted July 7, 2010 Hi, array_diff(2ndarray, mainarray) Quote Link to comment https://forums.phpfreaks.com/topic/207004-removing-items-from-an-array-recursively/#findComment-1082420 Share on other sites More sharing options...
pandyboy Posted July 7, 2010 Author Share Posted July 7, 2010 No - I don't want the difference between the two. I need a way of identifying the "required" items and then removing the items which don't have the items they require in the array. So because "collar" requires "dog" and "lead" requires "collar", if "dog" is not present, "collar" should be removed and then as a result so should "lead", even though "lead" does not directly require "dog" Quote Link to comment https://forums.phpfreaks.com/topic/207004-removing-items-from-an-array-recursively/#findComment-1082427 Share on other sites More sharing options...
sasa Posted July 7, 2010 Share Posted July 7, 2010 in your 1st example result is cat but without lead whay cat is not removed ? Quote Link to comment https://forums.phpfreaks.com/topic/207004-removing-items-from-an-array-recursively/#findComment-1082434 Share on other sites More sharing options...
pandyboy Posted July 7, 2010 Author Share Posted July 7, 2010 OK - I have written the code which does what I want if things are in logical order (i.e. dog comes before collar, before lead) as follows: <?php $allThings=array("dog","collar","lead","cat","mouse","trap"); $requiredThings=array("collar"=>"dog","lead"=>"collar","trap"=>"mouse"); $myThings=array("collar","lead","cat","trap"); foreach ($myThings as $myKey=>$myValue) { if ($requiredThings[$myValue]) { if (!in_array($requiredThings[$myValue],$myThings)) { echo $myValue." should be removed because there's no ".$requiredThings[$myValue]."<br>"; unset($myThings[$myKey]); } } } print_r($myThings); ?> This outputs: collar should be removed because there's no dog lead should be removed because there's no collar trap should be removed because there's no mouse Array ( [2] => cat ) Which is correct - however if I jumble up the arrays into a random order as follows: $myThings=array("lead","trap","collar","cat"); I get trap should be removed because there's no mouse collar should be removed because there's no dog Array ( [0] => lead [3] => cat ) But as you see "lead" is still there because it comes before "collar" in the array. I need a way to do this regardless of the order of the $myThings array. Quote Link to comment https://forums.phpfreaks.com/topic/207004-removing-items-from-an-array-recursively/#findComment-1082440 Share on other sites More sharing options...
pandyboy Posted July 7, 2010 Author Share Posted July 7, 2010 Solved it!! I created a function which calls itself until all elements which need to be removed have been... $allThings=array("dog","collar","lead","cat","mouse","trap"); $requiredThings=array("collar"=>"dog","lead"=>"collar","trap"=>"mouse"); $myThings=array("lead","trap","collar","cat"); function checkRequired ($allThings,$requiredThings,$myThings) { $count=count($myThings); foreach ($myThings as $myKey=>$myValue) { if ($requiredThings[$myValue]) { if (!in_array($requiredThings[$myValue],$myThings)) { echo $myValue." should be removed because there's no ".$requiredThings[$myValue]."<br>"; unset($myThings[$myKey]); } } } if($count!=count($myThings)) { $myThings=checkRequired($allThings,$requiredThings,$myThings); } return $myThings; } print_r(checkRequired($allThings,$requiredThings,$myThings)); Quote Link to comment https://forums.phpfreaks.com/topic/207004-removing-items-from-an-array-recursively/#findComment-1082472 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.