Is there a way in php to count how often a value exists in a large array?
So if I have an array like this:
$array = "1,2,3,4,joe,1,2,3,joe,joe,4,5,1,6,7,8,9,joe";
is there a way to output a new array that tells me (and sorts) which is used most and how many for each?
$result = array(
[joe] => 4
[1] => 3
[2] =>2
etc...
)
I've seen the php array_count_values, but can this be sorted by most -> least? or is there an easier way?
Thanks everyone!
Sort them after counting them with arsort()
$result = array_count_values(explode(',', $array));
arsort($result);
Array
(
[joe] => 4
[1] => 3
[2] => 2
[4] => 2
[3] => 2
[9] => 1
[8] => 1
[5] => 1
[6] => 1
[7] => 1
)
Related
I have an array like the below one
Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 2
[4] => 2
[5] => 3
[6] => 3
[7] => 3
[8] => 4
[9] => 4
[10] => 4
)
Is there any way to get a subset of this array using the values? So that, if need the subset of value 1, then it has to display
Array
(
[0] => 1
[1] => 1
[2] => 1
)
And if subset of 2 then
Array
(
[3] => 2
[4] => 2
)
And so on.. I need to preserve the index too.
I searched so many places. But didn't get an answer for this. I wish to avoid the multiple looping for this.
You can use array_filter to filter an array down to only the elements you're looking for. Set $subset to whatever value you're searching for, and it'll return the matching elements, without changing the keys.
$subset = 2;
$results = array_filter($array, function ($item) use ($subset) {
return $item === $subset;
});
print_r($results);
Array
(
[3] => 2
[4] => 2
)
See https://eval.in/926983
Try array_chunk
array_chunk($array,4,true);
First parameter array
Second parameter size
Third parameter preserve keys
Hello guys i would to move the last element in the node (of a multidimensional php array) at the beginning...
This is my array:
$arr= array(
array(2,3,4,5,1),
array(3,4,5,6,2),
array(4,5,6,7,3)
);
and this is the output that i would to have:
Array (
[0] => Array (
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
[1] => Array (
[0] => 2
[1] => 3
[2] => 4
[3] => 5
[4] => 6
)
[2] => Array (
[0] => 3
[1] => 4
[2] => 5
[3] => 6
[4] => 7
)
)
I created this code that works fine
$number= count($arr);
for($a= 0; $a < $number; $a++){
$element= $arr[$a][4];
unset($arr[$a][4]);
array_unshift($arr[$a],$element);
}
but i would know if there are other faster and efficient methods...
Thanks a lot and sorry for my english :)
Given the input you provided and the output, this is quicker to type:
array_map('sort', $arr);
Whether it runs faster or gives the desired result - I have no idea.
A correct answer that doesn't depend on the sort order of the values inside the array.
foreach($array as &$element) {
array_unshift($element,array_pop($element));
}
I have an array
Array (
[bla123] => Array
(
[0] => 2
[1] => 3
[2] => 2
[3] => 2
[4] => 2
[5] => 2
[6] => 2
[7] => 2
[8] => 2
)
[12xye] => Array
(
[0] => 4
[1] => 3
[2] => 3
[3] => 2
[4] => 2
[5] => 4
[6] => 2
[7] => 2
[8] => 2
)
)
How can i access this array in php and also get the number of 1,2,3.. etc from it in php.
The logic is to get the rating of a products. the data is fetched from the database completely and then sorted using php.
for eg:
product1
one star:1
two star:3
three star:2
etc...
somewhat like a star system in flipkart, amazons etc..
use the below code:
<?php
$mainArrDat = array('bla123'=>array('Your_Array_Data_Here'),'12xye'=>('Your_Array_Data_Here'));
foreach( $mainArrDat as $mainArr )
{
foreach($mainArr as $nowArr)
{
//you can access the data that you require from $nowArr
}
}
?>
I want to make a array of arrays.
Problem:
My final array will be like this:
Array(Array1, Array2, Array3);
and arrays will be
Array1=Array ( [0] => 0 [1] => 100 [2] => 100 [3] => 0 [4] => 0 [5] => 0 [6] => 0
Array2=Array ( [0] => 0 [1] => 100 [2] => 100 [3] => 60 [4] => 0 [5] => 30 [6] => 0
Array3=Array ( [0] => 50 [1] => 100 [2] => 100 [3] => 0 [4] => 0 [5] => 0 [6] => 40
So how can make the multidimensional array and how can I access data from this array. Help will be appreciated. Thanks
Unlike a statically typed language, there is no need to declare these up front which can be confusing to newcomers. Really what you're describing is just a two-dimensional array. So really you have two options. Assuming these aren't associative like your example above, either putting arrays together:
$array = array($array1, $array2, $array3);
Or if you are doing something with loops/iterators you can just define your two-d array on the fly:
$array[$itr][$inner_itr] = $array1[$inner_itr];
Hope that helps.
EDIT: For anyone who might come across this post with a similar problem, It was solved by taking konforce's supplied answer and tweaking around a bit with the custom sorting function:
function cmp($a, $b) {
if ($a[5] == $b[5]) {
return ($a[3] < $b[3]) ? -1 :1;
}
return ($a[5] > $b[5]) ? -1 : 1;
}
Notice $a[5] == $b[5] does not return zero. It was changed to check who has the most losses and then sort it in ASC order. I'm sure you can even keep going and add another if-statement in there in-case they have the same losses.
Lastly, all you do is usort($ARRAY, "cmp"); and finito!!!
Original Post
My apologies for coming up with yet another MD Array sorting question but I'm just not getting it. I've searched aplenty
for a solution and although many sites have provided what seemed like a logical answer I still have not been able to figure it out.
My problem is since I'm still learning its been rather difficult for me to grasp the concept of using usort with a custom comparing
function. Atleast, thats what I have seen the most when others have tried to sort MD Arrays.
I'm working on a small project to sharpen up on my php skills. Its a very basic tournament standings script that holds a team's information within an array. I would like to sort the array by most points($array[X][X][5]).
So the array looks something like this:
Array (
[0] => Array (
[0] => Array (
[0] => cooller
[1] => 6
[2] => 6
[3] => 0
[4] => 0
[5] => 18
)
)
[1] => Array (
[0] => Array (
[0] => strenx
[1] => 9
[2] => 5
[3] => 1
[4] => 3
[5] => 18
)
)
[2] => Array (
[0] => Array (
[0] => rapha
[1] => 10
[2] => 8
[3] => 1
[4] => 1
[5] => 25
)
) [3] => Array (
[0] => Array (
[0] => ronald reagan
[1] => 5
[2] => 4
[3] => 0
[4] => 1
[5] => 13
)
)
)
I would like to sort it by most points(cell #5), so it would look like this after sorting:
Array (
[0] => Array (
[0] => Array (
[0] => rapha
[1] => 10
[2] => 8
[3] => 1
[4] => 1
[5] => 25
)
)
[1] => Array (
[0] => Array (
[0] => cooller
[1] => 6
[2] => 6
[3] => 0
[4] => 0
[5] => 18
)
)
[2] => Array (
[0] => Array (
[0] => strenx
[1] => 9
[2] => 5
[3] => 1
[4] => 3
[5] => 18
)
)
[3] => Array (
[0] => Array (
[0] => ronald reagan
[1] => 5
[2] => 4
[3] => 0
[4] => 1
[5] => 13
)
)
)
The player with 25 points would be at the top, followed by 18, 18, and lastly 13. Sorry for my earlier post, was having difficulty wording my question correctly. Thanks in advanced!
I think you want something like this:
usort($standings, function($a, $b) { return $b[0][5] - $a[0][5]; });
Or prior to PHP 5.3:
function cmp($a, $b) { return $b[0][5] - $a[0][5]; }
usort($standings, 'cmp');
When using usort, the $a and $b parameters will be one "layer" into the supplied array. So in your case, an example of $a or $b will be:
[0] => Array (
[0] => cooller
[1] => 6
[2] => 6
[3] => 0
[4] => 0
[5] => 18
)
I'm not sure why you have an extra containing array there, but as you can see, you want to sort based on the [0][5] position.
usort($standings[0][0][5], 'cmp') won't work because the first parameter isn't an array to sort, it's just a single number, the points.