Php 前四个字符相同的Foreach

Php 前四个字符相同的Foreach,php,Php,我有一个目录,其中包含如下文件: 0000_01.jpg 0000_02.jpg 0000_03.jpg 2000_01.jpg 2000_02.jpg 2800_01.jpg 3200_01.jpg 我想得到一个表格,显示每篇文章的可用数字(0000、2000、2800、3200等等) 我明白了: $files = glob('new/' . $map . '*.{jpg}', GLOB_BRACE); foreach($files as $file) { } 现在我需要检查这四个字符是

我有一个目录,其中包含如下文件:

0000_01.jpg
0000_02.jpg
0000_03.jpg
2000_01.jpg
2000_02.jpg
2800_01.jpg
3200_01.jpg
我想得到一个表格,显示每篇文章的可用数字(0000、2000、2800、3200等等)

我明白了:

$files = glob('new/' . $map . '*.{jpg}', GLOB_BRACE);
foreach($files as $file) {

}
现在我需要检查这四个字符是否匹配,并检查是否有可用的
\u 01
\u 02
\u 03

我知道php函数
substr()
,但我不知道如何在
foreach
或其他程序中实现它

我想得到一张这样的桌子:

artnr    _01    _02    _03
0000      x      x      x
2000      x      x      
2800      x
..
有人能告诉我这样做的正确方向吗?

PHP代码 您可以通过以下方式检查“0000”中“01”的存在性:

    if ($arr["0000"]["01"])
后果
var\u导出($arr)

array (
  '0000' => 
  array (
    '01' => true,
    '02' => true,
    '03' => true,
  ),
  2000 => 
  array (
    '01' => true,
    '02' => true,
  ),
  2800 => 
  array (
    '01' => true,
  ),
  3200 => 
  array (
    '01' => true,
  ),
)
创建表

阿特纳
_01
_02
_03
后果

PHP代码 您可以通过以下方式检查“0000”中“01”的存在性:

    if ($arr["0000"]["01"])
后果
var\u导出($arr)

array (
  '0000' => 
  array (
    '01' => true,
    '02' => true,
    '03' => true,
  ),
  2000 => 
  array (
    '01' => true,
    '02' => true,
  ),
  2800 => 
  array (
    '01' => true,
  ),
  3200 => 
  array (
    '01' => true,
  ),
)
创建表

阿特纳
_01
_02
_03
后果


阿特纳

阿特纳

如果我正确理解了您的问题,那么您可以使用和来获取列标题和行标题:

<?php
header('Content-Type: text/plain; charset=utf-8');

$files = [
    "0000_01.jpg",
    "0000_02.jpg",
    "0000_03.jpg",
    "2000_01.jpg",
    "2000_02.jpg",
    "2800_01.jpg",
    "3200_01.jpg",
];

$buffer = array_map(
    function($name){ return explode('_', $name); },
    $files
);

$groups  = array_unique(array_column($buffer, 0));
$columns = array_unique(array_column($buffer, 1));

print_r($groups);
print_r($columns);
?>
然后建立他们的交叉点:

foreach($groups as $group){
    foreach($columns as $column){
        if(in_array("{$group}_{$column}", $files)){
            echo "{$group}_{$column}", ' <- exists', PHP_EOL;
        } else {
            echo "{$group}_{$column}", ' <- not exists', PHP_EOL;
        }
    }
}
foreach($group作为$group){
foreach($columns作为$column){
if(在数组(“{$group}{$column},$files”)中){

echo“{$group}{$column}”,如果我正确理解了您的问题,那么您可以使用和来获取列标题和行标题:

<?php
header('Content-Type: text/plain; charset=utf-8');

$files = [
    "0000_01.jpg",
    "0000_02.jpg",
    "0000_03.jpg",
    "2000_01.jpg",
    "2000_02.jpg",
    "2800_01.jpg",
    "3200_01.jpg",
];

$buffer = array_map(
    function($name){ return explode('_', $name); },
    $files
);

$groups  = array_unique(array_column($buffer, 0));
$columns = array_unique(array_column($buffer, 1));

print_r($groups);
print_r($columns);
?>
然后建立他们的交叉点:

foreach($groups as $group){
    foreach($columns as $column){
        if(in_array("{$group}_{$column}", $files)){
            echo "{$group}_{$column}", ' <- exists', PHP_EOL;
        } else {
            echo "{$group}_{$column}", ' <- not exists', PHP_EOL;
        }
    }
}
foreach($group作为$group){
foreach($columns作为$column){
if(在数组(“{$group}{$column},$files”)中){
echo“{$group}{$column}”,这个怎么样:

<?php
$files = array('0000_01.jpg', '0000_02.jpg', '0000_03.jpg', '2000_01.jpg', '2000_02.jpg', '2800_01.jpg', '3200_01.jpg');
?>
<!DOCTYPE html>
<html>
    <head>
        <title>testing</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="jquery.js" type="text/javascript"></script>
    </head>
    <body>
        <?php
        $list = array();

        foreach ($files as $file) {
            preg_match('/(\d+).?(\d+)\./', $file, $match);
            $name = $match[1];
            $col = $match[2];

            $list[$col][] = $name;
        }

        foreach ($list as $key => $item) {
            foreach ($item as $value) {
                echo "$key, $value<br/>";
            }
        }
        ?>
    </body>
</html>

测试
这个怎么样:

<?php
$files = array('0000_01.jpg', '0000_02.jpg', '0000_03.jpg', '2000_01.jpg', '2000_02.jpg', '2800_01.jpg', '3200_01.jpg');
?>
<!DOCTYPE html>
<html>
    <head>
        <title>testing</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="jquery.js" type="text/javascript"></script>
    </head>
    <body>
        <?php
        $list = array();

        foreach ($files as $file) {
            preg_match('/(\d+).?(\d+)\./', $file, $match);
            $name = $match[1];
            $col = $match[2];

            $list[$col][] = $name;
        }

        foreach ($list as $key => $item) {
            foreach ($item as $value) {
                echo "$key, $value<br/>";
            }
        }
        ?>
    </body>
</html>

测试
另一种解决方案:

$a=array(
'0000_01.jpg',
'0000_02.jpg',
'0000_03.jpg',
'2000_01.jpg',
'2000_02.jpg',
'2800_01.jpg',
'3200_01.jpg');

$result_array=array();
foreach ($a as $item) {
   list($art, $artnr) = array_slice(str_split($item, 4),0,2);
   $result_array[$art][] = substr($artnr,1,-1);
}
print '<pre>'; 
print_r($result_array); 
print '</pre>';
相关问题:

另一个解决方案:

$a=array(
'0000_01.jpg',
'0000_02.jpg',
'0000_03.jpg',
'2000_01.jpg',
'2000_02.jpg',
'2800_01.jpg',
'3200_01.jpg');

$result_array=array();
foreach ($a as $item) {
   list($art, $artnr) = array_slice(str_split($item, 4),0,2);
   $result_array[$art][] = substr($artnr,1,-1);
}
print '<pre>'; 
print_r($result_array); 
print '</pre>';

相关问题:

为什么不是正则表达式


for($i=1;i为什么不是正则表达式


for($i=1;通常
for
map
或PHP中的其他函数在数组中遍历快得多。总是
for
map
或PHP中的其他函数在数组中遍历快得多。
for($i=1;i<=3;i++){

$result = preg_match("/_0"+$i+"/",$file);

}