简体   繁体   中英

Split Array into multiple Array and store in different variables

I want to divide below given array and store it in different variable.For eg.$array1,$array2,$array3.Any help would be grateful.

Array
    (
        [0] => Array
            (
                [0] => PVTHR05L006
                [1] => PVTHR05L004
                [2] => PVTS1K05L004
            )

        [1] => Array
            (
                [0] => PVTHR05L004
            )

        [2] => Array
            (
                [0] => PVTHR05L006
                [1] => PVTHR05L004
                [2] => PVTSK05L008
            )

    )

My expected output is

$array1 = Array
        (
            [0] => PVTHR05L006
            [1] => PVTHR05L004
            [2] => PVTS1K05L004
        ); $array2 = Array
        (
            [0] => PVTHR05L004
        ); $array3 = Array
        (
            [0] => PVTHR05L006
            [1] => PVTHR05L004
            [2] => PVTSK05L008
        );

As you really don't know the logic- The main logic behind this is variables of variable. Assign two variable called $arr_name and $incre , the $arr_name will be use to create the dynamic array name with the help of $incre .

$mainArr = array(
            array('PVTHR05L006', 'PVTHR05L004', 'PVTS1K05L004'),
            array('PVTHR05L004'),
            array('PVTHR05L006', 'PVTHR05L004', 'PVTSK05L008'),
        );

$arr_name = 'array';
$incre = 1;
foreach($mainArr as $arr){
    ${$arr_name.$incre} = $arr;
    $incre++;
}

print_r($array2);

Online Example , click and see the output. If you need any further help let me know.

$arry = array(
    0 => array('a', 'b'),
    1 => array('c', 'd'),
    2 => array('e', 'f'),
    3 => array('g', 'h'),
    4 => array('i', 'j'),
    5 => array('k', 'l'),
    6 => array('m'),
    7 => array('n'),
    8 => array('o'),
    9 => array('p'),
   10 => array('q'),
   11 => array('r')
);


$i = 1;


foreach($arry as $val){

    $arr[$i] = $val;

    $i++;

}
print_r($arr[$i]);

Put the values inside a foreach assign to array keys 1,2,3. etc..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM