Jump to content

Removing items from an array recursively


pandyboy

Recommended Posts

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")

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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));

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.