brown26 brown26 - 3 months ago 9
PHP Question

how to print out array elements than using print_r();?

I want to print out array elements from my custom function. Here is the fucntion :

$input = $_POST["input"];

function preprocessing($input){
$input = trim(strtolower($input));
$remove = '/[^a-zA-Z0-9]/s';
$result = preg_split($remove, $input, -1, PREG_SPLIT_NO_EMPTY);

for($i = 0; $i < count($resultl); $i++){
$result[$i] = trim($result[$i]);
}
return $result;
}


This is the input (example) : qwd qwd qwdqd123 13#$%^&*) ADDA ''''

I use print_r(preprocessing($input)); to print the array contents.

Output : Array ( [0] => qwd [1] => qwd [2] => qwdqd123 [3] => 13 [4] => adda )

Is there any method to customize the output ?
I want to make the output look like this (my expectation) :

1 qwd

2 qwd

3 qwdqd123

4 13

5 adda


Thanks before.

Answer
function printArray($result) {
    foreach($result as $key => $value) {
        echo ($key + 1), ' ', $value, PHP_EOL;
    }
}