Jump to content

Remove items from one array to another without deleting pages


Tom.
 Share

Recommended Posts

Hey, 

I have a complex bit of code that is working with product variations. I loop through variations that need to be updated (when modifying the variations in any way). 

I store these in a PageArray() and then I need to compare it against a list of pages. In this instance ->not() would work great, however it expects a selector. It needs to be an $item.

What I'm trying to do is to remove a set of items from a list of other items using the API, using remove() or removeItems() deletes the repeater item, it doesn't remove the items from the array.

Link to comment
Share on other sites

I don't fully understand the issue, there is an example of buidling a PageArray, inserting pages, looking for a given page (item), removing it from the PageArray :

 

// example

$allpagesarray = new PageArray();
// loop to get all Page tree object and insert them in the PageArray for this example
foreach($pages->get(1)->children as $p) {
    $allpagesarray->add($p);
    if(count($p->children)) {
        foreach ($p->children as $cp) {
            $allpagesarray->add($cp);
        }
    }
}
echo "count allpagesarray: {$allpagesarray->count}<br>";

// new PageArray containing some Page object
$productpages = new PageArray();
foreach ($pages->get('/products/')->children as $prod) {
    $productpages->add($prod);
}

$firstprod = $productpages->first; // the Page object we are looking for in $allpagesarray
if($allpagesarray->has($firstprod)) {
    echo "page found (id:$firstprod->id), removing it from PageArray<br>";
    $allpagesarray->remove($firstprod);
    if($allpagesarray->not($firstprod)) echo "page (id:{$firstprod->id}) removed<br>";
}

// check if the Page still exist after removing it from the PageArray
if($allpagesarray->not($firstprod)) {
    echo "page not found (looking for id: {$firstprod->id})<br>";
}
else {
    echo "page still found (id:{$firstprod->id})<br>";
}
echo "count allpagesarray: {$allpagesarray->count}<br>";

 

940994755_Capturedecran2018-11-21a13_42_06.thumb.png.2d93d9b0d6913fda312d5cd4490f5030.png

Link to comment
Share on other sites

Hi @flydev

To give a better example. 

Array 1 (All Pages):

array(
	PageObject(1140),
	PageObject(1141),
	PageObject(1142),
	PageObject(1143)
)

Array 2 (Updated Pages):

array(
	PageObject(1140),
	PageObject(1141)
)

Return Array:

array(
	PageObject(1142),
	PageObject(1143)
)

Basically, I want to compare array 1 with array 2 and get rid of things that exist in the array. PageObject is just a page item and (1233) is the page ID. I just made this up for example but it will be a PageArray instance. 

Link to comment
Share on other sites

I guess the safest approach would be to create a new PageArray Array 3, iterate over Array 1, compare with each item in Array 2 and if not matched, add the Page to Array 3. Or, in code:

$array3 = new PageArray();

$array1->each(function($pg) use($array2) {
	if(! $array2->has($pg)) {
		$array3->add($pg);
	}
});

 

  • Like 2
Link to comment
Share on other sites

19 minutes ago, BitPoet said:

I guess the savest approach would be to create a new PageArray Array 3, iterate over Array 1, compare with each item in Array 2 and if not matched, add the Page to Array 3. Or, in code:


$array3 = new PageArray();

$array1->each(function($pg) use($array2) {
	if(! $array2->has($pg)) {
		$array3->add($pg);
	}
});

 

That's got it thank you. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...