我需要获得股票价值出这个数组:
I need to get the stock values out of this array:
Array ( [stock0] => 1 [stockdate0] => [stock1] => 3 [stockdate1] => apple [stock2] => 2 [ stockdate2] => )我需要这个数组,数组键=股+ 1通配符的模式匹配。我已经使用数组过滤功能,以获取有关PHP手册每隔值尝试,但空值似乎把它扔出去。我尝试了很多不同的东西,我发现,但没有什么工作。
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.
可以这样做?
推荐答案array_filter没有访问密钥,因此不适合你的工作的工具。
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; }现在$ stockList看起来是这样的:
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.
更多推荐
对数组的键模式匹配
发布评论