php 将数组名称与另一个数组的值匹配
php matching array names to another array's values
我有一些可用数组形式的数据。我必须在行和列中显示数据,如 table。
第一个数组 $header 如下所示:
$header[0]=a, $header[1]=d, $header[2]=b, $header[3]=c, $header[4]=e
这个数组可以有任意数量的元素。随后的数组代表我的 table 的行。他们来找我是这样的,可以有任意数量的行:
$b[0]=3,$c[0]=4,$a[0]=1,$d[0]=2,$e[0]=5 (in random order)
$c[1]=4,$e[1]=5,$a[1]=1,$b[1]=3,$d[1]=2 (in random order)
$a[2]=5,$b[2]=15,$c[2]=20,$e[2]=25 (in random order and missing an element)
$a[3]=10,$b[3]=30,$c[3]=40,$bananas[3]=25 (random, missing and extra)
$c[4]=4,$e[4]=5,$c[4]=1,$b[4]=3,$d[4]=2,$a[4]=1 (random and duplicate)
我想确定每一行中数组的名称,并重新排序它们,以便它们根据 header 数组中的值出现在列中,这样 header 数组值与行数组名称匹配,并且行数组值放置在正确的列中。为了说明,上面的数据将产生:
a,d,b,c,e
1,2,3,4,5
1,2,3,4,5
5,,15,20,25
10,,30,40,
1,2,3,5,5
规则:
- header - 按照索引
确定的顺序
- 随机 - 重新排序以匹配 header 顺序
- 缺少 - 在该列中显示空白或空值
- 额外 - 忽略
- 重复 - 如果是数字,则添加,如果是 alpha,则与 ~
连接
另一个例子:
$header[0]=apple, $header[1]=banana, $header[3]=grape, $header4=fruit
$banana[0]=yellow, $grape[0]=purple, $apple[0]=red, $fruit[0]=fresh
$grape[1]=cluster, $fruit[1]=bag, $apple[1]=bushel, $banana[1]=bunch
$grape[2]=cluster, $fruit[2]=bag, $banana[2]=bunch, $fruit[2]=crunchy, $extra[2]=new
会产生
apple, banana, grape, fruit
red, yellow, purple, fresh
bushel, bunch, cluster, bag
, bunch, cluster, bag~crunchy
我会 post 一些代码,但我不知道如何开始
所以这主要是工作,我不明白的部分是你如何确定重复项,因为当你设置 $fruit[2]='bag';
然后设置 $fruit[2]='crunchy';
它实际上会被覆盖所以它就好像 $fruit[2]='bag';
从未执行过
sample code
$header[0]= 'apple'; $header[1]='banana'; $header[3]='grape'; $header[4]='fruit';
$banana[0]='yellow'; $grape[0]='purple'; $apple[0]='red'; $fruit[0]='fresh';
$grape[1]='cluster'; $fruit[1]='bag'; $apple[1]='bushel'; $banana[1]='bunch';
$grape[2]='cluster'; $fruit[2]='bag'; $banana[2]='bunch'; $fruit[2]='crunchy'; $extra[2]='new';
$rows = [];
$maxRows = 0;
$headerSize = sizeOf($header);
$z = 0;
foreach($header as $index => $name) {
$rows[0][] = $name;
if (isset($$name)) {
$x = 0;
foreach($$name as $index2 => $name2) {
++$x;
if (false === isset($rows[$x])) {
for($y = 0; $y < $headerSize; $y++) {
$rows[$x][$y] = null;
}
}
if (isset($rows[$x][$z])) {
$rows[$x][$z] .= '~' . $name2;
} else {
$rows[$x][$z] = $name2;
}
}
}
$z++;
}
foreach($rows as $row) {
echo implode(', ', $row) . PHP_EOL;
}
output
apple, banana, grape, fruit
red, yellow, purple, fresh
bushel, bunch, cluster, bag
, bunch, cluster, crunchy
我有一些可用数组形式的数据。我必须在行和列中显示数据,如 table。 第一个数组 $header 如下所示:
$header[0]=a, $header[1]=d, $header[2]=b, $header[3]=c, $header[4]=e
这个数组可以有任意数量的元素。随后的数组代表我的 table 的行。他们来找我是这样的,可以有任意数量的行:
$b[0]=3,$c[0]=4,$a[0]=1,$d[0]=2,$e[0]=5 (in random order)
$c[1]=4,$e[1]=5,$a[1]=1,$b[1]=3,$d[1]=2 (in random order)
$a[2]=5,$b[2]=15,$c[2]=20,$e[2]=25 (in random order and missing an element)
$a[3]=10,$b[3]=30,$c[3]=40,$bananas[3]=25 (random, missing and extra)
$c[4]=4,$e[4]=5,$c[4]=1,$b[4]=3,$d[4]=2,$a[4]=1 (random and duplicate)
我想确定每一行中数组的名称,并重新排序它们,以便它们根据 header 数组中的值出现在列中,这样 header 数组值与行数组名称匹配,并且行数组值放置在正确的列中。为了说明,上面的数据将产生:
a,d,b,c,e
1,2,3,4,5
1,2,3,4,5
5,,15,20,25
10,,30,40,
1,2,3,5,5
规则:
- header - 按照索引 确定的顺序
- 随机 - 重新排序以匹配 header 顺序
- 缺少 - 在该列中显示空白或空值
- 额外 - 忽略
- 重复 - 如果是数字,则添加,如果是 alpha,则与 ~ 连接
另一个例子:
$header[0]=apple, $header[1]=banana, $header[3]=grape, $header4=fruit
$banana[0]=yellow, $grape[0]=purple, $apple[0]=red, $fruit[0]=fresh
$grape[1]=cluster, $fruit[1]=bag, $apple[1]=bushel, $banana[1]=bunch
$grape[2]=cluster, $fruit[2]=bag, $banana[2]=bunch, $fruit[2]=crunchy, $extra[2]=new
会产生
apple, banana, grape, fruit
red, yellow, purple, fresh
bushel, bunch, cluster, bag
, bunch, cluster, bag~crunchy
我会 post 一些代码,但我不知道如何开始
所以这主要是工作,我不明白的部分是你如何确定重复项,因为当你设置 $fruit[2]='bag';
然后设置 $fruit[2]='crunchy';
它实际上会被覆盖所以它就好像 $fruit[2]='bag';
从未执行过
sample code
$header[0]= 'apple'; $header[1]='banana'; $header[3]='grape'; $header[4]='fruit';
$banana[0]='yellow'; $grape[0]='purple'; $apple[0]='red'; $fruit[0]='fresh';
$grape[1]='cluster'; $fruit[1]='bag'; $apple[1]='bushel'; $banana[1]='bunch';
$grape[2]='cluster'; $fruit[2]='bag'; $banana[2]='bunch'; $fruit[2]='crunchy'; $extra[2]='new';
$rows = [];
$maxRows = 0;
$headerSize = sizeOf($header);
$z = 0;
foreach($header as $index => $name) {
$rows[0][] = $name;
if (isset($$name)) {
$x = 0;
foreach($$name as $index2 => $name2) {
++$x;
if (false === isset($rows[$x])) {
for($y = 0; $y < $headerSize; $y++) {
$rows[$x][$y] = null;
}
}
if (isset($rows[$x][$z])) {
$rows[$x][$z] .= '~' . $name2;
} else {
$rows[$x][$z] = $name2;
}
}
}
$z++;
}
foreach($rows as $row) {
echo implode(', ', $row) . PHP_EOL;
}
output
apple, banana, grape, fruit
red, yellow, purple, fresh
bushel, bunch, cluster, bag
, bunch, cluster, crunchy