I have an array that I use a loop to run throught, the code below:
foreach($arry as $parentkey => $parentvalue){
$secondloop = explode(",",$parentvalue);
foreach($secondloop as $childvalue){
echo $parentkey.' '.$childvalue ;
}
}
When I run it, it does not display the parentkey. Does php not support that kind of loop?
How do I make it display the parent key? What would be the best way to walk throught the loop to get the desired result?
original array
Array ( [1] => 2,3,10,11,27,28,35,36,165,37,38,40,41,42,43,44,46,49,50,61,62,65,66,75,67,71,69,72,73,74,76,96,90,91,97,107,118,147,119,122,139,142,148,149,168,169,170,171,172,173,174,181 [2] => 39,102,94,98,92,121 [3] => 45,77,117,103,109,99 [4] => 47,78,146,105,113,115,104 [5] => 48,79,106,114,120,110 [6] => 68,93,116,111,112 [7] => 140,150 [8] => 141,151 [9] => 143,144,166,153 [10] => 145,154,159 [11] => 157,155 [12] => 158,156 [13] => 160 [14] => 161 [15] => 162 [16] => 163 [17] => 164 )
Given the information given, your code works, see the following cleaned up example.
<?php
$arry = array(
1 => '2,3,10,11,27,28,35,36,165,37,38,40,41,42,43,44,46,49,50,61,62,65,66,75,67,71,69,72,73,74,76,96,90,91,97,107,118,147,119,122,139,142,148,149,168,169,170,171,17$
2 => '39,102,94,98,92,121',
3 => '45,77,117,103,109,99',
4 => '47,78,146,105,113,115,104',
5 => '48,79,106,114,120,110',
6 => '68,93,116,111,112',
7 => '140,150',
8 => '141,151',
9 => '143,144,166,153',
10 => '145,154,159',
11 => '157,155',
12 => '158,156',
13 => '160',
14 => '161',
15 => '162',
16 => '163',
17 => '164'
);
foreach($arry as $parentkey => $parentvalue){
$secondloop = explode(",",$parentvalue);
foreach($secondloop as $childvalue){
echo 'Parent key: ' . $parentkey . ', child value: ' . $childvalue . PHP_EOL;
}
}
Related
This question already has answers here:
Count number of values in array with a given value
(8 answers)
Closed 5 years ago.
i am trying to count all multiple value in array and display in one array.
Array
(
[0] => unicomp6.unicomp.net
[1] =>
[2] => burger.letters.com
[3] =>
[4] => burger.letters.com
[5] =>
[6] => burger.letters.com
[7] =>
[8] =>
[9] => d104.aa.net
[10] =>
[11] => unicomp6.unicomp.net
[12] =>
[13] =>
[14] => unicomp6.unicomp.net
[15] =>
[16] => unicomp6.unicomp.net
[17] =>
[18] => d104.aa.net
[19] =>
[20] => d104.aa.net
[21] =>
)
output result would be like that.
Array
(
[unicomp6.unicomp.net ] => 4
[burger.letters.com ] => 3
[d104.aa.net] => 3
)
I have written this code but i want to know how to merge all the unique value in array ,please help me how can iphp do it:
$j=0;
$arrayName = array();
foreach ($host_name as $key => $value) {
$size= sizeof($host_name);
if($value!='')
{
$count=1;
for ($k=0; $k<$size; $k++) {
if($host_name[$j]==$host_name[$k])
{
$arrayName = array($host_name[$j]=> $count++);
}
}
}
$j++;
}
print_r($arrayName);
<?php
$data=Array
(
'unicomp6.unicomp.net','','burger.letters.com','','burger.letters.com','','burger.letters.com','','','d104.aa.net','unicomp6.unicomp.net','','unicomp6.unicomp.net','d104.aa.net','','d104.aa.net',''
);
echo"<pre>";
print_r(array_count_values(array_filter($data)));
echo"</pre>";
And the output after trimmed the empty fields :
Array
(
[unicomp6.unicomp.net] => 3
[burger.letters.com] => 3
[d104.aa.net] => 3
)
I'm querying a database to get this array. The output is an account number followed by the amount for that account number. As you can see there are duplicate instances of the account number. This array will always be the same, ie account number and then amount. How do I get a total of a single account number? For example the total amount for 1B2 is $10119.59.
Array
(
[0] => 1B2
[1] => 1970.40
[2] => 1B2
[3] => 1493.60
[4] => 1B2
[5] => 1400.25
[6] => J014 1
[7] => 1423.20
[8] => J014 1
[9] => 2179.20
[10] => J014 1
[11] => 1432.00
[12] => J014 1
[13] => 711.60
[14] => 1B2
[15] => 298.72
[16] => 1B2
[17] => 1568.80
[18] => 1B2
[19] => 1822.62
[20] => 1B2
[21] => 1493.60
[22] => J014 1
[23] => 1400.25
[24] => 1B2
[25] => 711.60
[26] => J014 1
[27] => 1194.88
[28] => J014 1
[29] => 1493.60
)
Here is one way to do it using a bunch of array functions.
$your_account = '1B2';
$total = array_sum(array_column(array_filter(
array_chunk($array, 2), function($v, $k) use ($your_account) {
return $v[0] == $your_account;
}, ARRAY_FILTER_USE_BOTH), 1));
Working from inside to outside:
array_chunk creates a two dimensional array of account entries
array_filter filters the array to only entries for your account
array_column gets only the amount column
array_sum totals the
amounts
You can use the following code:
$data = [
0 => '1B2',
1 => '1970.40',
2 => '1B2',
3 => '1493.60',
4 => '1B2',
5 => '1400.25',
6 => 'J014 1',
7 => '1423.20',
8 => 'J014 1',
9 => '2179.20',
10 => 'J014 1',
11 => '1432.00',
12 => 'J014 1',
13 => '711.60',
14 => '1B2',
15 => '298.72',
16 => '1B2',
17 => '1568.80',
18 => '1B2',
19 => '1822.62',
20 => '1B2',
21 => '1493.60',
22 => 'J014 1',
23 => '1400.25',
24 => '1B2',
25 => '711.60',
26 => 'J014 1',
27 => '1194.88',
28 => 'J014 1',
29 => '1493.60',
];
$temp = array();
$i = 0;
$account_key = '';
foreach($data as $key => $value) {
if ($i % 2 == 0) { //This is the account number
$account_key = $value;
} else { //This is the amount
$temp[$account_key][] = $value;
}
$i++;
}
foreach($temp as $key => $value) { //For each account number sum the amount
echo $key.': '.array_sum($value).'<br />';
}
Result:
1B2: 10759.59
J014 1: 9834.73
I am using php
Below is my array
Array
(
[0] => Q .
[1] => What are the factors of x2+2x+1?
[2] => A .
[3] => (x+1)
[4] => B .
[5] => (x-1)
[6] => C .
[7] => (2x+1)
[8] => D .
[9] => (2x-1)
[10] => S .
[11] => (x+1)
[12] => M.
[13] => 20
[14] => Q .
[15] => Test are the factors of x2-2x+1?
[16] => A .
[17] => (x+1)
[18] => B .
[19] => (x-1)
[20] => C .
[21] => (2x+1)
[22] => D .
[23] => (2x-1)
[24] => S .
[25] => (x-1)
[26] => M.
[27] => 5
)
And i want result in
Array
(
[0] => Array
(
[question] => What are the factors of x2+2x+1?
[A.] => (x+1)
[B.] => (x-1)
[C.] => (2x+1)
[D.] => (2x-1)
[S.] => (x+1)
[M.] => 20
)
[0] => Array
(
[question] => TEST What are the factors of x2+2x+1?
[A.] => (x+1)
[B.] => (x-1)
[C.] => (2x+1)
[D.] => (2x-1)
[S.] => (x+1)
[M.] =>
)
)
Please Advise.
You could do it like this:
$array = Array
(
0 => "Q .",
1 => "What are the factors of x2+2x+1?",
2 => "A .",
3 => "(x+1)",
4 => "B .",
5 => "(x-1)",
6 => "C .",
7 => "(2x+1)",
8 => "D .",
9 => "(2x-1)",
10 => "S ." ,
11 => "(x+1)" ,
12 => "M." ,
13 => "20" ,
14 => "Q ." ,
15 => "Test are the factors of x2-2x+1?" ,
16 => "A ." ,
17 => "(x+1)" ,
18 => "B ." ,
19 => "(x-1)" ,
20 => "C ." ,
21 => "(2x+1)" ,
22 => "D ." ,
23 => "(2x-1)" ,
24 => "S ." ,
25 => "(x-1)" ,
26 => "M." ,
27 => "5" ,
);
$keys = array(
'question','A. ','B. ','C. ','D. ','S. ','M. '
);
$result = array_map(
function($q) use ($keys) {
return array_combine($keys, $q); // 4. make our keys the keys of each resulting array
},
array_chunk(
array_map(
function($a){
return $a[1]; // 2. select only the 2nd part of the chunk
},
array_chunk($array, 2) // 1. split initial array into chunks of 2
)
,7) // 3. split it into chunks of 7
);
Ive sat with this for a while now, and its getting late.
Im trying to get a top 3 of most sales from last month, and i need to count how many times a id from array 1 is equal to array 2 last month(6 = last atm.) like id 4 = 2, id 7 = 3
It might not be the perfect solution, but im just trying to break it down by my self, so later on 'maybe' problems, will i take care of when i hit the wall,
so please, if anyone can help me here, ill be greatfull.
UPDATE
- I will add the result im looking for here: (sorry i didnt before, it makes it alot easier :-)
- The result below, is because i want the count from 2014-06-01 and up to the last day of that month monly, on array[0][1] under this array, only 6-7-8 is not from 2014-06
Hope it makes a bit more sense now ^^
Array
(
[0] => Array
(
[0] => Array
(
[4] => 2
[7] => 3
[1] => 2
[3] => 2
[9] => 1
[12] => 1
[2] => 1
[13] => 1
)
)
)
Array
(
[0] => Array
(
[0] => Array
(
[0] => 4
[1] => 4
[2] => 7
[3] => 1
[4] => 7
[5] => 7
[6] => 3
[7] => 3
[8] => 4
[9] => 9
[10] => 12
[11] => 2
[12] => 13
[13] => 1
)
[1] => Array
(
[0] => 2014-06-18
[1] => 2014-06-10
[2] => 2014-06-05
[3] => 2014-06-05
[4] => 2014-06-12
[5] => 2014-06-11
[6] => 2013-12-12
[7] => 2014-07-23
[8] => 2014-05-13
[9] => 2014-06-01
[10] => 2014-06-12
[11] => 2014-06-04
[12] => 2014-06-04
[13] => 2014-06-11
)
)
)
I hope that what I understood is what you're really asking for. let's say your array definition is :
$arr = Array
(
0 => Array
(
0 => Array
(
0 => 4,
1 => 4,
2 => 7,
3 => 1,
4 => 7,
5 => 7,
6 => 3,
7 => 3,
8 => 4,
9 => 9,
10 => 12,
11 => 2,
12 => 13,
13 => 1
),
1 => Array
(
0 => "2014-06-18",
1 => "2014-06-10",
2 => "2014-06-05",
3 => "2014-06-05",
4 => "2014-06-12",
5 => "2014-06-11",
6 => "2013-12-12",
7 => "2014-07-23",
8 => "2014-05-13",
9 => "2014-06-01",
10 => "2014-06-12",
11 => "2014-06-04",
12 => "2014-06-04",
13 => "2014-06-11"
)
)
);
If you need to test if the date is lower than 6 month and then put their id, sales and date you need to use this code
$result = [];
$index=0;
foreach ($arr[0][0] as $key => $value)
{
$date1 = new DateTime($arr[0][1][$key]);
$date2 = new DateTime();
$diff = $date1->diff($date2);
$diff = ($diff->format('%y') * 12) + $diff->format('%m');
if($diff<=6)
{
$result[$index]['id'] = $key;
$result[$index]['sales'] = $value;
$result[$index]['date'] = $arr[0][1][$key];
$index++;
}
}
var_dump($result);
array_count_values() will give you the number of times each value appears in array 1.
$count = array_count_values($array[0][0]);
Result:
Array
(
[4] => 3
[7] => 3
[1] => 2
[3] => 2
[9] => 1
[12] => 1
[2] => 1
[13] => 1
)
Then you can use a loop to combine with array 2:
$result = array();
foreach($count as $key=>$val) {
$result[$array[0][1][$key]] = $val;
}
Result:
Array
(
[2014-06-12] => 3
[2014-07-23] => 3
[2014-06-10] => 2
[2014-06-05] => 1
[2014-06-01] => 1
[2014-06-04] => 1
[2014-06-11] => 1
)
See demo
I have array format like:
Array
(
[Australia] => Array
(
[0] => [1990,0.01],
[1] => [1991,0.02],
[2] => [1992,0.02],
[3] => [1993,0.02],
[4] => [1994,0.02],
[5] => [1995,0.02],
[6] => [1996,0.02],
[7] => [1997,0.02],
[8] => [1998,0.02],
[9] => [1999,0.02],
[10] => [2000,0.02],
[11] => [2001,0.02],
[12] => [2002,0.02],
[13] => [2003,0.02],
[14] => [2004,0.02],
[15] => [2005,0.02],
[16] => [2006,0.02],
[17] => [2007,0.02],
[18] => [2008,0.02],
[19] => [2009,empty],
[20] => [2010,empty],
[21] => [2011,empty],
[22] => [2012,empty],
[23] => [2013,empty],
[24] => [2014,empty],
[25] => [2015,empty]
)
[Pakistan] => Array
(
[0] => [1990,0.00],
[1] => [1991,0.00],
[2] => [1992,0.00],
[3] => [1993,0.00],
[4] => [1994,0.00],
[5] => [1995,0.00],
[6] => [1996,0.00],
[7] => [1997,0.00],
[8] => [1998,0.00],
[9] => [1999,0.00],
[10] => [2000,0.00],
[11] => [2001,0.00],
[12] => [2002,0.00],
[13] => [2003,0.00],
[14] => [2004,0.01],
[15] => [2005,0.01],
[16] => [2006,0.00],
[17] => [2007,0.00],
[18] => [2008,0.00],
[19] => [2009,empty],
[20] => [2010,empty],
[21] => [2011,empty],
[22] => [2012,empty],
[23] => [2013,empty],
[24] => [2014,empty],
[25] => [2015,empty]
)
)
and i want to replace 'empty' with 0 without change the array structure and elements position. I stuck how to do..
You can use array_walk_recursive function:
function replace_empty(&$item, $key) {
$item = str_replace('empty', '0', $item);
}
array_walk_recursive($your_array, 'replace_empty');
You could use the array_walk_recursive function, with a callback function that would replace empty by 0.
For example, considering your array is declared this way :
$myArray[0] = array(23, empty, 43, 12);
$myArray[1] = array(empty, empty, 53, 19);
Note : I supposed you made a typo, and your arrays are not containing only a string, but several sub-elements.
You could use this kind of code :
array_walk_recursive($myArray, 'replacer');
var_dump($myArray);
With the following callback functon :
function replacer(& $item, $key) {
if ($item === empty) {
$item = 0;
}
}
Note that :
the first parameter is passed by reference !
which means modifying it will modify the corresponding value in your array
I'm using the === operator for the comparison
And you'd get the following output :
array(
0 =>
array
0 => int 23
1 => int 0
2 => int 43
3 => int 12
1 =>
array
0 => int 0
1 => int 0
2 => int 53
3 => int 19)
I would foreach in both indices (not tested):
foreach($array as $country){
foreach($country as &$field){
if($field[1] == 'empty'){
$field[1] = 0;
}
}
}
(I assume empty is a string)
EDIT:
If this [1990,0.00] is not an array but a string, you could use str_replace instead
foreach($array as $country){
foreach($country as &$field){
$field = str_replace('empty', '0.00', $field);
}
}
}