• 8
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 187

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 187
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

selecting an array key based on partial string

I have an array and in that array I have an array key that looks like, show_me_160 this array key may change a little, so sometimes the page may load and the array key maybe show_me_120, I want to now is possible to just string match the array key up until the last _ so that I can check what the value is after the last underscore?

one solution i can think of:

foreach($myarray as $key=>$value){
  if("show_me_" == substr($key,0,8)){
    $number = substr($key,strrpos($key,'_'));
    // do whatever you need to with $number...
  }
}
  • 31
Reply Report

you would have to iterate over your array to check each key separately, since you don't have the possibility to query the array directly (I'm assuming the array also holds totally unrelated keys, but you can skip the if part if that's not the case):

foreach($array as $k => $v)
{
  if (strpos($k, 'show_me_') !== false)
  {
    $number = substr($k, strrpos($k, '_'));
  }
}

However, this sounds like a very strange way of storing data, and if I were you, I'd check if there's not an other way (more efficient) of passing data around in your application ;)

  • 8
Reply Report
    • This works for me in my situation, though note that this line: if (strpos($k, 'show_me_') !== false) should be if (strpos($v, 'show_me_') !== false)
      • 2
    • It would be more desirable to use 0 === strpos($k, 'show_me_') to ensure it is only the prefix that matches and values like noshow_me_ do not also match

You can also use a preg_match based solution:

foreach($array as $str) {
        if(preg_match('/^show_me_(\d+)$/',$str,$m)) {
                echo "Array element ",$str," matched and number = ",$m[1],"\n";
        }
}
  • 4
Reply Report

to search for certain string in array keys you can use array_filter(); see docs

// the array you'll search in
$array = ["search_1"=>"value1","search_2"=>"value2","not_search"=>"value3"];
// filter the array and assign the returned array to variable
$foo = array_filter(
    // the array you wanna search in
    $array, 
    // callback function to search for certain sting
    function ($key){ 
        return(strpos($key,'search_') !== false);
    }, 
    // flag to let the array_filter(); know that you deal with array keys
    ARRAY_FILTER_USE_KEY
);
// print out the returned array
print_r($foo);

if you search in the array values you can use the flag 0 or leave the flag empty

$foo = array_filter(
    // the array you wanna search in
    $array, 
    // callback function to search for certain sting
    function ($value){ 
        return(strpos($value,'value') !== false);
    }, 
    // flag to let the array_filter(); know that you deal with array value
    0
);

or

$foo = array_filter(
    // the array you wanna search in
    $array, 
    // callback function to search for certain sting
    function ($value){ 
        return(strpos($value,'value') !== false);
    }
);

if you search in the array values and array keys you can use the flag ARRAY_FILTER_USE_BOTH

$foo = array_filter(
    // the array you wanna search in
    $array, 
    // callback function to search for certain sting
    function ($value, $key){ 
        return(strpos($key,'search_') !== false or strpos($value,'value') !== false);
    },
    ARRAY_FILTER_USE_BOTH
);

in case you'll search for both you have to pass 2 arguments to the callback function

  • 2
Reply Report
 foreach($myarray as $key=>$value)
    if(count(explode('show_me_',$event_key)) > 1){
         //if array key contains show_me_
    }

More information (example):

if array key contain 'show_me_'

$example = explode('show_me_','show_me_120');

print_r($example)

Array ( [0] => [1] => 120 ) 

print_r(count($example))

2 

print_r($example[1])

 120 
  • 0
Reply Report