1

I have to search array element values similar to mysql like.

Array is as follows.

$arraydata=
  array (0=> array('data'=>1),
  1=> array('data'=>'1|5'),
  2=>array('data'=>'2|3'),
  3=>array('data'=>'1|5|6'),
  4=>array('data'=>'1|5|6|7'),
  5=>array('data'=>'2|3|4'),
  6=>array('data'=>'2|3|4|8')
);

If want to search "1" in array element 'data' values then the output should be return element 0,1,3,4. If search "2" then output should be return element 2,5,6

5
  • 1
    It's about time you learned how to format your own questions. Please indent code by 4 spaces (or use the {} button to do it for you). stackoverflow.com/editing-help Commented Jan 13, 2011 at 7:00
  • don't you need to quote 1|5|6? Commented Jan 13, 2011 at 7:12
  • I think this is quoted and a string Commented Jan 13, 2011 at 7:13
  • Yes this is quoted string. I want to result without using regular expression. Commented Jan 13, 2011 at 7:16
  • Check the Savetheinternet's answer Commented Jan 13, 2011 at 7:16

1 Answer 1

1

There is an optional search parameter in array_keys.

$array = Array(0 => 1, 1 => 0, 2 => 1);
print_r(array_keys($array, 1));

But you appear to be using multi-dimensional arrays -- I'm not sure. You'd be better off using a foreach instead.

$results = Array();
foreach($arraydata as $key => $value) {
  if(strpos($value['data'],'1') !== false) {
    $results[] = $key;
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

array data element is like 1, 1|5, 1|5|6, 1|5|6|7 So the output should be 0,1,3,4 array element.
@Rahul: I am sure 'data' contain the string not the numeric values. recheck the answer if it works
Its working fine if we search 1 or 2 upto 9. But if we search 11 or 22 then its return empty result. suppose $array data contain extra 7=>'11|12|13',8=>'22|23|24|25'
bad idea, 1* or *1 is included,
we can search '1|' or '11|' or '22|' if require. Please suggest solution.
|

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.