dongmi19720408 2016-08-27 17:58
浏览 23
已采纳

如何打印数组元素比使用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 looks like this (my expectation) :
1 qwd
2 qwd
3 qwdqd123
4 13
5 adda

Thanks before.

  • 写回答

1条回答 默认 最新

  • dongpin3794 2016-08-27 18:02
    关注
    function printArray($result) {
        foreach($result as $key => $value) {
            echo ($key + 1), ' ', $value, PHP_EOL;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库