首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP对齐不同长度的多个数组

PHP对齐不同长度的多个数组
EN

Stack Overflow用户
提问于 2018-02-01 22:14:49
回答 2查看 126关注 0票数 1

我可能只是忽略了显而易见的事实,但我想将其归咎于我对PHP并不熟悉的事实。

返回的数组中有一些类似的信息,但数量不同。

我将在下面列出一些示例数组:

代码语言:javascript
复制
(t1-s1-1=1, t1-s1-2=1, t1-s2-1=1, t1-s2-2=1)

(t2-s1-1=1, t2-s2-1=2, t2-s2-2=1)

(t3-s1-1=1, t3-s2-1=1, t3-s3-1=1, t3-s3-2=3)

所以我想用这些信息做一张桌子。就像这样:

代码语言:javascript
复制
test ..  s1-1 ..  s1-2 ..  s2-1 ..  s2-2 ..  s3-1 ..  s3-2
t1  ........1 .....1  ..........1 ....... 1.........1..........1
t2  ........1 .......X..........1..........1........1..........1
t3  ........1 .......X..........1..........X........1..........1

(其中x是不存在的东西。)

因此,每个数组都有一个s1,但是可以有s1-1s1-2s1-3或简单的s1-1。它创建了非常不同大小的数组。

问题是,每个数组可能有非常不同的信息,因为它们是索引数组,而不是关联数组,所以我不知道如何最好地平衡它们。我不能始终如一地说索引3是s1-3或其他什么。

我不能只是手动循环,因为我永远不知道差距会出现在哪里。我无法查找特定的索引,因为数组不是关联的,所以标题是内置在值中的,我不知道如何单独访问它们。

有什么好主意可以让新手忽视吗?我对非表格显示思想开放,只要我能很容易地对信息进行排序和显示。

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-02-01 23:42:25

假设原始数组包含字符串值,因此,例如,在PHP语法中,它们如下所示:

代码语言:javascript
复制
['t1-s1-1=1', 't1-s1-2=1', 't1-s2-1=1', 't1-s2-2=1']

基本上,您应该创建一个二维数组:

  • 遍历所有数组,并使用regex提取不同的部分,即对于上面数组中的第一个元素:t1 (为二维数组中第一级的索引)、s1-1 (为二维数组中的第二级索引)和值1
  • 在二维数组中插入值。
  • 保持在一个单独的数组中,让我们将其命名为allColumns每秒钟索引(sx-y),即使您有重复的值,在最后,您可以删除这些重复并按字母顺序排序。

在此之后,您将拥有二维数组中的所有值,但仍然遗漏了空白,因此您可以在二维数组上进行迭代,对于每个维度的tz (t1,t2,.),对存储在allColumns中的所有值进行遍历,如果您没有在该tz的二维数组中找到该sx-y的条目,则使用值x (或可能以值= 0)添加它。

我认为有一个例子可以澄清上述情况:

代码语言:javascript
复制
// arrays of arrays, I don't know how you receive the data
$arrays = [
    ['t1-s1-1=1', 't1-s1-2=1', 't1-s2-1=1', 't1-s2-2=1'],
    ['t2-s1-1=1', 't2-s2-1=2', 't2-s2-2=1'],
    ['t3-s1-1=1', 't3-s2-1=1', 't3-s3-1=1', 't3-s3-2=3']
];

// bi-dimensional array
$output = [];

// it will store all columns you find in the $arrays entry
$allColumns = [];

// iterate for every array you receive, i.e. ['t1-s1-1=1', 't1-s1-2=1', 't1-s2-1=1', 't1-s2-2=1']
foreach ($arrays as $array) {
    // iterate over every element in the array: 't1-s1-1=1', 't1-s1-2=1', 't1-s2-1=1' and 't1-s2-2=1'
    foreach ($array as $item) {
        // extract the parts on every element: $matches is an array containing the different parts
        preg_match('/^(t\d+)-(s\d+-\d+)=(\d+)/', $item, $matches);
    /**
     * $matches[0] would contains the element if matched: 't1-s1-1=1'
     * $matches[1] would contains 't1' if matched
     * $matches[2] would contains 's1-1' if matched
     * $matches[2] would contains 1 (integer) if matched
     */
        if (!empty($matches)) {
            $output[$matches[1]][$matches[2]] = $matches[3];
            $allColumns[] = $matches[2];
        }
    }
}

// clean duplicates
$allColumns = array_unique($allColumns);

// sort values alphabetically
sort($allColumns);

// iterate over the just created bi-dimensional array
foreach ($output as $row => $columns) {
    // iterate for all columns collected before
    foreach ($allColumns as $column) {
        // if one of column in 'allColumns' doesn't exit in $output you added in the correct place adding a zero value
        if (!in_array($column, array_keys($columns))) {
            $output[$row][$column] = 0;
        }
    }
}

要打印输出,您应该只在$ouput上迭代

这将是内部的数组:

代码语言:javascript
复制
(
 [t1] => Array
    (
        [s1-1] => 1
        [s1-2] => 1
        [s2-1] => 1
        [s2-2] => 1
        [s3-1] => 0
        [s3-2] => 0
    )

[t2] => Array
    (
        [s1-1] => 1
        [s2-1] => 2
        [s2-2] => 1
        [s1-2] => 0
        [s3-1] => 0
        [s3-2] => 0
    )

[t3] => Array
    (
        [s1-1] => 1
        [s2-1] => 1
        [s3-1] => 1
        [s3-2] => 3
        [s1-2] => 0
        [s2-2] => 0
    )

)

它还有其他方法来实现上面的内容,比如跳过填补空白的步骤,然后动态地执行,.

页面中显示结果的最简单的方法是嵌入一个php脚本来迭代关联数组并组成表(我鼓励您学习和研究MVC,将逻辑从视图中分离出来)

代码语言:javascript
复制
<!DOCTYPE html>
<?php
// arrays of arrays, I don't know how you receive the data
$arrays = [
    ['t1-s1-1=1', 't1-s1-2=1', 't1-s2-1=1', 't1-s2-2=1'],
    ['t2-s1-1=1', 't2-s2-1=2', 't2-s2-2=1'],
    ['t3-s1-1=1', 't3-s2-1=1', 't3-s3-1=1', 't3-s3-2=3']
];

// bi-dimensional array
$output = [];

// it will store all columns you find in the $arrays entry
$allColumns = [];

// iterate for every array you receive, i.e. ['t1-s1-1=1', 't1-s1-2=1', 't1-s2-1=1', 't1-s2-2=1']
foreach ($arrays as $array) {
    // iterate over every element in the array: 't1-s1-1=1', 't1-s1-2=1', 't1-s2-1=1' and 't1-s2-2=1'
    foreach ($array as $item) {
        // extract the parts on every element: $matches is an array containing the different parts
        preg_match('/^(t\d+)-(s\d+-\d+)=(\d+)/', $item, $matches);
        /**
         * $matches[0] would contains the element if matched: 't1-s1-1=1'
         * $matches[1] would contains 't1' if matched
         * $matches[2] would contains 's1-1' if matched
         * $matches[2] would contains 1 (integer) if matched
         */
        if (!empty($matches)) {
            $output[$matches[1]][$matches[2]] = $matches[3];
            $allColumns[] = $matches[2];
        }
    }
}

// clean duplicates
$allColumns = array_unique($allColumns);

// sort values alphabetically
sort($allColumns);

// iterate over the just created bi-dimensional array
foreach ($output as $row => $columns) {
    // iterate for all columns collected before
    foreach ($allColumns as $column) {
        // if one of column in 'allColumns' doesn't exit in $output you added in the correct place adding a zero value
        if (!in_array($column, array_keys($columns))) {
            $output[$row][$column] = 0;
        }
    }
}
?>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Table Page</title>
</head>
<body>

<table>
    <thead>
    <?php
        echo '<tr><th>Test</th>';
        foreach ($allColumns as $head) {
            echo sprintf('<th>%s</th>', $head);
        }
    echo '</tr>';
    ?>
    </thead>
    <tbody>
    <?php
        foreach ($output as $key => $columns) {
            echo sprintf('<tr><td>%s</td>', $key);
            foreach ($columns as $column) {
                echo sprintf('<td>%s</td>', $column);
            }
            echo '</tr>';
        }
    ?>

    </tbody>
</table>
</body>
</html>
票数 0
EN

Stack Overflow用户

发布于 2018-02-01 22:37:19

尝试以下几点:

代码语言:javascript
复制
$final_array = array();
$temp_array = array();
foreach ($t1 as $t) {
  $isin = 0;
  $expression = substr($t, 0, strpos($t, "="));
  $expression = str_replace("t1-", "" , $expression)
  $value = substr($t, strpos($t, "=") + 1);
  for ($i = 0; $i < 3; $i++) {
    foreach ($x = 0; $x < 3; $x++) {
      if ($expression == "s{$i}-{$x}") {
         $isin = 1;
         array_push($temp_array, $value);
      }      
    }
  }
  if ($isin == 0) { array_push($temp_array, "X"); }
}
array_push($final_array, $temp_array);

这不是一个很好的解决方案,因为您选择以一种非常奇怪的方式这样做,但是您应该了解如何从这个示例中获得您想要的东西的要点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48572636

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档