如何在php中读取多维数组中的图像名称

时间:2022-03-30 21:35:57

I am having multidimensional array and In multidimensional array I am getting image/banner names with single image and images with comma seprated.

我有多维数组,在多维数组中,我得到的图像/横幅名称只有一个图像,图像用逗号分隔。

Now I want to display all image/baneer in silder so with this multidimensional array, how it will be possible with loop?

现在我想要在silder中显示所有的图像/巴纳尔那么对于这个多维数组,它怎么可能有循环呢?

Array
(
    [0] => Array
        (
            [banners] => http://www.eclipse-pos.com/eclipse/images/small_banner2.jpg
        )

    [1] => Array
        (
            [banners] => 143038313801.jpg,143038313809.jpg,143038313811.jpg
        )

    [2] => Array
        (
            [banners] => 143038306301.jpg,143038306302.jpg,143038306303.jpeg,143038306310.jpg,143038306311.jpg,143038306312.png
        )

)

I want to use all bellow image names in slider.

我想在滑动条中使用所有圆润的图像名。

http://www.eclipse-pos.com/eclipse/images/small_banner2.jpg
143038313801.jpg
143038313809.jpg
143038313811.jpg
143038306301.jpg
143038306302.jpg
143038306303.jpeg
143038306310.jpg
143038306311.jpg
143038306312.png

Any idea?

任何想法?

I have never done this way so no I idea how to do that so need your support.

我从来没有这样做过,所以我不知道该怎么做,所以需要你的支持。

What I have done:

for($i=0; $i<count($getBefamousHomepage); $i++){
    echo $getBefamousHomepage[$i]['banners'];
    echo "<br>";
}

Thanks.

谢谢。

4 个解决方案

#1


2  

This should work with your example:

这应该适用于您的示例:

for ($i=0; $i<count($getBefamousHomepage); $i++) {
    foreach (explode(',',$getBefamousHomepage[$i]['banners']) as $banner) {
        echo $banner;
        echo "<br />";
    }
}

#2


3  

This should work for you:

这应该对你有用:

Just go through your array with array_walk_recursive(), then simply explode() all values by a comma and print it implode()'ed, like this:

只需使用array_walk_recursive()遍历数组,然后用逗号将所有值爆炸,然后将其内爆()'ed,如下所示:

array_walk_recursive($arr, function($v){
    echo implode("<br>", explode(",", $v));
});

#3


0  

<?php

$given_multi_array; // Store your result into this array
$image_names = array(); // To store image names.

//loop given array $given_multi_array and store to $image_names

//循环给定数组$given_multi_array并存储到$image_names

foreach($given_multi_array as $key => $val)
{
foreach($val as $key1 => $image_name)
{
 $image_names =  $image_name;
}
}
 print_r($image_names);
?>

#4


0  

Recursion is inappropriate when you are not dealing with variable-depth data. In this case, you know exactly how much depth you are working with. In fact, you know you specifically want to target a single column of data. PHP has a dedicated function to call for this: array_column().

当不处理变深数据时,递归是不合适的。在这种情况下,您确切地知道您正在处理的深度是多少。事实上,您知道您特别想针对一列数据。PHP有一个专门的函数来调用这个函数:array_column()。

Here is a clean, direct function-based process with no unnecessary overhead:

这里是一个干净的、直接的基于功能的过程,没有不必要的开销:

  1. isolate the columnar data.
  2. 隔离柱状数据。
  3. join all elements with the same delimiter used within the elements (comma).
  4. 使用元素(逗号)中使用的相同分隔符连接所有元素。
  5. replace all commas with a <br> tag.

  6. 标签替换所有逗号。

Code: (Demo)

代码:(演示)

$array = [
    ['banners' => 'http://www.eclipse-pos.com/eclipse/images/small_banner2.jpg'],
    ['banners' => '143038313801.jpg,143038313809.jpg,143038313811.jpg'],
    ['banners' => '143038306301.jpg,143038306302.jpg,143038306303.jpeg,143038306310.jpg,143038306311.jpg,143038306312.png']
];

echo str_replace(",", "<br>", implode(",", array_column($array, 'banners')));

Output:

输出:

http://www.eclipse-pos.com/eclipse/images/small_banner2.jpg
143038313801.jpg
143038313809.jpg
143038313811.jpg
143038306301.jpg
143038306302.jpg
143038306303.jpeg
143038306310.jpg
143038306311.jpg
143038306312.png

#1


2  

This should work with your example:

这应该适用于您的示例:

for ($i=0; $i<count($getBefamousHomepage); $i++) {
    foreach (explode(',',$getBefamousHomepage[$i]['banners']) as $banner) {
        echo $banner;
        echo "<br />";
    }
}

#2


3  

This should work for you:

这应该对你有用:

Just go through your array with array_walk_recursive(), then simply explode() all values by a comma and print it implode()'ed, like this:

只需使用array_walk_recursive()遍历数组,然后用逗号将所有值爆炸,然后将其内爆()'ed,如下所示:

array_walk_recursive($arr, function($v){
    echo implode("<br>", explode(",", $v));
});

#3


0  

<?php

$given_multi_array; // Store your result into this array
$image_names = array(); // To store image names.

//loop given array $given_multi_array and store to $image_names

//循环给定数组$given_multi_array并存储到$image_names

foreach($given_multi_array as $key => $val)
{
foreach($val as $key1 => $image_name)
{
 $image_names =  $image_name;
}
}
 print_r($image_names);
?>

#4


0  

Recursion is inappropriate when you are not dealing with variable-depth data. In this case, you know exactly how much depth you are working with. In fact, you know you specifically want to target a single column of data. PHP has a dedicated function to call for this: array_column().

当不处理变深数据时,递归是不合适的。在这种情况下,您确切地知道您正在处理的深度是多少。事实上,您知道您特别想针对一列数据。PHP有一个专门的函数来调用这个函数:array_column()。

Here is a clean, direct function-based process with no unnecessary overhead:

这里是一个干净的、直接的基于功能的过程,没有不必要的开销:

  1. isolate the columnar data.
  2. 隔离柱状数据。
  3. join all elements with the same delimiter used within the elements (comma).
  4. 使用元素(逗号)中使用的相同分隔符连接所有元素。
  5. replace all commas with a <br> tag.

  6. 标签替换所有逗号。

Code: (Demo)

代码:(演示)

$array = [
    ['banners' => 'http://www.eclipse-pos.com/eclipse/images/small_banner2.jpg'],
    ['banners' => '143038313801.jpg,143038313809.jpg,143038313811.jpg'],
    ['banners' => '143038306301.jpg,143038306302.jpg,143038306303.jpeg,143038306310.jpg,143038306311.jpg,143038306312.png']
];

echo str_replace(",", "<br>", implode(",", array_column($array, 'banners')));

Output:

输出:

http://www.eclipse-pos.com/eclipse/images/small_banner2.jpg
143038313801.jpg
143038313809.jpg
143038313811.jpg
143038306301.jpg
143038306302.jpg
143038306303.jpeg
143038306310.jpg
143038306311.jpg
143038306312.png