PHP Splitting an Array into two arrays - keys array and values array
There are two functions called array_keys and array_values:
$array_keys = array_keys($array);
$array_values = array_values($array);
Split the array in to sub arrays based on array key value
One way to do it with simple foreach()
loop to push values based on your brand_id like below-
$key = 'brand_id';
$return = array();
foreach($array as $v) {
$return[$v[$key]][] = $v;
}
print_r($return);
WORKING DEMO: https://3v4l.org/bHuWV Clean way to divide one array to two arrays in Laravel 8
You can use array_slice()
as you say you want to split the array into the first 5 and then all the rest.
$input = [ "key1" => "value1",
"key2" => "value2",
"key3" => "value3",
"key4" => "value4",
"key5" => "value5",
"key6" => "value6",
"key7" => "value7",
"key8" => "value8",
"key9" => "value9",
"key10" => "value10",
"_token" => "47p7eZpSOVOP0kSrL1HBSXn2OrvYT1kCiNoR2Ekr",
"submit" => "Save"
];
$p1 = array_slice($input, 0, 5);
$p2 = array_slice($input, 5);
print_r($p1);
print_r($p2);
RESULTSArray
(
[key1] => value1
[key2] => value2
[key3] => value3
[key4] => value4
[key5] => value5
)
Array
(
[key6] => value6
[key7] => value7
[key8] => value8
[key9] => value9
[key10] => value10
[_token] => 47p7eZpSOVOP0kSrL1HBSXn2OrvYT1kCiNoR2Ekr
[submit] => Save
)
split an array into multiple part based on key pattern
If I understand correctly you can do it like the following, but it is an XY problem, why are you not structuring your arrays properly first?
$normalised = [];
foreach ($array as $key => $value) {
list($k, $a) = explode('-', $key, 2);
$normalised[$k][$a] = $value;
}
Split an array into multiple arrays depending on number of similar adjacent values
I don't recommend the use of dynamic number of resulting vars like you asked in your question. It's better to have the result inside an array. This way you can cycle through the array indexes, instead of guessing how many $input_array_(number) your code generated.
$input_array = array('45', '21', '45', '45', '45', '29', '35', '35', '21');
$result = [];
$index = 0;
$processedElement = null;
foreach ($input_array as $currentElement) {
if ($processedElement != $currentElement) {
$index++;
}
$result[$index][] = $currentElement;
$processedElement = $currentElement;
}
Printing $result
will give you:Array
(
[0] => Array
(
[0] => 45
)
[1] => Array
(
[0] => 21
)
[2] => Array
(
[0] => 45
[1] => 45
[2] => 45
)
[3] => Array
(
[0] => 29
)
[4] => Array
(
[0] => 35
[1] => 35
)
[5] => Array
(
[0] => 21
)
)
Split PHP array into two arrays based on associate keys
No frills:
$newArray1 = [];
$newArray2 = [];
foreach ($myArray as $key => $value) {
if (in_array($key, $split)) {
$newArray1[$key] = $value;
} else {
$newArray2[$key] = $value;
}
}
Frills:$newArray1 = array_intersect_key($myArray, array_flip($split));
$newArray2 = array_diff_key($myArray, $newArray1);
See also: array_intersect_key()
array_flip()
array_diff_key()
Split an array into mutiple arrays based on a value - PHP
$sk = "first";
foreach ($array as $key=>$value)
{
if(in_array(BEGIN_DAY, $value)&&$key!=0)
{
$sk = "second";
}
$result[$sk][] = $value;
}
echo "<pre>";
print_r($result);
$first = $result['first'];
$second = $result['second'];
Related Topics
Custom Repository Class in Symfony2
PHP to Check If a Url Contains a Query String
Create Multidimensional Array Using a Foreach Loop
PHP Displaying Unread Mail Count
Setting Max_Input_Vars PHP.Ini Directive Using Ini_Set
PHP Curl Post Returns a 415 - Unsupported Media Type
Calling a Stored Procedure from Codeigniter's Active Record Class
Different Timezone_Types on Datetime Object
PHP Flush() Not Working in Chrome
Config Nginx for Laravel in a Subfolder
No Hint Path Defined for [Mail] Laravel 5.4
Save CSV Files into MySQL Database
SQL Select * from Multiple Tables