对数组的键模式匹配

编程入门 行业动态 更新时间:2024-10-18 22:28:46
本文介绍了对数组的键模式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要获得股票价值出这个数组:

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.

更多推荐

对数组的键模式匹配

本文发布于:2023-07-29 00:42:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1235643.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   模式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!