Reputation: 81
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?
Upvotes: 2
Views: 1470
Reputation: 19
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
Upvotes: 0
Reputation: 48049
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',
)
Upvotes: 3
Reputation: 48049
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',
)
Upvotes: 0
Reputation: 12433
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);
Upvotes: 5