1

I have an array that looks like the below:

staff : staff,company
managers : managers,staff,company
executives : executives,staff,company
customers : customers,company
loyalty members : loyalty members,customers,company
non loyalty : non loyalty,customers,company

The array is made with the following command:

array_push($group_array, "$group : $area"); 

This array is created from the output of a command and i would like to remove the values staff and customers because for my purposes they are basically parents so i dont want them in my array.

I have been trying to find a way to essentially do this (not my actual php cod)e:

if array_value contains ",group," then remove it

To try and do this i have been trying to use strpos but i cant figure out how to compare the separate areas of my array values. This has only removed all items from my array:

if (strpos($area, ",$group,") !== false) {
    $group_array = array_diff($group_array, group_array(",$group,"));
 }

Additionally my array will not always match exactly the above but it will always follow the same layout e.g. group : group,parent,parent

I am using php in a html page, if i run this at the bottom of my page:


foreach($group_array as $items ){
    echo "$items <br>";
} 

My output is blank. I guess because my if is matching everything?

Expected (hoped for ) array would look like this but it is not as simple as remove item 0 and 3 because the array may change:


managers : managers,staff,company
executives : executives,staff,company

loyalty members : loyalty members,customers,company
non loyalty : non loyalty,customers,company

I know i have not worded this well and im expecting a torrent of abuse and downvotes but im trying my best to explain my task and goal.

7
  • Please share actual print_r of your input and expected output php array. Commented Nov 19, 2019 at 11:33
  • Instead of removing them from the final array would it be possible to not add them in the first place? A bit more code example would help! Commented Nov 19, 2019 at 11:36
  • the if is clearing my array of any values. im hoping for it to be my array without the string values starting with Staff and Customers Commented Nov 19, 2019 at 11:37
  • @ThomasGregory higher up in my code i am running a command that builds a much bigger array i am am picking out of that my $group and $area. I can remove it before this part of my code but the problem is the same. How to remove values that contain the string ",$group," Commented Nov 19, 2019 at 11:40
  • So, you want save items 1,2,4,5 ? Commented Nov 19, 2019 at 11:40

1 Answer 1

1

Here's one way to do this. Extract all the group names mentioned between commas using array_reduce and using preg_match_all to find them:

$groups = array_reduce($group_array, function ($c, $v) { 
    preg_match_all('/,(\w+),/', $v, $matches);
    return array_merge($c, $matches[1]);
}, array());

Since there may be duplicates we pass this array through array_unique and then create a regex to match each of the values:

$regex = '/^(' . implode('|', array_unique($groups)) . ')\s*:/';

Then use array_filter to process each value in $group_array, removing those whose value to the left of the : is in the $groups array:

$group_array = array_filter($group_array, function ($v) use ($groups) {
    return !preg_match($regex, $v);
});

Output:

Array
(
    [1] => managers : managers,staff,company
    [2] => executives : executives,staff,company
    [4] => loyalty members : loyalty members,customers,company
    [5] => non loyalty : non loyalty,customers,company
)

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

7 Comments

I get the impression you will know how to achieve what im aiming for but this is not quite it. Rather then remove "staff" from the managers and executive strings i want to remove the item staff and like wise the item customers.
@a.smith yeah, I've read all the comments since I posted the answer and I realised I misinterpreted your question. I'll edit...
@a.smith I think this new version will give you the results you actually want...
Thanks @nick i can see this working in your demo but in my code its still giving me the full array when i loop through it. I have even added the foreach loop that i have in my code to the demo. I am doing and array shift on it before your code because of a different issue with the array and that is fine, if i remove it the first item is put back into the array as expected. So the array is all there when im echoing it out to the page. I dont think your going to be able to help without seeing it all but its fairly long and pretty messy because it used ldap to create the array.
Ive updated your demo to give a better reflection of what im doing but it works in there just not in my live code
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.