2

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?

0

4 Answers 4

5

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);
Sign up to request clarification or add additional context in comments.

6 Comments

I did as u suggested but it doesnt store value as ['dog@ant','dogant','antdog'].
Do you want the literal string ['dog@ant','dogant','antdog'] or do you want $array = ['dog@ant','dogant','antdog']? What I did does the latter, where they are saved to an array, similar to $orders.
When I did print_r ($ordersNewArray);, it gave output as Array ( [0] => dog@ant ) Array ( [0] => dog@ant [1] => dogant ) Array ( [0] => dog@ant [1] => dogant [2] => antdog )
You have the print_r() inside the loop. You need to put it after your loop ends.
Thanks It is solved,I put print_r() outside the loop and got output as expected.
|
3

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

Comments

0

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

Comments

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

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

1 Comment

This works with multidimensional arrays and will let you format the table upon a few parameters : Not only does it work well, it's short and sweet too :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.