符合特定格式的数组中的截断字符串,然后过滤不变的元素

发布于 2025-02-03 03:22:48 字数 724 浏览 1 评论 0 原文

我有一个数组,它是我想要保留的弦和弦的混合物,我不想保留。在要保留的字符串中,我需要将它们从第一次发生的斜线中截断。

我的数组:

$code = [
    'PO/2022/0001',  // modify
    'abc',           // remove
    'xyz',           // remove
    'PO2022/0001',   // remove
    'XY/2022/0002',  // modify
    'PO/2022/0232'   // modify
];

我正在尝试爆炸上面的数组中的字符串在/上,如果新生成的数组具有3个元素,那么我需要将第一个值推入我的结果数组。

样本输入的预期结果:

['PO', 'XY', 'PO']

我能知道这样做的更好,有效的方法是什么?

这就是我到目前为止的:

foreach ($code as $v) {
    $nwCode = explode("/",$v);
    if(count($nwCode) == 3) {
      $nwAry[] = $newCode[0];
    }
    
    $nwCode = [];
}

echo '<pre>',print_r ($nwAry).'</pre>';

I have an array which is a mix of strings that I want to keep and strings that I don't want to keep. Of the strings to keep, I need to truncate them from their first occurring slash.

My array:

$code = [
    'PO/2022/0001',  // modify
    'abc',           // remove
    'xyz',           // remove
    'PO2022/0001',   // remove
    'XY/2022/0002',  // modify
    'PO/2022/0232'   // modify
];

I am trying to explode the strings in the above array on / and if the newly generated array has 3 elements, then I need to push the first value into my result array.

Expected result from sample input:

['PO', 'XY', 'PO']

Can I know what is the better and efficient approach to do this?

This is what I have so far:

foreach ($code as $v) {
    $nwCode = explode("/",$v);
    if(count($nwCode) == 3) {
      $nwAry[] = $newCode[0];
    }
    
    $nwCode = [];
}

echo '<pre>',print_r ($nwAry).'</pre>';

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

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

发布评论

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

评论(2

捎一片雪花 2025-02-10 03:22:48
$code = ['PO/2022/0001', 'abc','xyz','PO2022/0001', 'XY/2022/0002','PO/2022/0232'];

$prefixes = array_map(function($e){
  $nwCode = explode('/', $e);
  if(count($nwCode) == 3)
  {
    return $nwCode[0];
  }
} ,$code);

$prefixes = array_filter($prefixes, function($e){ if(strlen($e) > 0) return true; });

echo "<pre>";
var_dump($prefixes);
echo "</pre>";

array_map 用于获得这些前缀,while array_filter 从无法比拟的项目中删除空的前缀。

您可以使用 array_reduce 也是如此。

$prefixes = array_reduce($code, function($carry, $item){
  $nwCode = explode('/', $item);
  if(count($nwCode) == 3)
  {
    array_push($carry, $nwCode[0]);
  }
  return $carry;
}, array());
$code = ['PO/2022/0001', 'abc','xyz','PO2022/0001', 'XY/2022/0002','PO/2022/0232'];

$prefixes = array_map(function($e){
  $nwCode = explode('/', $e);
  if(count($nwCode) == 3)
  {
    return $nwCode[0];
  }
} ,$code);

$prefixes = array_filter($prefixes, function($e){ if(strlen($e) > 0) return true; });

echo "<pre>";
var_dump($prefixes);
echo "</pre>";

The array_map used to get those prefixes, while array_filter to remove the empty prefixes from the unmatching items.

You could use array_reduce too.

$prefixes = array_reduce($code, function($carry, $item){
  $nwCode = explode('/', $item);
  if(count($nwCode) == 3)
  {
    array_push($carry, $nwCode[0]);
  }
  return $carry;
}, array());
┼── 2025-02-10 03:22:48

因为您想使用严格的格式/签名在字符串上执行替换,并过滤出与签名匹配的元素,因此PHP提供的最佳工具是 pregg_filter()

模式分解:

^              #start of string
[^/]+          #one or more non-slash characters
\K             #forget/release previously matched characters
(?:/[^/]+){2}  #match 2 sets of slash followed by one or more non-slash characters
$              #end of string

代码:( demo

var_export(
    preg_filter(
        '~^[^/]+\K(?:/[^/]+){2}$~',
        '',
        $code
    )
);

输出:

array (
  0 => 'PO',
  4 => 'XY',
  5 => 'PO',
)

如果您需要索引键,请致电 array_values(array_values()在结果数组中。

Because you want to perform replacements on strings with a strict format/signature AND filter out elements that do not match the signature, the best tool that PHP has to offer is preg_filter().

Pattern breakdown:

^              #start of string
[^/]+          #one or more non-slash characters
\K             #forget/release previously matched characters
(?:/[^/]+){2}  #match 2 sets of slash followed by one or more non-slash characters
$              #end of string

Code: (Demo)

var_export(
    preg_filter(
        '~^[^/]+\K(?:/[^/]+){2}$~',
        '',
        $code
    )
);

Output:

array (
  0 => 'PO',
  4 => 'XY',
  5 => 'PO',
)

IF you require indexed keys, call array_values() on the result array.

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