根据数组的特定字段将给定数组拆分为新数组

时间:2017-11-23 03:43:47

标签: php arrays multidimensional-array

我有这样的数组。

$arrr = [
    95 => ['title' => 'CONTACT US', 'break' => '1', 'id' => 95],
    100 => ['title' => 'CUSTOMER SERVICE', 'break' => '1', 'id' => 100], 
    105 => ['title' => 'SHOPPING TOOLS', 'break' => '0', 'id' => 105], 
    126 => ['title' => 'COMPANY INFO1', 'break' => '0', 'id' => 126], 
    108 => ['title' => 'COMPANY INFO', 'break' => '1', 'id' => 108], 
    111 => ['title' => 'CONNECT WITH US', 'break' => '0', 'id' => 111], 
    114 => ['title' => 'OUR OTHER SITES', 'break' => '1', 'id' => 114], 
];

现在我想要这样的数组。

$arrr = [
    0 => ['title' => 'CONTACT US', 'break' => '1', 'id' => 95],
    1 => ['title' => 'CUSTOMER SERVICE', 'break' => '1', 'id' => 100], 
    2 => [
        0 => ['title' => 'SHOPPING TOOLS', 'break' => '0', 'id' => 105],
        1 => ['title' => 'COMPANY INFO1', 'break' => '0', 'id' => 126],
        2 => ['title' => 'COMPANY INFO', 'break' => '1', 'id' => 108], 
    ],
    3 => [
        0 => ['title' => 'CONNECT WITH US', 'break' => '0', 'id' => 111], 
        1 => ['title' => 'OUR OTHER SITES', 'break' => '1', 'id' => 114]
    ]
];

OR

$arrr = [
    95 => ['title' => 'CONTACT US', 'break' => '1', 'id' => 95],
    100 => ['title' => 'CUSTOMER SERVICE', 'break' => '1', 'id' => 100], 
    105 => [
        105 => ['title' => 'SHOPPING TOOLS', 'break' => '0', 'id' => 105],
        126 => ['title' => 'COMPANY INFO1', 'break' => '0', 'id' => 126],
        108 => ['title' => 'COMPANY INFO', 'break' => '1', 'id' => 108], 
    ],
    111 => [
        111 => ['title' => 'CONNECT WITH US', 'break' => '0', 'id' => 111], 
        114 => ['title' => 'OUR OTHER SITES', 'break' => '1', 'id' => 114]
    ]
];

给定具有字段“break”的数组。在此字段“break”的基础上,数组必须按结果数组中所示进行拆分。

请分享解决方案。提前谢谢。

1 个答案:

答案 0 :(得分:0)

给第一个数组的答案....

<?php
    $arrr = array(
        95 => array('title' => 'CONTACT US', 'break' => '1', 'id' => 95),
        100 => array('title' => 'CUSTOMER SERVICE', 'break' => '1', 'id' => 100), 
        105 => array('title' => 'SHOPPING TOOLS', 'break' => '0', 'id' => 105), 
        126 => array('title' => 'COMPANY INFO1', 'break' => '0', 'id' => 126), 
        108 => array('title' => 'COMPANY INFO', 'break' => '1', 'id' => 108), 
        111 => array('title' => 'CONNECT WITH US', 'break' => '0', 'id' => 111), 
        114 => array('title' => 'OUR OTHER SITES', 'break' => '1', 'id' => 114), 
    );
    $new_array = array();$i=0;
    foreach($arrr as  $row){
        if($row['break']==1){ if(!isset($new_array[$i]))$new_array[$i] = $row;else $new_array[$i][]=$row;$i++;}
        else $new_array[$i][] = $row;
    }
    echo "<pre>";print_r($new_array);
?>

结果

    Array(
        [0] => Array([title] => CONTACT US, [break] => 1, [id] => 95),
        [1] => Array([title] => CUSTOMER SERVICE, [break] => 1, [id] => 100),
        [2] => Array(
            [0] => Array([title] => SHOPPING TOOLS, [break] => 0, [id] => 105),
            [1] => Array([title] => COMPANY INFO1, [break] => 0, [id] => 126),
            [2] => Array([title] => COMPANY INFO, [break] => 1, [id] => 108),
        )
        [3] => Array(
            [0] => Array([title] => CONNECT WITH US, [break] => 0, [id] => 111)
            [1] => Array([title] => OUR OTHER SITES, [break] => 1, [id] => 114)
        )
    )