please check this :
Array
(
[0] => 46-65
[1] => 7-12|31-45
[2] => 31-45
[3] => 31-45
[4] => 66+
[5] => 18-30
[6] => 46-65
[7] => 13-17|46-65
Here i converted string to array then i got below array :
Array
(
[0] => Array
(
[0] => 46-65
)
[1] => Array
(
[0] => 7-12
[1] => 31-45
)
[2] => Array
(
[0] => 31-45
)
[3] => Array
(
[0] => 31-45
)
[4] => Array
(
[0] => 66+
)
[5] => Array
(
[0] => 18-30
)
[6] => Array
(
[0] => 46-65
)
[7] => Array
(
[0] => 13-17
[1] => 46-65
)
[8] => Array
(
[0] => 31-45
)
[9] => Array
(
[0] => 31-45
)
[10] => Array
(
[0] => 31-45
)
[11] => Array
(
[0] => 18-30
)
[12] => Array
(
[0] =>
)
[13] => Array
(
[0] => 46-65
)
[14] => Array
(
[0] => 7-12
[1] => 31-45
)
[15] => Array
(
[0] => 18-30
)
[16] => Array
(
[0] => 18-30
[1] => 31-45
)
[17] => Array
(
[0] => 18-30
)
[18] => Array
(
[0] => 31-45
)
[19] => Array
(
[0] => 18-30
)
[20] => Array
(
[0] => 13-17
[1] => 18-30
[2] => 46-65
)
}
I stored age group in string format because each row can be have multiple age group. after that i converted into array.
How can i get count of each age group like 46-65 is 6 times 18-30 is 8 times.
HEre i want count of each age group
This code should work for that:
<?php
$selections = [/** your array here */];
$counted = [];
// Loop full list
foreach($selections as $selection) {
// Loop age elements in list
foreach ($selection as $age) {
// If not already counted initialise age range
if(!isset($counted[$age])) {
$counted[$age] = 0;
}
// Increment age range
$counted[$age]++;
}
}
var_dump($counted);
I'm not sure I fully understood tom problem. Try something like this.
This will return you a array with as key the groups and as value the number of times it has been met.
I used your second array or you already subdivided into sub array
$groups = [];
foreach($array as $subArray) {
foreach ($subArray as $group) {
if (isset($groups[$group])) {
$groups[$group] += 1;
} else {
$groups[$group] = 1;
}
}
}
Related
This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 7 months ago.
There are many references on S/O showing various methods to flatten a multidimensional recursive array (with more than two levels). I have been through dozens (and tried most) but I'm still running into an odd problem with every one I've tried. What I am getting as a result is:
Array
(
)
Array
(
)
Array
(
)
Array
(
[0] => 1000043
[1] => 1000045
[2] => 1000050
)
Array
(
)
Array
(
)
Array
(
)
Array
(
)
Array
(
[0] => 1000030
[1] => 1000032
[2] => 1000058
[3] => 1000064
) ...
But what I'm expecting is a truly flattened single array:
Array
[0] => 1000043
[1] => 1000045
[2] => 1000050
[3] => 1000030
[4] => 1000032
[5] => 1000058
[6] => 1000064
)
The method I found on S/O is supposed to handle an "empty array" (which I assume is the problem) but I'm still getting the wrong output. Here is my code:
function array_flatten5(array $array)
{
$flat = array(); // initialize return array
$stack = array_values($array); // initialize stack
while($stack) // process stack until done
{
$value = array_shift($stack);
if (is_array($value)) // a value to further process
{
$stack = array_merge(array_values($value), $stack);
}
else // a value to take
{
$flat[] = $value;
}
}
return $flat;
}
Could someone point out what I missing here because I'm thinking it's something simple but at this point my eyes are crossed with the number of attempts I've made. Thank you for any help you can provide.
Here is the original array. It is 4-deep:
Array ( [0] => 1000043 [1] => 1000045 [2] => 1000050 ) Array ( [0] => 1000030 [1] => 1000032 [2] => 1000058 [3] => 1000064 ) Array ( [0] => 1000041 [1] => 1000059 [2] => 1000069 ) Array ( [0] => 1000021 [1] => 1000044 [2] => 1000049 [3] => 1000071 ) Array ( [0] => 1000009 [1] => 1000013 [2] => 1000015 [3] => 1000017 [4] => 1000053 ) Array ( [0] => 1000022 [1] => 1000034 [2] => 1000070 ) Array ( [0] => 1000038 [1] => 1000047 [2] => 1000055 [3] => 1000063 ) Array ( [0] => 1000019 [1] => 1000054 [2] => 1000060 [3] => 1000066 [4] => 1000068 ) Array ( [0] => 1000006 [1] => 1000014 [2] => 1000016 [3] => 1000072 ) Array ( [0] => 1000024 [1] => 1000025 [2] => 1000046 [3] => 1000061 [4] => 1000067 ) Array ( [0] => 1000028 [1] => 1000039 [2] => 1000048 ) Array ( [0] => 1000042 [1] => 1000057 ) Array ( [0] => 1000027 [1] => 1000033 [2] => 1000036 [3] => 1000037 ) Array ( [0] => 1000008 [1] => 1000010 [2] => 1000012 [3] => 1000018 ) Array ( [0] => 1000026 [1] => 1000062 [2] => 1000065 ) Array ( [0] => 1000020 [1] => 1000023 [2] => 1000031 [3] => 1000035 [4] => 1000040 ) Array ( [0] => 1000007 [1] => 1000011 [2] => 1000029 ) Array ( [0] => 1000051 [1] => 1000052 [2] => 1000056 ) Array ( [0] => 1000001 [1] => 1000002 [2] => 1000003 [3] => 1000004 [4] => 1000005 ) Array ( [0] => 1000073 )
And here is the outcome using the array_walk_recursive suggestion ...
Array
(
)
Array
(
)
Array
(
)
Array
(
)
Array
(
)
Array
(
[0] => 1000111
[1] => 1000113
[2] => 1000129
[3] => 1000134
)
Array
(
)
Array
(
)
Array
(
[0] => 1000012
[1] => 1000085
)
Array
(
) ...
You didn't prepare suitable array, but looking on this code you need probably just array_walk_recursive() function.
$array = [
[1, 2, 3, 4],
[[5, 6], [7, 8]],
[[[9], [10]], [11]]
];
$result = [];
array_walk_recursive($array, function ($tempV) use (&$result) {
$result[] = $tempV;
});
print_r($result);
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
[10] => 11
)
Hi I am working on some array operations.
I need to convert first value of array as key and second value of array as value.
I have one variable $testArray which stores array like below.
Array
(
[0] => Array
(
[0] => Color
[1] => White on Red
)
[1] => Array
(
[0] => Depicted Text
[1] => EMPTY
)
[2] => Array
(
[0] => Depth [Nom]
[1] => 0.004 in
)
[3] => Array
(
[0] => Language
[1] => English
)
[4] => Array
(
[0] => Length [Nom]
[1] => 10 in
)
[5] => Array
(
[0] => Material
[1] => Adhesive Vinyl
)
[6] => Array
(
[0] => Mounting
[1] => Surface
)
[7] => Array
(
[0] => Width [Nom]
[1] => 14 in
)
[8] => Array
(
[0] => Wt.
[1] => 0.056 lb
)
)
Expected output :
Array
(
[0] => Array
(
[Color] => White on Red
)
[1] => Array
(
[Depicted Text] => EMPTY
)
[2] => Array
(
[Depth [Nom]] => 0.004 in
)
[3] => Array
(
[Language] => English
)
[4] => Array
(
[Length [Nom]] => 10 in
)
[5] => Array
(
[Material] => Adhesive Vinyl
)
[6] => Array
(
[Mounting] => Surface
)
[7] => Array
(
[Width [Nom]] => 14 in
)
[8] => Array
(
[Wt.] => 0.056 lb
)
)
I have already tried with array function array_keys and array_values but it won't working
Simple solution using array_map function:
$result = array_map(function($v){
return [$v[0] => $v[1]];
}, $testArray);
Assuming that structure will always be the same, you could do this:
$output = array();
foreach($testArray as $v){
$output[] = array($v[0] => $v[1]);
}
See it in action here.
I need to merge a PHP array, this array has 2 arrays into it named "targetXX", I can have 2 or more. Each target have the same keys, for each key I have an array with 2 values a and b, a is always the same in both targets, but I need to merge both B values like this:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
)
[2] => Array
(
[0] => 600
[1] => 1392282320
)
[3] => Array
(
[0] => 200
[1] => 1392282380
)
[4] => Array
(
[0] => 400
[1] => 1392282440
)
[5] => Array
(
[0] => 600
[1] => 1392282500
)
)
)
[1] => Array
(
[target] => hitcount(stats.asdf.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 4321
[1] => 1392282200
)
[1] => Array
(
[0] => 76567
[1] => 1392282260
)
[2] => Array
(
[0] => 5556
[1] => 1392282320
)
[3] => Array
(
[0] => 7675
[1] => 1392282380
)
[4] => Array
(
[0] => 2344
[1] => 1392282440
)
[5] => Array
(
[0] => 0999
[1] => 1392282500
)
)
)
Result:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
[2] => 4321
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
[2] => 76567
)
[2] => Array
(
[0] => 600
[1] => 1392282320
[2] => 5556
)
[3] => Array
(
[0] => 200
[1] => 1392282380
[2] => 7675
)
[4] => Array
(
[0] => 400
[1] => 1392282440
[2] => 2344
)
[5] => Array
(
[0] => 600
[1] => 1392282500
[2] => 0999
)
)
)
Use array_merge() to achieve this:
$newArray = array();
foreach ($myArray['target2'] as $key => $innerArr1) {
$newArray['target'][$key] = array_merge(
$myArray['target1'][$key], /* 0th and 1st index */
array($innerArr1[1]) /* 2nd index */
);
}
print_r($newArray);
Output:
Array
(
[target] => Array
(
[0] => Array
(
[0] => 333333
[1] => 13
[2] => 99
)
[1] => Array
(
[0] => 444444
[1] => 15
[2] => 98
)
[2] => Array
(
[0] => 555555
[1] => 17
[2] => 97
)
)
)
Demo
The built-in function array_merge may do the work for you. You need to merge each subarrays in fact, as the array_merge_recursive function doesn't handle indexes.
$newArray = array();
foreach ($myArray['target2'] as $key => $arr) {
$newArray['target'][$key] = array_merge($myArray['target1'][$key], $arr[1]);
}
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
If you have more than 2 keys to merge, you can loop on the algorithm multiple times.
I'm working on Array list with have many duplication
I use array_unique
foreach ($aData as $test) {
preg_match( "~http://www.site.com/.*?/~", $test,$match) ;
$a[] = $match;
}
output
Array (
[0] => Array ( [0] => */liars-all-2013-hdrip-xvid-s4a/ )
[1] => Array ( [0] => */liars-all-2013-hdrip-xvid-s4a/ )
[2] => Array ( )
[3] => Array ( [0] => */liars-all-2013-hdrip-xvid-s4a/ )
[4] => Array ( [0] => */mt-zion-2013-dvdrip-xvid-fihvid/ )
[5] => Array ( [0] => */mt-zion-2013-dvdrip-xvid-fihvid/ )
[6] => Array ( [0] => */mt-zion-2013-dvdrip-xvid-fihvid/ )
[7] => Array ( [0] => */the-ghastly-love-of-johnny-x-2012-webrip-xvid-fan0n/ )
[8] => Array ( [0] => */the-ghastly-love-of-johnny-x-2012-webrip-xvid-fan0n/ )
[9] => Array ( [0] => */the-ghastly-love-of-johnny-x-2012-webrip-xvid-fan0n/ )
[10] => Array ( [0] => */kung-fu-panda-good-croc-bad-croc-2013-dvdrip-x264-ac3-deep1007/ )
[11] => Array ( [0] => */kung-fu-panda-good-croc-bad-croc-2013-dvdrip-x264-ac3-deep1007/ )
[12] => Array ( [0] => */kung-fu-panda-good-croc-bad-croc-2013-dvdrip-x264-ac3-deep1007/ )
[13] => Array ( [0] => */a-viking-saga-the-darkest-day-2013-1080p-bluray-x264-ulshd/ )
[14] => Array ( [0] => */a-viking-saga-the-darkest-day-2013-1080p-bluray-x264-ulshd/ )
[15] => Array ( [0] => */a-viking-saga-the-darkest-day-2013-1080p-bluray-x264-ulshd/ )
[16] => Array ( [0] => */dead-man-down-2013-extras-720p-bluray-x264-phd/ )
[17] => Array ( [0] => */dead-man-down-2013-extras-720p-bluray-x264-phd/ )
[18] => Array ( [0] => */dead-man-down-2013-extras-720p-bluray-x264-phd/ )
[19] => Array ( [0] => */spider-baby-1968-720p-bluray-x264-geckos/ )
[20] => Array ( [0] => */spider-baby-1968-720p-bluray-x264-geckos/ )
[21] => Array ( [0] => */spider-baby-1968-720p-bluray-x264-geckos/ )
[22] => Array ( [0] => */drift-2013-bluray-720p-750mb-direct-download/ )
[23] => Array ( [0] => */drift-2013-bluray-720p-750mb-direct-download/ )
[24] => Array ( [0] => */drift-2013-bluray-720p-750mb-direct-download/)
[25] => Array ( [0] => */beautiful-creatures-2013-720p/ )
[26] => Array ( [0] => */beautiful-creatures-2013-720p/ )
[27] => Array ( [0] => */beautiful-creatures-2013-720p/ )
[28] => Array ( [0] => */death-race-3-inferno-2012-bluray-720p-direct-download/ )
[29] => Array ( [0] => */death-race-3-inferno-2012-bluray-720p-direct-download/ )
[30] => Array ( [0] => */death-race-3-inferno-2012-bluray-720p-direct-download/ )
)
It's look like array_unique cant go deep in to array like : []**[x]**
so only thing i get after array_unique is my first [0][string]
I tried to use only $match but its only show me last string which is useless
So how could I resolve this BUG?
By default array_unique compares the items as if they are string, which means that it actually converts the arrays to string (and any array casted to string will be the same - the string 'Array'). So you have to do:
$a = array_unique($a, SORT_REGULAR)
I'd like to sum products by the same ref number, but I have 3rd parameter like dimension e.g. (..., 24, 26mm,...) and I can't sum this values only when they have the same dimension. I tried this: Group a multidimensional array by a particular value? but how to sum values?
My array looks like this:
Array
(
[0] => Array
(
[0] => 2
[1] => 790180X
[2] => 26mm
)
[1] => Array
(
[0] => 4
[1] => 762182Z
)
[2] => Array
(
[0] => 2
[1] => 072182X
)
[3] => Array
(
[0] => 4
[1] => 660122Y
)
[4] => Array
(
[0] => 2
[1] => 790180X
[2] => 24mm
)
[5] => Array
(
[0] => 1
[1] => 225160Y
)
[6] => Array
(
[0] => 1
[1] => 244160Y
)
[7] => Array
(
[0] => 1
[1] => 225160Y
)
[8] => Array
(
[0] => 8
[1] => 954120Y
)
[9] => Array
(
[0] => 3
[1] => 072182X
)
)
I'd like to something like this:
Array
(
[0] => Array
(
[0] => 2
[1] => 790180X
[2] => 26mm
)
[1] => Array
(
[0] => 2
[1] => 790180X
[2] => 24mm
)
[2] => Array
(
[0] => 4
[1] => 762182Z
)
[3] => Array
(
[0] => 5
[1] => 072182X
)
[4] => Array
(
[0] => 4
[1] => 660122Y
)
[5] => Array
(
[0] => 2
[1] => 225160Y
)
[6] => Array
(
[0] => 1
[1] => 244160Y
)
[7] => Array
(
[0] => 8
[1] => 954120Y
)
)
The array in short version:
Array => TO => Array
( (
[0] => 2:790180X:26mm [0] => 2:790180X:26mm
[1] => 4:762182Z [1] => 2:790180X:24mm
[2] => 2:072182X [2] => 4:762182Z
[3] => 4:660122Y [3] => 5:072182X
[4] => 2:790180X:24mm [4] => 4:660122Y
[5] => 1:225160Y [5] => 2:225160Y
[6] => 1:244160Y [6] => 1:244160Y
[7] => 1:225160Y [7] => 8:954120Y
[8] => 8:954120Y )
[9] => 3:072182X
)
Just sum into a new array, using the various "grouping" fields as keys in the new array:
$sums = array();
foreach ($yourarray as $element) {
$sums[$element['dimension1']][$element['dimension2']]++;
}
For every dimension you need to 'group' by, you add a key to the $sums array.