I want to split the multidimensional array using the key.. here in this, I need to split the array by using the key (5) and split into separate arrays.
Array (
[0] => Array (
[0] => i
[1] => FW
),
[1] => Array (
[0] => was
[1] => VBD
),
[2] => Array ( [
0] => reading
[1] => VBG
),
[3] => Array (
[0] => a
[1] => DT
),
[4] => Array (
[0] => book
[1] => NN
),
[5] => Array (
[0] => but
[1] => CC
),
[6] => Array (
[0] => she
[1] => PRP
),
[7] => Array (
[0] => was
[1] => VBD
),
[8] => Array (
[0] => writing
[1] => VBG
)
)
but i want result like this
array (
[0] => Array ( [0] => i [1] => FW )
[1] => Array ( [0] => was [1] => VBD )
[2] => Array ( [0] => reading [1] => VBG )
[3] => Array ( [0] => a [1] => DT )
[4] => Array ( [0] => book [1] => NN )
)
array (
[0] => Array ( [0] => she [1] => PRP )
[7] => Array ( [0] => was [1] => VBD )
[8] => Array ( [0] => writing [1] => VBG )
)
$array1 = [];
$array2 = [];
foreach($yourArray as $key=>$value)
{
if($key < 5) {
// If the key is lesser than 5, put it into the array 1
$array1[$key] = $value;
} elseif($key == 5) {
// if it's 5, don't do anything
continue;
} else {
// if it's greater than 5, put it into the array 2
//if($key == 6){ $key = 0; }
$array2[$key] = $value;
}
}
use foreach loop
and use condition if key
is less then 5 then create a array
if greater then create another
<?php
$data = array(array('a','b'),array('c','d'),array('e','f'),array('g','h'),array('i','j'),array('k','l'),array('m','n'),array('o','p'),array('q','r'));
foreach($data as $key=>$value){
if($key < 5){
$first[$key] = $value;
}else{
if($key == 5)continue;
else{
$key = $key == 6 ? 0 : $key;// comment this line if you not want to change key 6 to 0
$second[$key] = $value;
}}
}
?>
Replace $data
with your array
php code:
<?php
$key = 5;
$start_array = [
['i', 'FW'],
['was', 'VBD'],
['reading', 'VBG'],
['a', 'DT'],
['book', 'NN'],
['but', 'CC'],
['she', 'PRP'],
['was', 'VBD'],
];
$first = [];
$second = [];
foreach ($start_array as $start_array_key => $start_array_sub_array){
if($key == $start_array_key ){
}else{
if($start_array_key<$key)
$first[$start_array_key] = $start_array_sub_array;
else
$second[$start_array_key] = $start_array_sub_array;
}
}
print_r($first);
echo "\n";
print_r($second);
result:
Array
(
[0] => Array
(
[0] => i
[1] => FW
)
[1] => Array
(
[0] => was
[1] => VBD
)
[2] => Array
(
[0] => reading
[1] => VBG
)
[3] => Array
(
[0] => a
[1] => DT
)
[4] => Array
(
[0] => book
[1] => NN
)
)
Array
(
[6] => Array
(
[0] => she
[1] => PRP
)
[7] => Array
(
[0] => was
[1] => VBD
)
[8] => Array
(
[0] => writing
[1] => VBG
)
)
This is what you need
$array1=array();
$array2=array();
for($x=0;$x<9;$x++){
if($x<5){
$array1[$x][0]=$cars[$x][0];
$array1[$x][1]=$cars[$x][1];
}
else if($x>5){
$array2[$x][0]=$cars[$x][0];
$array2[$x][1]=$cars[$x][1];
}
}
print_r($array1);
echo "\n";
print_r($array2);
?>
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.