0

I have some problem to fetching key of matching value in array. Here is my array,

Array
(
    [0] => Array
        (
            [0] => 1001;vbaker;[email protected];Vern;Baker;
        )

    [1] => Array
        (
            [0] => 1002;Tesdsd;[email protected];Test;vbaker;
        )

    [2] => Array
        (
            [0] => 1003;demosa;[email protected];Baker;Tesdsd;
        )

)

So here for example, I want get key of 1002(value).. How do I get that ..? My need is ,I have only the id(say 1002) so I have to get the other values like Tesdsd;[email protected];Test;vbaker;

9
  • 1
    Loop through the array and use explode() to separate on the semicolon? Commented Apr 23, 2014 at 16:57
  • By "key", do you mean the index of the outer array? So given 1002, you want the result to be 1? Commented Apr 23, 2014 at 16:58
  • yes..I want get the index of particular matching value Commented Apr 23, 2014 at 17:00
  • Your update seems to contradict that. Commented Apr 23, 2014 at 17:01
  • Each element in your array is an array of length 1, each solo-child element containing a string. Are you suggesting that you explode that string into an array (delimiter ;) such that the expected result searching for "1002" would be [1][0][0]? Commented Apr 23, 2014 at 17:13

4 Answers 4

1

Loop through the array, split on ;, store the first and last parts into variables ($first and $last), check if $first is the same as your search string, and if so return $last:

$search = 1002;

foreach ($array as $key => $sub) {
    list($first, $last) = explode(';', $sub[0], 2);
    if ($first == $search) {
        echo $last;
    }
}

Output:

Tesdsd;[email protected];Test;vbaker;

Demo

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

5 Comments

Thanks for your help.But I need to get the key..for fetching other values..please have a look at my updated question..
@Elavarasan: I'm confused after seeing your update. Could you edit your question to include the expected result so that I can understand what you're trying to do?
The array is a csv file. I just convert it to array. It contains users details, like id, name, email etc..For my purpose I want get all details of that user by id. So I want get details of user_id 1002. How do I achieve that ..?
@Elavarasan: Personally, I'd use something like while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) { ... } and store it in an array and then search the array afterwards. But does this answer your question?
Thanks Amal..Finally I got fixed with your answer, but additionally I want get the key for the matching value(1002).. like. $array[0][1][0];
1

To get the value:

$search = 1002;
$result = array_values(preg_grep("/$search/", call_user_func_array('array_merge', $array)))[0];

Which gives:

1002;Tesdsd;[email protected];Test;vbaker;

Then you can explode() on ; and get the parts you want.

To get the key/index (which is 1) try:

$search = 1002;
$key = key(preg_grep("/$search/", call_user_func_array('array_merge', $array)));

Comments

1

Because I love PHP's array functions so much, here are my 2 cents:

$search = '1003';

$matches = array_filter($array, function($v) use ($search) {

    return strpos($v[0], $search) !== false;

});

echo 'The keys are: ' . implode(', ', array_keys($matches));

//EDIT:

If you don't actually need the keys (as you said in your original question) but also want to work with the values, this may be a better approach:

$search = '1003';

array_walk($array, function($v, $k) use ($search) {

    if ( strpos($v[0], $search) !== false )
    {
        // do whatever you want with the data
        echo 'Found ' . ltrim($v[0], $search.';') . ' at key ' . $k;
    }

});

The other answers will also do the trick, but as I said, I'm a huge fan of array functions, because I find them more elegant than loops.

3 Comments

@AmalMurali What the... The updated question is completely different from the original one.
Yes. The updated question reads "My need is ,I have only the id(say 1002) so I have to get the other values like Tesdsd;[email protected];Test;vbaker" — your code will output 2 (which I assume is not what the OP wants).
@AmalMurali Thanks for the clarification, I've updated my answer accordingly.
0

This should get you on the right tracks...

$yourKey = null;
foreach ($arr as $key => $value) {
    if($value == $someSearchQuery){
        $yourKey = $key;
    }
}

1 Comment

Yeah, you're right.. this should get him into the right tracks though.

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.