PHP打印出没有方括号的数组

时间:2022-06-09 02:56:23

I want to print an array without printing the square brackets and the word "Array", for example if I do

我想打印一个数组而不打印方括号和单词“数组”,例如,如果我这样做

print_r($Array);

I will get this:

我会得到这个:

Array ( [0] => Example0 [1] => Example1) 

How can I get this?

我怎么能得到这个?

Example0
Example1

5 个解决方案

#1


2  

Any of these ways should work just fine.

任何这些方法应该工作得很好。

// First way
print_r(implode("<br>", $your_array));

// Second way
for ($i = 0; $i < count($your_array); $i++) { 
    print_r($your_array[$i]);
    echo "<br>";
}

// Third way
foreach ($your_array as $value) {
    print_r($value);
    echo "<br>";
}

The first method works for one-dimensional arrays only. If you have multidimensional arrays, you need to use for loops and to check whether the current element is an array or not and recursively enter into more for loops in order to print out all the data.

第一种方法仅适用于一维数组。如果你有多维数组,你需要使用for循环并检查当前元素是否是一个数组,并递归地输入更多for循环以打印出所有数据。

#2


2  

You can do it in this way:

你可以这样做:

function print_array ($array) {
    foreach ($array as $key => $value) {
       if (is_array ($value)) {
           print_array ($value);
       } else {
           echo ($value."<br />");
       }
    }
}

#3


2  

You could use array walk recursive

您可以使用数组遍历递归

$array = ['Example0','Example1', ['Example2']];

array_walk_recursive($array,function($item,$key){echo"$item\n";});
// tip use <br> instead of \n for HTML

Outputs

输出

Example0
Example1
Example2

See it online

在线查看

array_walk_recursive — Apply a user function recursively to every member of an array

array_walk_recursive - 递归地将用户函数应用于数组的每个成员

So this will seamlessly handle multi-dimensional arrays, as my example shows.

因此,这将无缝地处理多维数组,如我的示例所示。

#4


1  

If I understood correctly, you want to print the values for each key. You can use

如果我理解正确,您想要打印每个键的值。您可以使用

foreach ($Array as $value) {
    print_r($value);
    echo "\n";
}

This will result in

这将导致

Example0
Example1

#5


1  

foreach($Array as $key) {
  echo $key.", ";
}

#1


2  

Any of these ways should work just fine.

任何这些方法应该工作得很好。

// First way
print_r(implode("<br>", $your_array));

// Second way
for ($i = 0; $i < count($your_array); $i++) { 
    print_r($your_array[$i]);
    echo "<br>";
}

// Third way
foreach ($your_array as $value) {
    print_r($value);
    echo "<br>";
}

The first method works for one-dimensional arrays only. If you have multidimensional arrays, you need to use for loops and to check whether the current element is an array or not and recursively enter into more for loops in order to print out all the data.

第一种方法仅适用于一维数组。如果你有多维数组,你需要使用for循环并检查当前元素是否是一个数组,并递归地输入更多for循环以打印出所有数据。

#2


2  

You can do it in this way:

你可以这样做:

function print_array ($array) {
    foreach ($array as $key => $value) {
       if (is_array ($value)) {
           print_array ($value);
       } else {
           echo ($value."<br />");
       }
    }
}

#3


2  

You could use array walk recursive

您可以使用数组遍历递归

$array = ['Example0','Example1', ['Example2']];

array_walk_recursive($array,function($item,$key){echo"$item\n";});
// tip use <br> instead of \n for HTML

Outputs

输出

Example0
Example1
Example2

See it online

在线查看

array_walk_recursive — Apply a user function recursively to every member of an array

array_walk_recursive - 递归地将用户函数应用于数组的每个成员

So this will seamlessly handle multi-dimensional arrays, as my example shows.

因此,这将无缝地处理多维数组,如我的示例所示。

#4


1  

If I understood correctly, you want to print the values for each key. You can use

如果我理解正确,您想要打印每个键的值。您可以使用

foreach ($Array as $value) {
    print_r($value);
    echo "\n";
}

This will result in

这将导致

Example0
Example1

#5


1  

foreach($Array as $key) {
  echo $key.", ";
}