Pattern matching by array type

I need to get the stock values ​​from this array:

Array ( [stock0] => 1 [stockdate0] => [stock1] => 3 [stockdate1] => apple [stock2] => 2 [ stockdate2] => ) 

I need to match pattern matching in this array, where the array key = "stock" + 1 wildcard. I tried using the array filter function to get any other value in the PHP manual, but empty values ​​seem to throw it away. I tried a lot of different things that I found, but nothing works.

Can this be done?

+5
arrays php wildcard pattern-matching key
source share
6 answers

array_filter does not have access to the key and therefore is not suitable for your work.

I believe you want to do this:

 $stocks = Array ( "stock0" => 1, "stockdate0" => '', "stock1" => 3, "stockdate1" => 'apple', "stock2" => 2, "stockdate2" => '' ); $stockList = array(); //Your list of "stocks" indexed by the number found at the end of "stock" foreach ($stocks as $stockKey => $stock) { sscanf($stockKey,"stock%d", &stockId); // scan into a formatted string and return values passed by reference if ($stockId !== false) $stockList[$stockId] = $stock; } 

Now $ stockList looks like this:

 Array ( [0] => 1 [1] => 3 [2] => 2 ) 

You may need a little with him, but I think this is what you are asking for.

HOWEVER, you really should follow Jeff Obron's advice, if you have the opportunity to do so.

+2
source share
 <?php $foo = array ( 'stock0' => 1, 'stockdate0' => 1, 'stock1' => 3, 'stockdate1' => 2, ); $keys = array_keys( $foo ); foreach ( $keys as $key ) { if ( preg_match( '/stock.$/', $key ) ) { var_dump( $key ); } } 

I hope I interpret correctly and you would like to have a β€œmargin”, 1 wildcard that is not a newline, and then the end of the line.

+4
source share

You must save them as:

 Array( [0] => Array( stock => 1, stockdate => ... ), [1] => Array( stock => 3, stockdate => apple ), ... ) 
+4
source share
 # returns array('stock1' => 'foo') array_flip(preg_grep('#^stock.$#', array_flip(array('stock1' => 'foo', 'stockdate' => 'bar')))) 

Not sure how good the performance is due to regex and two flips, but excellent maintainability (no errors in the loop).

+1
source share

Since PHP 5.6.0, the flag parameter has been added to array_filter . This allows you to filter based on the keys of the array, and not its values:

 array_filter($items, function ($key) { return preg_match('/^stock\d$/', $key); }, ARRAY_FILTER_USE_KEY); 
+1
source share

Good working solution: Green for ChronoFish!

  $stockList = array(); //Your list of "stocks" indexed by the number found at the end of "stock" foreach ($stock as $stockKey => $stock) { sscanf($stockKey,"message%d", $stockId); // scan into a formatted string and return values passed by reference if ($stockId !== false) { $stockList[$stockId] = $stock; } $stockList=array_values($stockList); //straightens array keys out $stockList = array_slice ($stockList, "0", $count); //gets rid of blank value generated at end of array (where $count = the array orginal length) print_r ($stockList); 
0
source share

All Articles