2

I need to get the value for a certain key, they key is not the same all the time. initial part remains same but every time I get new id added in the end.

My array is like this:

senario 1:

 Array
(
    [custom_194_1] => 123
    [_f_upload] => Save
)

senario 2:

Array
(
    [custom_194_2] => 456
    [_f_upload] => Save
)

I need to get the value 123 in senario 1, 456 in senario 2.

Can anyone please help me on how to get the value from this array key.

7
  • foreach($ar as $k=>$v) if preg_match(...$k) Commented Aug 10, 2016 at 13:49
  • If the initial part is always the same, substr should be faster: if(substr($key,0,10)=='custom_194') then the key begins with custom_194. Or, you can use strpos to ensure the substring is at position 0. There are many ways to do it if regex is too confusing for you. Commented Aug 10, 2016 at 13:50
  • Possible duplicate of Fastest way to iterate array in PHP Commented Aug 10, 2016 at 13:51
  • Tthanks for the idea kainaw, but how do i get 123 value?? as end part is changing all the time Commented Aug 10, 2016 at 13:56
  • what is constant part - custom_194 or custom_ ? Commented Aug 10, 2016 at 14:12

3 Answers 3

0

If your key is always the first element, and the array is $array the fastest way is:

$result = reset($array);

Or if you don't want to mess with the array's internal pointer:

$result = array_values($array)[0];

If you want the value of the key:

$key = array_keys($array)[0];
Sign up to request clarification or add additional context in comments.

1 Comment

array is big, and its not the first element, that does change the position based on the fields, so I cant depend on it. I just gave 2 elements in array as example. I need to find that last bit in the end of the key.
0

Thanks for your time guys. I'm using foreach to loop through and then checking with every key with substring. Hope it helps someone in future, not the very best solution though.

foreach($fields as $key => $val)
    {
        if(substr($key,0,10)=='custom_194'){
            $realValue = $val;
            echo "<br>value i'm looking for:";print_r($val);    
        }
    }

Comments

0

Because you stated that you want the number at the end of the key and because you appear to want to learn more about regular expressions... This is not a hard task to do with preg_match.

Assume $array is the array that you begin with that has all the key=>val values.

foreach($fields as $key=>$val)
{
    if(preg_match('/^custom_194_([0-9]+)$/', $key, $matches))
    {
        $num = $matches[1];
        print "Key number $num has value $val\n";
    }
}

The regular expression is ^custom_194_([0-9]+)$. The ^ means "beginning of the string." The $ means "end of the string." You can see that we explicitly spell out custom_194_. Then, we use ( and ) to identify a substring that we want to keep in the matches array. Inside ( and ), we look for the characters 0 through 9 using [0-9]. The + means "1 or more characters." So, we want 1 or more 0 through 9 characters.

The match array contains the entire string matched in the first index and then each sub-match in the remaining indexes. We only have one sub-match, which will be in index 1. So, $num is in $matches[1].

Comments

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.