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
);
Related
I have this array:
Array (
[0] => 1
[1] => 0
[2] => 0.32593699614063
[3] => -0.010875407736967
[4] => 0.32593699614063
[5] => 0.325936996140630.32593699614063
[6] => 0.1135301278
[7] => -0.028758634562
[8] => 0.044068247856859
[9] => -0.028758634562
[10] => 0
[11] => -0.01759558330449
[12] => 0
[13] => 0
[14] => 0
[15] => 0.05005732991382
[16] => 0.17093532486612
[17] => 0.098872325264
[18] => 0.346220929
[19] => 0.098872325264
[20] => 0.38405017865388
[21] => 0.098872325264
[22] => 0.64744282045057
[23] => 0.098872325264
[24] => 1
[25] => 0
)
I need to convert it to the following string:
1 0, 0.32593699614063 -0.010875407736967, 0.32593699614063 0.32593699614063, etc.
In other words, there should be a comma after every two elements.
Can you suggest the php code? Currently I have string without commas after every two elements.
Code:
$od = array_flatten($MultiDimensional)
$result = implode(" ",$od);
Output:
1 0 0.32593699614063 -0.010875407736967 0.32593699614063 -0.028758634562 0.1135301278 -0.028758634562 0.044068247856859
You can use array_chunk with implode
$arr = [1,2,3,4,5,6,7,8,9,10];
$res= '';
foreach(array_chunk($arr, 2) as $key => $value){
$res .= ($key < count(array_chunk($arr, 2))-1) ? (implode(' ',$value).",") : implode(' ',$value);
}
Live Demo
You can combine array_chunk, array_map and implode :
$od = array(1, 0, 0.32593699614063, -0.010875407736967, 0.32593699614063, -0.028758634562, 0.1135301278, -0.028758634562, 0.044068247856859);
echo implode(',' , // each pair is separated by a comma
array_map(function($pair){return implode(' ', $pair);}, // each element in pair separated by a space
array_chunk($od, 2) // cut original array in blocks of 2 items
)
);
Using cURL:
$url = $some_site;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
where the '$output' variable contains the following 7 columns (# of rows will vary):
1,a,6045,6168,6731,6847,522800
2,b,7847,8124,7645,7716,614400
3,c,7288,7633,7150,7442,801800
4,d,5546,5791,5460,5581,554200
5,e,4579,4679,4359,4572,557400
etc ...
As you can see, in the 4th column, the numbers are:
6168
8124
7633
5791
4679
And the highest number is: 8124
I am trying to figure out how to parse the '$output' variable so I can evaluate all the numbers in the 4th column to determine the high.
I have tried:
$csv = array_map('str_getcsv', $output);
echo max($csv[4]);
But nothing is returned
If you output your $csv value you will see that it is array with structure like:
...
[2] => Array
(
[0] => 3
[1] => c
[2] => 7288
[3] => 7633
[4] => 7150
[5] => 7442
[6] => 801800
)
[3] => Array
(
[0] => 4
[1] => d
[2] => 5546
[3] => 5791
[4] => 5460
[5] => 5581
[6] => 554200
)
[4] => Array
(
[0] => 5
[1] => e
[2] => 4579
[3] => 4679
[4] => 4359
[5] => 4572
[6] => 557400
)
....
So your $csv[4] is not a list of values from column 4. It's the 5th element of your array. So you need to go further, either with a simple foreach:
$max = 0;
foreach ($csv as $row) {
// use index `3` because numeration starts with `0`
if ($max < $row[3]) {
$max = $row[3];
}
}
Or with array_column (since php5.5):
// take all values witn index `3` and find max
echo max(array_column($csv, 3));
In both cases it is 8124
It's because str_getcsv not following standards, and from output You have a "broken array"... I suppose that it may look like this:
Array
(
[0] => 1
[1] => a
[2] => 6045
[3] => 6168
[4] => 6731
[5] => 6847
[6] => 522800
2
[7] => b
[8] => 7847
[9] => 8124
[10] => 7645
[11] => 7716
[12] => 614400
3
[13] => c
[14] => 7288
[15] => 7633
[16] => 7150
[17] => 7442
[18] => 801800
4
[19] => d
[20] => 5546
[21] => 5791
[22] => 5460
[23] => 5581
[24] => 554200
5
[25] => e
[26] => 4579
[27] => 4679
[28] => 4359
[29] => 4572
[30] => 557400
)
http://sandbox.onlinephpfunctions.com/code/b5834d98eda11b1726adc9ef1b7592f9ba32242c
You can do it like this:
<?php
// $csv is just a string ... output from Your curl will be same I think
$csv = <<<EOT
1,a,6045,6168,6731,6847,522800
2,b,7847,8124,7645,7716,614400
3,c,7288,7633,7150,7442,801800
4,d,5546,5791,5460,5581,554200
5,e,4579,4679,4359,4572,557400
EOT;
function csv2array($csv) {
$csv = str_getcsv($csv, PHP_EOL);
return array_map('str_getcsv', $csv);
}
function findMaxInColumn($array, $column = 3) {
return max(array_column($array, $column));
}
$parsedCsv = csv2array($csv);
print_r(findMaxInColumn($parsedCsv));
http://sandbox.onlinephpfunctions.com/code/82020f0d85acf8d6b15b5e7fd78a8d1a80618ff2
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
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 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;
}
}