简体   繁体   English

如何在php中打印数组中的输出?

[英]How to print output in array in php?

I created one hashmap array.I used foreach loop to print output.Here it prints dog@ant dogant antdog我创建了一个 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

But I want output inside array like ['dog@ant','dogant','antdog'] .How to get value inside array in php?但我想要像['dog@ant','dogant','antdog']这样的数组内部输出。如何在 php 中获取数组内部的值?

Instead of printing the values -而不是打印值 -

echo $str . "\n";

Save it to an array将其保存到数组

$ordersNewArray[] = $str;

So it would be something like -所以它会是这样的 -

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

Nah, my earlier answer isn't good enough... strtr() is all the magic you need.不,我之前的回答不够好​​...... strtr()就是你需要的全部魔法。 http://php.net/manual/en/function.strtr.php http://php.net/manual/en/function.strtr.php

Code: ( 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);

Output:输出:

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

You don't need to split your input strings, php allows you to traverse a string and access each character by its offset.您不需要拆分输入字符串,php 允许您遍历字符串并按其偏移量访问每个字符。

Code: ( 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);

Output:输出:

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

If it happens an element in any array is so filled you don't see its name anymore, just hoover its rectangle and name/value will appear in html title.如果发生任何数组中的元素被填满,您将看不到其名称,只需悬停其矩形,名称/值将出现在 html 标题中。 Looks like this: http://www.daregame.net/array.png看起来像这样: http : //www.daregame.net/array.png

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

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