In addition to stefgosselin's answer that has some mistakes: Lets start with his array:
$input = array(1,2,3);
This contains:
array(3) {
[0]=> int(1)
[1]=> int(2)
[2]=> int(3)
}
Then you do array_slice:
var_dump(array_slice($input, 1));
The function will return the values after the first element (thats what the second argument, the offset means). But notice the keys!
array(2) {
[0]=> int(2)
[1]=> int(3)
}
Keep in mind that keys aren't preserved, until you pass true for the fourth preserve_keys parameter. Also because there is another length parameter before this, you have to pass NULL if you want to return everything after the offset, but with the keys preserved.
var_dump(array_slice($input, 1, NULL, true));
That will return what stefgosselin (incorrectly) wrote initially.
array(2) {
[1]=> int(2)
[2]=> int(3)
}
Answer from totymedli on Stack OverflowVideos
In addition to stefgosselin's answer that has some mistakes: Lets start with his array:
$input = array(1,2,3);
This contains:
array(3) {
[0]=> int(1)
[1]=> int(2)
[2]=> int(3)
}
Then you do array_slice:
var_dump(array_slice($input, 1));
The function will return the values after the first element (thats what the second argument, the offset means). But notice the keys!
array(2) {
[0]=> int(2)
[1]=> int(3)
}
Keep in mind that keys aren't preserved, until you pass true for the fourth preserve_keys parameter. Also because there is another length parameter before this, you have to pass NULL if you want to return everything after the offset, but with the keys preserved.
var_dump(array_slice($input, 1, NULL, true));
That will return what stefgosselin (incorrectly) wrote initially.
array(2) {
[1]=> int(2)
[2]=> int(3)
}
This function returns a subset of the array. To understand the example on the man page you have to understand array keys start at 0, ie
$array_slice = $array(1,2,3);
The above contains this:
$array[0] = 1,
$array[1] = 2,
$array[2] = 3
So, array_slice(1) of $array_sliced would return:
$arraysliced = array_slice($array_slice, 1);
$arraysliced[1] = 2;
$arraysliced[2] = 3;
Better make assosative array instead of typing $array_1, $array_2. And instead of making array with same values like array('1', '1', '1'), you can just keep the amount of value like array('1' => 3)
Copy$mix_array = array ('1','3','6','1','5','3','6','5','1','7','3','9');
$group_array = array();
foreach ($mix_array as $value) {
if(isset($group_array[$value])) {
$group_array[$value]++;
}
else {
$group_array[$value] = 1;
}
}
print_r($group_array);
Result
CopyArray
(
[1] => 3
[3] => 3
[6] => 2
[5] => 2
[7] => 1
[9] => 1
)
Or as Josh said simply use array_count_values()
There are several ways to do it, here is an example (by the way, quite inefficient, but enough if it is a short work):
Copy<?php
$mix_array = array('1','3','6','1','5','3','6','5','1','7','3','9');
$sliced_array = array();
foreach($mix_array as $k => $v) {
if( count($sliced_array[$v]) > 0 ) {
array_push($sliced_array[$v], $v);
} else {
$sliced_array[$v] = array($v);
}
}
?>
You should take a look at http://es.php.net/manual/en/ref.array.php