0

Need help trying to remove a array value (Customer ID) from a attribute array value in a collection

 //unfollowAction //Get Model to save data to. $model = Mage::getModel("userprofiles/userprofiles"); $model->getCollection()->addFieldToFilter('userprofiles_id', $id); $model->setUserprofilesId($id); //Get Model to fetch data from $model2 = Mage::getModel("userprofiles/userprofiles")->load($id); //Build Array output is 123,234,345,456, $FollowProfilesArray = array($model2->getFollowProfiles()); //Remove specified ID from array, $profile_id equals current customer ID $remove_from_array = array_diff($FollowProfilesArray, array($profile_id,)); foreach($remove_from_array as $key => $value){ $select .= ''.$value.','; } //save new array to attribute value with the specified ID removed. $model->setFollowProfiles($select); 

Problem is that this code is not removing the specified ID and saving the attribute value as 123,234,345,,

Note:

$model2->getFollowProfiles() output/saved value is 123,234,345,456 etc

the code $FollowProfilesArray = array($model2->getFollowProfiles()); does not work as stated above but if it hardcode the array data to

//Works,but not ideal or relevant $FollowProfilesArray = array(123,234,345,456,); 

So is there something im missing like exploding or imploding the $FollowProfilesArray = array($model2->getFollowProfiles()); code

1 Answer 1

1

Ok sorted it out yeah. Code below.


changed

$FollowProfilesArray = array($model2->getFollowProfiles()); 

to

//explode out the attribute value $FollowProfilesArray = explode(",","".$model2->getFollowProfiles().""); 

and changed

foreach($remove_from_array as $key => $value){ $select .= ''.$value.',';} 

to

 //so if $value is NULL save no value id, stoppped the adding of , to the attribute value foreach($remove_from_array as $key => $value){ if($value == NULL) { //$select .= ''.$value.','; }else{ $select .= ''.$value.','; } } 

so now on call to unfollowAction the specified ID is removed from the attribute value but keeps any other ID values.

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.