我有一个dir文件,如:
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) {
}
现在我需要检查这四个字符是否匹配,并检查_01
,_02
和_03
是否可用。
我知道php函数substr()
,但我无法弄清楚如何在foreach
或其他东西中实现它。
我想得到一张表格给我这样的表格:
artnr _01 _02 _03
0000 x x x
2000 x x
2800 x
..
有人能指出我这样做的正确方向吗?
答案 0 :(得分:2)
$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;
}
您可以通过以下方式检查'0000'中'01'的存在:
if ($arr["0000"]["01"])
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,
),
)
<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>
答案 1 :(得分:1)
如果我正确理解了您的问题,那么您可以使用array_column()
和array_unique()
来获取列标题和行标题:
<?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);
?>
节目:
Array
(
[0] => 0000
[3] => 2000
[5] => 2800
[6] => 3200
)
Array
(
[0] => 01.jpg
[1] => 02.jpg
[2] => 03.jpg
)
然后建立他们的交集:
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;
}
}
}
节目:
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
答案 2 :(得分:1)
另一种解决方案:
$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>';
输出:
Array
(
[0000] => Array
(
[0] => 01
[1] => 02
[2] => 03
)
[2000] => Array
(
[0] => 01
[1] => 02
)
[2800] => Array
(
[0] => 01
)
[3200] => Array
(
[0] => 01
)
)
相关问题:Grouping and resorting values in PHP array using substrings
答案 3 :(得分:0)
<?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>
答案 4 :(得分:0)
怎么样:
<?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>
答案 5 :(得分:0)