Foreach前四个字符相同

I have a dir with files like:

0000_01.jpg
0000_02.jpg
0000_03.jpg
2000_01.jpg
2000_02.jpg
2800_01.jpg
3200_01.jpg

And I want to get a table showing me which numbers are available for each article (0000, 2000, 2800, 3200 and so on).

I got this:

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

}

Now I need to check if the four characters are matching and check is _01, _02 and _03 are available or not.

I know about the php function substr() but I just can't figure out how to get that implemented in the foreach or something..

I want to get a table showing me like this:

artnr    _01    _02    _03
0000      x      x      x
2000      x      x      
2800      x
..

Can anyone point me in the right direction of doing this?

If I understood your question correctly, then you might use array_column() and array_unique() to get column titles and row headers:

<?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);
?>

Shows:

Array
(
    [0] => 0000
    [3] => 2000
    [5] => 2800
    [6] => 3200
)
Array
(
    [0] => 01.jpg
    [1] => 02.jpg
    [2] => 03.jpg
)

Then build their intersection:

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;
        }
    }
}

Shows:

0000_01.jpg <- exists
0000_02.jpg <- exists
0000_03.jpg <- exists
2000_01.jpg <- exists
2000_02.jpg <- exists
2000_03.jpg <- not exists
2800_01.jpg <- exists
2800_02.jpg <- not exists
2800_03.jpg <- not exists
3200_01.jpg <- exists
3200_02.jpg <- not exists
3200_03.jpg <- not exists

PHP code

    $files = array(
        "0000_01.jpg",
        "0000_02.jpg",
        "0000_03.jpg",
        "2000_01.jpg",
        "2000_02.jpg",
        "2800_01.jpg",
        "3200_01.jpg",
    );
    $arr = array();
    foreach ($files as $file) {
        $arr[substr($file, 0, 4)][substr($file,5, 2)] = true;
    }

and you can check the existance of '01' in '0000' by :

    if ($arr["0000"]["01"])

Result

var_export($arr) :

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

Creating the table

<table>
    <tr>
        <td width="80">artnr</td>
        <td width="80">_01</td>
        <td width="80">_02</td>
        <td width="80">_03</td>
    </tr>
    <?php foreach ($arr as $key => $value) { ?>
    <tr>
        <td><?php echo $key ?></td>
        <td><?php echo $value['01'] ? "X" : "" ?></td>
        <td><?php echo $value['02'] ? "X" : "" ?></td>
        <td><?php echo $value['03'] ? "X" : "" ?></td>
    </tr>
    <?php  } ?>
</table>

Result

The table

<?php
$files = glob('new/' . $map . '*.{jpg}', GLOB_BRACE);
$tableArr = array();
foreach($files as $file) {
    $fileNameArr = explode('_', $file) {
        if(count($fileNameArr) == 2 AND strlen($fileNameArr[0]) == 4 AND strlen($fileNameArr[1]) == 2) {
            $tableArr[$fileNameArr[0]][$fileNameArr[1]] = 'x';
        }
    }
}
?>
<table>
    <thead>
        <tr>
            <th>
                artnr
            </th>
            <?php
            foreach(array_keys($fileNameArr) as $key) {
                ?>
                <th><?=$key?></th>
                <?php
            }
            ?>
        </tr>
    </thead>
    <tbody>
        <?php
        foreach($fileNameArr as $key=>$row) {
            ?>
            <tr>

            <td><?=$key?></td>
            <?php
            foreach($row as $col) {
                ?>
                <td><?=$col?></td>
                <?php
            }
            ?>
            </tr>
            <?php
        }
        ?>
    </tbody>
</table>

how about this:

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

Another solution:

$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>';

Output:

Array
(
    [0000] => Array
        (
            [0] => 01
            [1] => 02
            [2] => 03
        )

    [2000] => Array
        (
            [0] => 01
            [1] => 02
        )

    [2800] => Array
        (
            [0] => 01
        )

    [3200] => Array
        (
            [0] => 01
        )

)

Related question: Grouping and resorting values in PHP array using substrings

Why not a regular expression?

for($i=1;i<=3;i++){

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

}

preg_match on PHP.net