简体   繁体   中英

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

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

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. 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.

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. Looks like this: http://www.daregame.net/array.png

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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