首頁 >後端開發 >php教程 >二维数组转化

二维数组转化

WBOY
WBOY原創
2016-06-06 20:28:431173瀏覽

<code>[x] => Array
    (
        [0] => 2015-08-26
        [1] => 2015-08-27
        [2] => 2015-08-28
        [3] => 2015-08-29
        [4] => 2015-08-30
        [5] => 2015-08-31
        [6] => 2015-09-01
        [7] => 2015-09-02
    )

[data] => Array
    (
        [0] => 0
        [1] => 3
        [2] => 1
        [3] => 0
        [4] => 0
        [5] => 0
        [6] => 3
        [7] => 0
    )</code>

将数组[x]和[data]变化成一个新的二维数组

<code>[0] => Array
    (
        [0] => 2015-08-26
        [1] => 0
    )
[1] => Array
    (
        [0] => 2015-08-27
        [1] => 3
    )
[2] => Array
    (
        [0] => 2015-08-28
        [1] => 1
    )
依此类推,把全部的数据转化成下面数组格式
    </code>

回复内容:

<code>[x] => Array
    (
        [0] => 2015-08-26
        [1] => 2015-08-27
        [2] => 2015-08-28
        [3] => 2015-08-29
        [4] => 2015-08-30
        [5] => 2015-08-31
        [6] => 2015-09-01
        [7] => 2015-09-02
    )

[data] => Array
    (
        [0] => 0
        [1] => 3
        [2] => 1
        [3] => 0
        [4] => 0
        [5] => 0
        [6] => 3
        [7] => 0
    )</code>

将数组[x]和[data]变化成一个新的二维数组

<code>[0] => Array
    (
        [0] => 2015-08-26
        [1] => 0
    )
[1] => Array
    (
        [0] => 2015-08-27
        [1] => 3
    )
[2] => Array
    (
        [0] => 2015-08-28
        [1] => 1
    )
依此类推,把全部的数据转化成下面数组格式
    </code>

<code>header('Content-Type: text/plain; charset=utf-8');
$arr = array(
    'x'    => array('2015-08-26', '2015-08-27', '2015-08-28'),
    'data' => array(0, 3, 1)
);
$new = array();
$i = 0;
foreach($arr as $k => $v) {
    foreach($v as $k1 => $v1) {
        //$new[$k1][$k] = $v1;
        $new[$k1][$i] = $v1;
    }
    $i++;
}
print_r($new);
Array
(
    [0] => Array
        (
            [0] => 2015-08-26
            [1] => 0
        )

    [1] => Array
        (
            [0] => 2015-08-27
            [1] => 3
        )

    [2] => Array
        (
            [0] => 2015-08-28
            [1] => 1
        )

)</code>

<code>array_map(function ($v, $v1) {
    return array($v, $v1);
}, [x], [data]);</code>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn