-1

I have an Array with this format :

countries(
    [0] = Array
        (
            [0] = 1
            [1] = USA


        )

    [1] = Array
        (
            [0] = 93
            [1] = Afghanistan
        )

    [2] = Array
        (
            [0] = 358
            [1] = Finland
        )



)

=======

[0] country code
[1] country name

and I want to search this array to get which country the number belong to ..

example : 358545454554 , how can i search the array to get which country , in this case = Finland please consider the performance , so I want the fastest way

Ali
  • 73
  • 10
  • is 358545454554 an array or string? – Ripa Saha Feb 12 '13 at 10:08
  • 1
    [What have you tried?](http://whathaveyoutried.com) Is it too much to expect to see some kind of loop where you are trying to see if your number starts with the area code of each array element? – Jon Feb 12 '13 at 10:09
  • Check http://stackoverflow.com/questions/1019076/how-to-search-by-key-value-in-a-multidimensional-array-in-php?rq=1 – Rikesh Feb 12 '13 at 10:09

4 Answers4

3

To match the first digits, compare the substring of these digits with your result: (demo)

<?php
$search = '358545454554';
$countries = [[1, 'USA'], [93, 'Afghanistan'], [358, 'Finland']];
$country = null;
foreach ($countries as $c) {
  if (substr($search, 0, strlen($c[0])) == $c[0]) {
    $country = $c[1];
    break;
  }
}
echo ($country === null) ? 'No country found!' : $country;
phihag
  • 278,196
  • 72
  • 453
  • 469
  • I normally would not upvote any answer to a question like this, but the other ones are just appalling. – Jon Feb 12 '13 at 10:32
  • I know this answer , but i am looking for better solution that get the result very fast – Ali Feb 12 '13 at 11:03
  • @Ali This whole program executes about 0.01s on ideone.com, including php warmup. If you want a faster program, you'll have to have better data structures, for example a [tree](http://en.wikipedia.org/wiki/Search_tree) of all countries that you can navigate. In any case, this code is unlikely to be the slowest thing in your application. Most likely, fetching the objects from the database will dwarf associating them to countries. – phihag Feb 12 '13 at 11:22
  • @phihag thanks, but in my case , we have 1000,000 mobile number and want to for loop to get the country belong to each number – Ali Feb 17 '13 at 13:04
0
if (strpos($string, $word) === FALSE) {
   ... not found ...
}

note that strpos is case sensitive, if you want a case-insensitive search, use stripos() instead.

also note the ===, forcing a strict equality test. strpos CAN return a valid 0 if the 'needle' string is at the start of the 'haystack'. By forcing a check for an actual boolean false (aka 0), you eliminate that false positiv

One Man Crew
  • 9,420
  • 2
  • 42
  • 51
0
$countries =  array(
    array('1','USA'),
    array('93','Afghanistan'),
    array('358','Finland'),
    array('3','India')
);
$input_code = '358545454554';
$searched = array();
foreach($countries as $country) {
    if (strpos($input_code, $country[0]) !== FALSE) 
        $searched[] = $country[1];
}
print_r($searched);

please visit here(demo).

  • `strpos == true` relies on very questionable casting. `strpos` can only return `false` or numbers. Also, you're essentially emulating [`array_filter`](http://php.net/array_filter) for no good reason. – phihag Feb 12 '13 at 10:21
0

You could use array_filter for this. The first variant searches the number anywhere in the country ID, so 3581545454554 matches the US and Finnland, second variant uses a regular expression to filter only ids starting with the same digits.

$data = array(
    array(1, 'USA'),
    array(93, 'Afghanistan'),
    array(358,'Finland')

);
$term = '358545454554';

// in case you want to match it anywhere in the term string...
$result = array_filter($data, function($elem) use($term) {
    return (strpos((string) $term,(string) $elem[0]) !==false ) ? true : false;
});

var_dump($result);

// in case you want to match only from start
$result = array_filter($data, function($elem) use($term) {
    return preg_match('/^'.$elem[0].'/',$term);
});

var_dump($result);
axel.michel
  • 5,764
  • 1
  • 15
  • 25
  • `array_filter` works fine, but that means if 99% of his customers are in the US, the runtime will be multiplied by 200, since all countries are always evaluated. – phihag Feb 12 '13 at 11:25