4

I have an array like below

$array =array(
 "123,456,789"=> "1,1,1",
 "333"=>"1",
 "777"=>"1"
)

Now if I am searching 456 then need to return me array key(123,456,789) and its value (1,1,1)

I also tried like below to make it working but i didn't get success.I need like if i am searching with any value of array key like (123 or 456 or 789) then need to provide me same result. I know that i can achieve this using foreach loop but i don't want to create foreach loop for this so suggest me if any other solution exists.

$matching_key = preg_grep("/\b456\b/", $array);

Sorry but I am not much good in regular expression.

Ideas? Any suggestions?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Ankit Doshi
  • 1,164
  • 3
  • 21
  • 43
  • Related: [Search for part of an key in array - PHP](https://stackoverflow.com/q/15139259/8958173) – joeljpa Jun 06 '23 at 05:53

2 Answers2

4

preg_grep function works on array's values, not keys as it is what you need.

You could get keys in first place.

<?php

$array =array(
   "123,456,789"=> "1,1,1",
   "333"=>"1",
   "777"=>"1"
);

$keys = array_keys($array);
$matching_key = preg_grep("/\b456\b/", $keys);

var_dump($matching_key);
//returns "123,456,789"

https://3v4l.org/SndkV

Anyway I need to say that you probably should change your data structure. This looks like a bad design.

Jakub Matczak
  • 15,341
  • 5
  • 46
  • 64
  • Thanks for reply.It works but now issue coming like it provide me result like below array( [35] => 123,456,789 ); But its not give me only 123,456,789 – Ankit Doshi May 29 '17 at 10:21
  • That's because there could be multiple results. If you're sure there will be only one element returned, you could use `current($matching_key)` to get this element's value. Also you should validate if there is any result in first place. – Jakub Matczak May 29 '17 at 10:24
  • Thanks i got it.But can you please help some more like i want that element value need to be changed like this way.456 is matched and its 2nd element then i am sure this key value 1,1,1 (2nd element having value is 1) and want to change it to 0 and rest of the same so new array like array(123,456,789)=>1,0,1 – Ankit Doshi May 29 '17 at 10:36
  • That's why I said you should change your data structure. It's hard to do any operations now. It is possible, but won't be easy. – Jakub Matczak May 29 '17 at 10:47
  • I know your idea is really good and i accept that data structure is not much good but actually it's very long web app which has been already developed now its very difficult for me to change this logic because it will take very large affect so how can i do this ? Will you guide me ? – Ankit Doshi May 29 '17 at 10:50
  • Use `explode` for key and value, then `array_combine`, then change the value of target key, and then implode modified values back and put into original array under original key. – Jakub Matczak May 29 '17 at 10:51
0

Here is a try

$array =array(
   "123,456,789"=> "1,1,1",
   "333"=>"1",
   "777"=>"1"
);

$search = '456';
$result = [];

foreach($array as $key=>$value){

  if (strpos($key, $search) !== false) {
    array_push($result, $value);
  }

}

Print_r($result);

$search is a dynamic Variable which you want to search, i used str_pos(), to find if the string is there in that key.

Reiah Paul Sam
  • 555
  • 6
  • 16
  • @Ankit Doshi try the above example , if works for your scenario replace the foreach with array_walk() function – Reiah Paul Sam May 29 '17 at 10:17
  • This will also match on "12,3456,789". Any non-regex solution that doesn't explode on commas will give incorrect results. – mickmackusa May 31 '17 at 02:15
  • 1
    @mickmackusa I understand. The problem is not that. They will even search for `3,4`, or `456,7`, whatever combination in the key, that is the reason i went for `strpos` instead of `foreach` and regular expression., I don't know why they down voted my answer., When there is a inbuilt method why to go for `regex` or `foreach`. – Reiah Paul Sam May 31 '17 at 04:57
  • I don't think you do understand. Your answer will fail horribly if the search key value is anywhere inside of the comma-separated string. Here is a demonstration: http://sandbox.onlinephpfunctions.com/code/43f6465586ec2b81c8db8e807502bdc2c4d2c8f1 Please delete your answer as it does not provide the OP with the desired result. – mickmackusa May 31 '17 at 10:20