views:

303

answers:

4

Hi. Since I'm finding this site helpful I thought I'd sign up :)

I need to get the stock values out of this array:

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

I need to pattern match on this array, where the array key = "stock" + 1 wildcard character. I have tried using the array filter function to get every other value on the php manual but the empty values seem to throw it out. I tried alot of different things I found but nothing is working.

Can this be done?

+1  A: 
<?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'm hoping I interpreted correctly and you wanted 'stock', 1 wildcard character thats not a newline, then end of string.

meder
This looks like the straight answer to the OP's question. I would apply http://www.refactoring.com/catalog/inlineTemp.html to this solution to get rid of the temp variable $keys.
Ewan Todd
I only left the temporary variable incase for whatever reason he wanted to use them again, and usually it's easier to grasp for newbies the more variables you make instead of making the expressions harder to grasp.
meder
I'm a big fan of RegEx and this could certainly be done this way.
ChronoFish
+2  A: 

You should store those as:

Array(
  [0] => Array(
    stock => 1,
    stockdate => ...
  ),
  [1] => Array(
    stock => 3,
    stockdate => apple
  ),
  ...
)
Jeff Ober
I'm actually getting this from a $_POST array. I don't seem to have much choice in it.
Oliver
+1  A: 

array_filter does not have access to the key and therefore is not the right tool for your job.

I belive what you're looking to do is 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 to fuss with it a bit, but I think this is what you are asking for.

HOWEVER, you really should be following Jeff Ober's advice if you have the option to do so.

ChronoFish
This looks good.<br>At the moment however it's returning<br>[0] => ''<br>[1] => apple<br>[2] => ''<br><br>I.E the stockdate instead of the stock value<br>I think I can adjust it - just feel free to help me with that as well!
Oliver
I just tried switching "stock%d" (wrong values) to "stockdate%d" which gave me the values: [] => 1 [0] => 2 [1] => 3 [2] => ''. Do you know how straighten the keys out?
Oliver
A: 

Ok 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's orginal length)
print_r ($stockList);
Oliver