0

this is my array

Array (

    [0] => administrators (Members can fully administer the computer/domain)
    [1] =>  SID: S-1-5-32-544
    [2] =>  No members
    [3] => backup operators (Members can bypass file security to back up files)
    [4] =>  SID: S-1-5-32-551
    [5] =>  No members
    [6] => power users (Members can share directories)
    [7] =>  SID: S-1-5-32-547
    [8] =>  No members
    [9] => john (qwerty)
    [10] =>     SID: S-1-5-21-418404614-1514096366-3962945920-2147483748
    [11] =>     Members:
    [12] =>         \vijay
    [13] => bishop (abcd)
    [14] =>     SID: S-1-5-21-418404614-1514096366-3962945920-2147483748
    [15] =>     Members:
    [16] =>         \ram
    [17] =>         \vijay
)

now i want to search from this array with name vijay using any preg function in php it will search vijay in this array but my problem is i need to search vijay and i want output like where ever vijay is i need that group name in one array,like below vijay is available in group1 and in group2 (array[9] nd array[13]) so i need output like below using regular expression

Array (

     [0] => john
     [1] => bishop
)

thnku in advance if u did nt get any thing from question plz comment i will clear u

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Aj Kosh
  • 47
  • 9
  • FYI, the tag for regexes in general is [tag:regex], not [tag:nsregularexpression]. But I don't see how this is a regex question. – Alan Moore May 20 '14 at 21:17
  • with regular expression u have exact word search nd cut nd stor in new array – Aj Kosh May 23 '14 at 09:54

2 Answers2

1

To do this:

foreach ($your_array as $item){
  if (strpos($item, 'group') !== FALSE){
     echo $item;
  }
  if (strpos($item, 'vijay' !== FALSE){
     echo $item;
  }
}

Here's a simple solution, I cannot test it right now, but it is an idea, about the preg_match, you can use it if you want to send the result to an array to manipulate later in your code,

Otherwise, if you only need to display in a quick and dirty way, you can use strpos which will give the position of the substring in your string (an item in the example above)

Hope this helps you,

PS: This is only an example, there are multiple ways to get it, it is one of easiest ways ...

EDIT:

So in this case:

$groupName = "";
foreach ($your_array as $item){
  if (strpos($item, '(') !== FALSE && strpos($item, ')') !== FALSE){
     $groupName = $item;
  }
  if (strpos($item, 'vijay' !== FALSE){
     echo "This user '$item' belongs to the group '$groupName'";
     //Normally this structure keeps the same you don't need the next line:
     $groupName = ""; //optional
  }
}

Regards,

user2983848
  • 33
  • 1
  • 7
  • sry my mistake i did nt mention group1 nd group2 are assuming group name ,it can be any name,so i edited question ,so will get idea – Aj Kosh May 19 '14 at 12:41
0

The easiest way I think is to make a foreach on the array, then use on each index the function pref_match:

Documentation: http://www.php.net/manual/fr/function.preg-match.php

Hope this help you,

Kind regards,

EDIT: I also found this link which can help you: How to search in an array with preg_match?

Community
  • 1
  • 1
user2983848
  • 33
  • 1
  • 7