简体   繁体   English

PHP表创建(多维数组)

[英]PHP Table Creation (Multidimensional Arrays)

I have 4 arrays the first being the table headings for which there are six: 我有4个数组,第一个是表标题,该数组有六个数组:

array(6) {
    [0]=> string(3) "Who",
    [1]=> string(4) "What",
    [2]=> string(4) "When",
    [3]=> string(5) "Where",
    [4]=> string(3) "Why",
    [5]=> string(3) "How"
}

and 3 other arrays that contain coinciding values. 以及其他三个包含一致值的数组。 What I want to do is take the values of the first array is to make them the keys of another array with a value or array like so: 我想做的是将第一个数组的值设为具有值或array的另一个数组的键,如下所示:

array(6) {
    ["Who"]=> array(0) {},
    ["What"]=> array(0) {},
    ["When"]=> array(0) {},
    ["Where"]=> array(0) {},
    ["Why"]=> array(0) {},
    ["How"]=> array(0) {}
}

and then populate those arrays with the coinciding values much like a table. 然后使用与表类似的一致值填充这些数组。 An example of one of the other arrays would be: 其他数组之一的示例是:

array(6) {
    [0]=> string(3) "red",
    [1]=> string(4) "blue",
    [2]=> string(4) "green",
    [3]=> string(6) "yellow",
    [4]=> string(3) "black",
    [5]=> string(3) "white"
}

For the sake of simplicity I am going to say that all 3 arrays have the exact same value as the one above. 为了简单起见,我要说的是所有3个数组的值都与上述数组完全相同。

In the end I want the resulting array to be: 最后,我希望结果数组为:

array(6) {
    ["Who"]=> array(3) {
        [0]=> string(3) "red",
        [1]=> string(3) "red",
        [2]=> string(3) "red"
    },
    ["What"]=> array(3) {
        [0]=> string(4) "blue",
        [1]=> string(4) "blue",
        [2]=> string(4) "blue"
    },
    ["When"]=> array(3) {
        [0]=> string(5) "green",
        [1]=> string(5) "green",
        [2]=> string(5) "green"
    },
    ["Where"]=> array(3) {
        [0]=> string(6) "yellow",
        [1]=> string(6) "yellow",
        [2]=> string(6) "yellow"
    },
    ["Why"]=> array(3) {
        [0]=> string(5) "black",
        [1]=> string(5) "black",
        [2]=> string(5) "black"
    },
    ["How"]=> array(3) {
        [0]=> string(5) "white",
        [1]=> string(5) "white",
        [2]=> string(5) "white"
    }
}

The code I am currently working with is as follows: 我当前正在使用的代码如下:

... //Tokens are generated above (this is all in a loop)
foreach ($tokens as $token) {
    if ($i == 0) {
        $table[$token] = array();
    } else {
        foreach ($table as $col) {
            $table[$col][$i] = $token;
        }
    }
}
$i = $i + 1;

All this generates is array(0) {} 所有生成的都是array(0) {}

It'd be appreciated if someone could point out a flaw in my logic. 如果有人可以指出我的逻辑缺陷,将不胜感激。

EDIT: 编辑:

Here is what the table will look like: 该表如下所示:

_________________________________________
| Who | What | When | Where | Why | How |
-----------------------------------------
| red | blue | green| yellow|black|white|
-----------------------------------------
| red | blue | green| yellow|black|white|
-----------------------------------------
| red | blue | green| yellow|black|white|
-----------------------------------------

I've sort of come to a conclusion for your problem and it's shown below: 我为您的问题得出了一个结论,如下所示:

This is just to build the arrays similar to yours 这只是建立与您相似的数组

$one = array(
        0 => 'Who',
        1 => 'What',
        2 => 'When',
        3 => 'Where',
        4 => 'Why',
        5 => 'How'
    );



    $der = array();
    for ($i = 0; $i < 3; $i++) {
        $array = array(
            0 => 'red',
            1 => 'blue',
            2 => 'green',
            3 => 'yellow',
            4 => 'black',
            5 => 'white'
        );
        array_push($der, $array);
    }

I've basically created a multidimensional array( $der ) instead of the 3 seperate arrays of values that you have to complete this task. 我基本上已经创建了一个多维数组( $der )来代替完成此任务所需的3个单独的值数组。

You can do the same with the use of array_push() to create the new array. 您可以使用array_push()创建新的数组来做同样的事情。

Now we need to loop through our $der array while retaining the keys (table headings) from the $one array. 现在我们需要遍历$der数组,同时保留$one数组中的键(表标题)。

    $new = array();
    foreach($der as $item) {

        foreach($item as $key => $val) {
            $new[$one[$key]][] = $val;
        }

    }

Where a print_r($new) warrants a return of: 如果print_r($new)保证返回:

Array
(
    [Who] => Array
        (
            [0] => red
            [1] => red
            [2] => red
        )

    [What] => Array
        (
            [0] => blue
            [1] => blue
            [2] => blue
        )

    [When] => Array
        (
            [0] => green
            [1] => green
            [2] => green
        )

    [Where] => Array
        (
            [0] => yellow
            [1] => yellow
            [2] => yellow
        )

    [Why] => Array
        (
            [0] => black
            [1] => black
            [2] => black
        )

    [How] => Array
        (
            [0] => white
            [1] => white
            [2] => white
        )

)

As you can see in the loop, we reference the $one[$key] where $key is the current index of the array item that you're iterating through. 正如您在循环中看到的那样,我们引用$one[$key] ,其中$key是您要遍历的数组项的当前索引。


Alternatively 或者

You could do something like this ( NOTE : The $one below is the same as the $one above ): 你可以做这样的事情( $one下面是一样的$one以上 ):

$values = array();
foreach ($der as $item) {

    foreach ($item as $key => $val) {
        $values[$key][] = $val;
    }
}

Now harnessing array_combine() you could simply do: 现在利用array_combine()您可以简单地做到:

$new = array_combine($one, $values);

Which will warrant the same output as above. 这将保证与上述相同的输出。

try array_fill_keys() function 尝试array_fill_keys()函数

$tokens= array('Who', 'What', 'When', 'Where', 'Why', 'How');
$table = array('red', 'blue', 'green', 'yellow','black', 'white');
$a = array_fill_keys($tokens, $table);
print_r($a);                

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM