我想知道如何分隔下面这样的数组的值,并用变量分隔:https://prnt.sc/pi98a5
1 - 7 - Artes 1 - artes1
2 - 10 - Artes 2 - artes2
3 - 8 - Artes 3 - artes2
$array =
[
["1", "2", "3" ],
["7", "10", "8" ],
["Artes 1", "Artes 2", "Artes 3" ],
["artes1", "artes2", "artes3" ]
];
我尝试自己做,但是这让我很困惑。
foreach ($array as $key => $row){
foreach ($array[$key] as $key2 => $row2){
echo $row2; //1237108Artes 1Artes 2Artes 3artes1artes2artes3
}
}
答案 0 :(得分:2)
使用标签(取决于输出的位置),您可以implode()
数据,使用array_column()
从所有行中提取列...
for ( $i = 0; $i < count($array[0]); $i++ ) {
echo implode("\t-\t", array_column($array, $i)).PHP_EOL;
}
给予...
1 - 7 - Artes 1 - artes1
2 - 10 - Artes 2 - artes2
3 - 8 - Artes 3 - artes3
仅转换数据...
$trans = [];
for ( $i = 0; $i < count($array[0]); $i++ ) {
$trans[] = array_column($array, $i);
}
答案 1 :(得分:1)
$array =
array(
["1", "2", "3" ],
["7", "10", "8" ],
["Artes 1", "Artes 2", "Artes 3" ],
["artes1", "artes2", "artes3" ]
);
$newArray=array();
foreach ($array as $key => $value) {
foreach ($value as $key2 => $value2) {
$newArray[$key2][]=$value2;
}
}
foreach ($newArray as $key => $value) {
foreach ($value as $key2 => $value2) {
if($key2!=0)
echo " - ";
echo "$value2";
}
echo "<br>";
}
答案 2 :(得分:1)
这将帮助您:
<table>
<?php
$array = [ ["1", "2", "3" ], ["7", "10", "8" ], ["Artes 1", "Artes 2", "Artes 3" ], ["artes1", "artes2", "artes3" ] ];
foreach ($array as $key => $row){
echo "<tr>";
foreach($array as $key2=> $row2){
echo "<td>".$row2[$key]."</td>";
}
echo "</tr>";
}?>
</table>
编辑:尝试理解代码
<table>
<?php
$array = [ ["1", "2", "3" ], ["7", "10", "8" ], ["Artes 1", "Artes 2", "Artes 3" ], ["artes1", "artes2", "artes3" ] ];
$new_arr=[];
foreach ($array as $key => $row){
$var=[];
echo "<tr>";
foreach($array as $key2=> $row2){
echo "<td>".$row2[$key]."</td>";
array_push($var,$row2[$key]);
}
array_push($new_arr,$var);
echo "</tr>";
}
foreach($new_arr as $key => $row){
foreach($row as $key2=> $row2){
echo $row2;
}
}?>
</table>
这将创建$new_array
数组。尝试回显并查看相同代码的输出
答案 3 :(得分:1)
对数组进行转置,然后像Demo
那样加入每一行$array_transpose = null;
foreach($array as $k_row => $row){
foreach($row as $k_col => $v){
$array_transpose[$k_col][$k_row] = $v;
}
}
foreach($array_transpose as $row){
echo join("\t-\t",$row) . "\n";
}
答案 4 :(得分:1)
因此,基本上其他用户提供的答案是可行的,但我认为您无法理解如何将这些值作为单独的字符串放在某个地方。
$array =[
["1", "2", "3" ],
["7", "10", "8" ],
["Artes 1", "Artes 2", "Artes 3" ],
["artes1", "artes2", "artes3" ]
];
$arr = [];
$newArray=array();
foreach ($array as $key => $value) {
foreach ($value as $key2 => $value2) {
$newArray[$key2][]=$value2;
}
}
$arr = [];
foreach($newArray as $val){
// $arr[] = implode(' - ', $val);
$key = $val[0];
$arr[$key] = array(
"title" => $val[2],
"var" => $val[3],
"value" => $val[1],
);
}
print_r($arr);
// A B C D
// 1 - 7 - Artes 1 - artes1
// 2 - 10 - Artes 2 - artes2
// 3 - 8 - Artes 3 - artes3