数组键上的模式匹配

发布于 2024-08-08 22:35:53 字数 306 浏览 5 评论 0原文

我需要从该数组中获取股票值:

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

我需要在此数组上进行模式匹配,其中数组键 =“stock”+ 1 个通配符。 我尝试过使用数组过滤函数来获取 PHP 手册上的所有其他值,但是 空值似乎将其丢弃。我尝试了很多不同的方法,但发现没有任何效果。

这可以做到吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

尴尬癌患者 2024-08-15 22:35:53
<?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 );
    }
}

我希望我的解释正确,并且您想要“stock”、1 个不是换行符的通配符,然后是字符串结尾。

<?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.

灼痛 2024-08-15 22:35:53

您应该将它们存储为:

Array(
  [0] => Array(
    stock => 1,
    stockdate => ...
  ),
  [1] => Array(
    stock => 3,
    stockdate => apple
  ),
  ...
)

You should store those as:

Array(
  [0] => Array(
    stock => 1,
    stockdate => ...
  ),
  [1] => Array(
    stock => 3,
    stockdate => apple
  ),
  ...
)
内心旳酸楚 2024-08-15 22:35:53

从 PHP 5.6.0 开始,flag 选项已添加到 array_filter 中。这允许您根据数组键而不是其值进行过滤:

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

Since PHP 5.6.0 the flag option has been added to array_filter. This allows you to filter based on the array keys rather than its values:

array_filter($items, function ($key) {
  return preg_match('/^stock\d$/', $key);
}, ARRAY_FILTER_USE_KEY);
流殇 2024-08-15 22:35:53

array_filter 无权访问密钥,因此不是适合您工作的工具。

我相信您想要做的是这样的:

$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 看起来像这样:

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

您可能需要对它稍微大惊小怪,但我认为这就是您所要求的。

然而,如果您可以选择的话,您确实应该遵循杰夫·奥伯的建议。

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.

季末如歌 2024-08-15 22:35:53
# returns array('stock1' => 'foo')
array_flip(preg_grep('#^stock.$#', array_flip(array('stock1' => 'foo', 'stockdate' => 'bar'))))

不确定正则表达式和两次翻转的性能有多好,但可维护性极好(循环中没有错误搜寻)。

# 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 the regex and two flips, but excellent maintainability (no bug hunting in loops).

热风软妹 2024-08-15 22:35:53

好的工作解决方案:
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);

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);
疾风者 2024-08-15 22:35:53

要允许超过 9 的数字后缀,您可以使用 rtrim() 而不是正则表达式来删除数字后缀,然后再次检查搜索字符串是否有相同的匹配项。

代码:(演示

$stocks = [
    "stock0" => 1,
    "stockdate0" => '',
    "stock1" => 3, 
    "stockdate1" => 'apple',
    "stock4DON'TMATCHTHIS!!!" => 4,
    "stock2" => 2,
    "stockdate2" => '',
    "stock33" => 333,
    "stockdate33" => ''
];

$find = 'stock';
var_export(
    array_filter($stocks, fn($k) => rtrim($k, '0..9') === $find, ARRAY_FILTER_USE_KEY)
);

输出:

array (
  'stock0' => 1,
  'stock1' => 3,
  'stock2' => 2,
  'stock33' => 333,
)

To allow numeric suffixes that exceed 9, you can use rtrim() instead of regex to remove the numeric suffix, then check for an identical match again the search string.

Code: (Demo)

$stocks = [
    "stock0" => 1,
    "stockdate0" => '',
    "stock1" => 3, 
    "stockdate1" => 'apple',
    "stock4DON'TMATCHTHIS!!!" => 4,
    "stock2" => 2,
    "stockdate2" => '',
    "stock33" => 333,
    "stockdate33" => ''
];

$find = 'stock';
var_export(
    array_filter($stocks, fn($k) => rtrim($k, '0..9') === $find, ARRAY_FILTER_USE_KEY)
);

Output:

array (
  'stock0' => 1,
  'stock1' => 3,
  'stock2' => 2,
  'stock33' => 333,
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文