Filter array for values ​​starting with the user selected character

I have a quickie :)

I have a null array consisting of string values:

array 0 => string 'Message 1' (length=9) 1 => string '%company' (length=8) 2 => string 'Message 2' (length=9) 3 => string '%name' (length=5) 

I need to select all values ​​starting with % and ideally put them in another array.

 array 0 => string 'Message 1' (length=9) 1 => string 'Message 2' (length=9) array 0 => string '%company' (length=8) 1 => string '%name' (length=5) 

Thanks!

For anyone interested, the first array is the result of a validation function, and since I hate it when the validator returns information about the required inputs in a million lines (for example: it requires <br><br> , which is required ...), instead of displaying real messages, I display the names of the necessary and unfilled entries that should be placed in one nice message "Fields are, these are even mandatory" :)

Miniedit: we will be grateful even for links to questions with answers to stackoverflow :)

+4
source share
5 answers

PHP> 5.3, below that you need to use create_function () .

This solution first filters the source array and gets elements starting with % . Then array_diff () is used to get the array with the remaining values.

 $array_percent = array_filter($orig_array, function ($v) { return substr($v, 0, 1) === '%'; }); $array_others = array_diff($orig_array, $array_percent); 
+16
source

This is simple code for partitioning using array_walk() and an anonymous callback.

 $result1 = array(); $result2 = array(); array_walk($array, function($v) use (&$result1, &$result2) { if ($v[0] == '%') { $result1[] = $v; } else { $result2[] = $v; } }); 

Put this in the reusable code:

 function partition($array, $callback) { $resultTrue = array(); $resultFalse = array(); array_walk($array, function($v) use (&$resultTrue, &$resultFalse, $callback) { if (call_user_func($callback, $v) === true) { $resultTrue[] = $v; } else { $resultFalse[] = $v; } }); return array($resultTrue, $resultFalse); } // and put to use: $partitionedData = partition($array, function($v) { return $v[0] == '%'; }); 
+5
source

Apologies for resurrecting the question, but simple filtering like this is very simple: preg_grep() .

 $subject = array('Message 1', '%company', 'Message 2', '%name'); $percents = preg_grep('/^%/', $subject); $others = preg_grep('/^%/', $subject, PREG_GREP_INVERT); var_dump($percents, $others); 
+5
source

you can use array_filter with callback ,

 <?php $array = array( 0 => 'Message 1' , 1 => '%company' , 2 => 'Message 2' , 3 => '%name' ); function start_with($var) { return $var[0] == '%'; } function dont_start_with($var) { return $var[0] != '%'; } $startw = array_filter($dizi,'start_with'); // filtering elements $startw = array_values($startw); // reseting indexes. $nstartw = array_filter($dizi,'dont_start_with'); $nstartw = array_values($nstartw); var_dump($startw , $nstartw); ?> 
+2
source
 $array2=array(); foreach($array as $key => $value) { if($value[0] =='%') { $array2[]=$value; unset($array[$key]); } } 

EDIT

after cancellation, you can either use the foreach for the iterative array, or use the array_values function to build a new array

+1
source

Source: https://habr.com/ru/post/1346682/


All Articles