如何在php中以数组形式打印输出?
我创建了一个hashmap数组。我使用foreach循环打印输出。这里它打印如何在php中以数组形式打印输出?,php,arrays,Php,Arrays,我创建了一个hashmap数组。我使用foreach循环打印输出。这里它打印dog@antdogant antdog <?php $rule = [ "c" => "d", "a" => "o", "t" => "g", "h" => "a", "1" => "@", "e" => "n", "n" => "t" ]; $orders = ['cat1hen','cathen','hencat']; foreach($orders as $or
dog@antdogant antdog
<?php
$rule =
[
"c" => "d",
"a" => "o",
"t" => "g",
"h" => "a",
"1" => "@",
"e" => "n",
"n" => "t"
];
$orders = ['cat1hen','cathen','hencat'];
foreach($orders as $order){
$arr = str_split($order);
$str ="";
foreach($arr as $key){
$str .= $rule[$key];
}
echo $str . "\n";
}
// dog@ant dogant antdog
而不是打印值-
echo $str . "\n";
将其保存到数组中
$ordersNewArray[] = $str;
所以会是这样的-
<?php
$rule =
[
"c" => "d",
"a" => "o",
"t" => "g",
"h" => "a",
"1" => "@",
"e" => "n",
"n" => "t"
];
$orders = ['cat1hen','cathen','hencat'];
//Create new Array
$ordersNewArray = [];
foreach($orders as $order){
$arr = str_split($order);
$str ="";
foreach($arr as $key){
$str .= $rule[$key];
}
// Save to new Array
$ordersNewArray[] = $str;
}
//IF you want to verify, notice this is **after** the loop is done
print_r($ordersNewArray);
您不需要拆分输入字符串,php允许您遍历字符串并按其偏移量访问每个字符
代码:()
不,我之前的回答不够好strtr()
就是您所需要的全部魔力
代码:()
输出:
array (
0 => 'dog@ant',
1 => 'dogant',
2 => 'antdog',
)
array (
0 => 'dog@ant',
1 => 'dogant',
2 => 'antdog',
)
函数将数组显示为表格($array、$key\u color='blue'、$value\u color=“black”、$fontfamine=“arial”、$fontsize=“12pt”、$position=”、$zindex=0){
回声“;
foreach($key=>$value的数组){
回声“;
//关键单元
回显“{$key}”;
//值单元格(如果值是数组,则将包含一个表)
回声“;
if(是_数组($value)){
$array\u contents=计数($value);
如果($array\u contents>0){
将_数组_显示为_表($value、$key_-color、$value_-color、$fontfamily、$fontsize、“继承”、$zindex);
}否则{
回声“Array()”;
}
}否则{
如果($value==“”){
回显“空字符串”;
}否则{
回显“{$value}”;
}
}
回声“;
回声“;
}
回声“;
}
如果任何数组中的一个元素被填充得如此之满,以至于你再也看不到它的名称,那么它的矩形和名称/值将显示在html标题中。看起来是这样的:我按照u的建议做了,但它没有将值存储为['dog@ant“,'dogant','antdog']
。是否要文本字符串['dog@ant“,'dogant','antdog']
还是要$array=['dog@ant“,'dogant','antdog']
?我所做的是后者,它们被保存到一个数组中,类似于$orders
代码>,它以数组([0]=>dog@ant)数组([0]=>dog@ant[1]=>dogant)数组([0]=>dog@ant[1]=>dogant[2]=>antdog)
循环中有print\r()
。您需要在循环结束后放置它。感谢它被解决,我将print\r()
放在循环外部,并按预期获得输出。这与多维数组一起工作,并允许您根据几个参数格式化表:它不仅工作得很好,而且很短且很好:)
$rule = [
"c" => "d",
"a" => "o",
"t" => "g",
"h" => "a",
"1" => "@",
"e" => "n",
"n" => "t"
];
$orders = ['cat1hen','cathen','hencat'];
foreach($orders as $order){
$result[] = strtr($order, $rule);
}
var_export($result);
array (
0 => 'dog@ant',
1 => 'dogant',
2 => 'antdog',
)
function show_array_as_table($array,$key_color='blue',$value_color="black",$fontfamily="arial",$fontsize="12pt",$position="",$zindex=0) {
echo "<table style='position:{$position}; z-index:{$zindex}; color:{$color}; font-family:{$fontfamily}; font-size:{$fontsize}'>";
foreach($array as $key => $value) {
echo "<tr>";
// key cell
echo "<td title=\"{$key}\" style='position:inherit; z-index:{$zindex}; width:150pt; height:25pt; text-align:right; vertical-align:top; background-color:cccccc; color:{$key_color}'><b>{$key}</b></td>";
// value cell (will contain a table if the value is an array)
echo "<td style='position:inherit; z-index:{$zindex}; width:300pt; height:25pt; text-align:justify; vertical-align:top; color:{$value_color}'>";
if (is_array($value)) {
$array_contents = count($value);
if ($array_contents > 0) {
show_array_as_table($value,$key_color,$value_color,$fontfamily,$fontsize,"inherit",$zindex);
} else {
echo "<i>Array()</i>";
}
} else {
if ($value == "") {
echo "empty string";
} else {
echo "<i>{$value}</i>";
}
}
echo "</td>";
echo "</tr>";
}
echo "</table>";
}