分组返回array_chunk [英] return array_chunk in groups

查看:56
本文介绍了分组返回array_chunk的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常可以对基本数组做得相当不错,但是这使我的头脑与墙壁不符。

I can usually do pretty decent with basic arrays, but this one has put my head vs the wall.

我正在尝试传递一些信息(用于菜单) )通过函数返回,并以格式化的方式返回。

I am trying to pass some information (for a menu) through a function, and return it in a formatted fashion.

我想要的最终结果是发送一些这样的信息。如果我有很多字段,我需要能够重复该数组直到它为空

My desired end result is to send some information like this. I need to be able to repeat the array until it is empty in the event that I have a number of fields

$Sort = array('imgup.jpg','imagedn.jpg','Name','imgx.jpg','imagy.jpg','Name4');
NewSortBox($Sort);

,最终结果将返回,如

<div>Name <img src='imgup.jpg'><img src='imgdn.jpg'></div>
<div>Name4 <img src='imgx.jpg'><img src='imgy.jpg'></div>

我已经知道必须使用Array_Chunk函数来破坏数组,但是我不是能够弄清楚如何使其正确使用foreach或loop函数。

I have figured out that I have to use the Array_Chunk function to break the array, but I am not able to figure out how to make it properly use the foreach or loop functions.

function NewSortBox(&$array){

$newArray = array_chunk($array, 3, false);
$i = 0;
foreach ($newArray as $inner_array) {
    $i++;
    echo "<div>";
    while (list($key, $value) = each($inner_array)) {    
        echo "$key: $value"; 
        // Here is where I am totally lost, I want to acheive something like ??
        // echo "$value[1] <img src='$value[2]'><img src='$value[3]'>";
    }
    echo "</div>";
}


推荐答案

像这样的事情可能有助于得到期望的结果:

Something like this may help to get the desired result:

    $newArray = array_chunk($Sort, 3, false);

    foreach ($newArray as $inner_array) {
echo "<div>";
    list($a, $b, $c) = $inner_array;
echo $c.":".$b.":".$a; //arrange the variables as required
echo "</div>";
    }

这篇关于分组返回array_chunk的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆