Home >Backend Development >PHP Tutorial >thinkPHP数组变形的问题

thinkPHP数组变形的问题

WBOY
WBOYOriginal
2016-06-06 20:51:381244browse

从前 有这样一个数组

Array
(
    [id] => Array
        (
            [0] => 500002-016
            [1] => 500471-012
        )

    [name] => Array
        (
            [0] => 乐普国产药物支架(国产)[限额]
            [1] => 一次性正压无针连接式留置针(国产)[乙10%]
        )

    [specification] => Array
        (
            [0] => y
            [1] => x
        )

    [quantity] => Array
        (
            [0] => 22
            [1] => 23
        )

)

想要变成这样一个数组

Array
(
    [0] => Array
        (
            [id] => 500002-016
            [name] => 乐普国产药物支架(国产)[限额]
            [specification] => y
            [quantity] => 22
        )

    [1] => Array
        (
            [id] => 500471-012
            [name] => 一次性正压无针连接式留置针(国产)[乙10%]
            [specification] => x
            [quantity] => 23
        )
)

或者请问一下ThinkPHP的高手 第一种格式的数组怎样volist到视图的表格中去 各位有什么好办法么 先谢谢啦~

回复内容:

从前 有这样一个数组

Array
(
    [id] => Array
        (
            [0] => 500002-016
            [1] => 500471-012
        )

    [name] => Array
        (
            [0] => 乐普国产药物支架(国产)[限额]
            [1] => 一次性正压无针连接式留置针(国产)[乙10%]
        )

    [specification] => Array
        (
            [0] => y
            [1] => x
        )

    [quantity] => Array
        (
            [0] => 22
            [1] => 23
        )

)

想要变成这样一个数组

Array
(
    [0] => Array
        (
            [id] => 500002-016
            [name] => 乐普国产药物支架(国产)[限额]
            [specification] => y
            [quantity] => 22
        )

    [1] => Array
        (
            [id] => 500471-012
            [name] => 一次性正压无针连接式留置针(国产)[乙10%]
            [specification] => x
            [quantity] => 23
        )
)

或者请问一下ThinkPHP的高手 第一种格式的数组怎样volist到视图的表格中去 各位有什么好办法么 先谢谢啦~

易理解的话,我会写成这样。

function rebuild($data)
{
	$result = array();
	$keys = array_keys($data);
	$num = count($data['id']);

	for ($i = 0; $i 
                            
            <p class="answer fmt" data-id="1020000000132158">
                                    </p><p>这个。。。我想到的唯一办法也就是foreach了</p><pre class="brush:php;toolbar:false">function arr_format($arr) {
    $res = array ();
    foreach($arr as $k => $v) {
        foreach ($v as $kk => $vv) {
            $res[$kk][$k] = $vv;
        }
    }

    return $res;
}

$data=你的数据;
$temp = array();//保存改变后的数据
foreach($data['id'] as $key=>$val){
    array_push($temp,array(
                              'id'=>$val,
                              'name'=>$data['name'][$key],
                              'specification'=>$data['specification'][$key],
                              'quantity'=>$data['quantity'][$key]
                          )
              );
}
unset($data);

来个不一样的,如果字段固定的话。

function arr_format($arr) {
    $result = array();
    list($id, $name, $specification, $quantity) = array_values($arr);
    for ($i = 0,$count = count($id);$i  $id[$i],
            'name' => $name[$i],
            'specification' => $specification[$i],
            'quantity' => $quantity[$i]
        );
    }
    return $result;
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn