如何在php中以数组形式输出输出?

时间:2018-12-19 05:06:54

标签: php arrays

我创建了一个hashmap数组。我使用了foreach循环来打印输出。这里它打印dog@ant dogant 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

但是我想要在['dog@ant','dogant','antdog']之类的数组内输出。如何在php中获取数组内的值?

4 个答案:

答案 0 :(得分:5)

代替打印值-

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);

答案 1 :(得分:3)

不,我先前的回答还不够好……strtr()是您所需要的全部魔术。 http://php.net/manual/en/function.strtr.php

代码:(Demo

$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',
)

答案 2 :(得分:0)

您无需拆分输入字符串,php允许您遍历字符串并按其偏移量访问每个字符。

代码:(Demo

$rule = [
    "c" => "d",
    "a" => "o",
    "t" => "g",
    "h" => "a",
    "1" => "@",
    "e" => "n",
    "n" => "t"
];
$orders = ['cat1hen','cathen','hencat'];

foreach($orders as $i => $order){
    $result[$i] = '';  // allow string concatenation
    for ($offset = 0, $length = strlen($order); $offset < $length; ++$offset) {
        $result[$i] .= $rule[$order[$offset]];
        //                   ^^^^^^^^^^^^^^^- e.g. "c" from cat1hen
    }
}
var_export($result);

输出:

array (
  0 => 'dog@ant',
  1 => 'dogant',
  2 => 'antdog',
)

答案 3 :(得分:0)

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>";
}

如果发生这种情况,则任何数组中的元素都已填充,您将不再看到其名称,只需将鼠标悬停在其矩形上,名称/值将显示在html标题中。看起来像这样:http://www.daregame.net/array.png