Is there any way to transform an array looking like this:
Array ( [level-1] => 2 [quarter-1] => 1 [year-1] => 2014 [level-2] => 2 [quarter-2] => 2 [year-2] => 2015 [level-49] => 2 [quarter-49] => 2 [year-49] => 2015 [level-58] => 1 [quarter-58] => 1 [year-58]
and take only the numbers after the keywords to use them in an sql table. For example, the table would look like:
ID 1 Level 2 Quarter 1 Year 2014
ID 2 level 2 quater 2 Year 2015
ID 49 level 2 quarter 2 year 2015
Etc
I tried
if(!empty($_POST)){
print_r ($_POST);
echo "<br/><br/>";
$s=$_POST;
echo $abc= implode(',', $s);
for($a=0;$a<count($s);$a++){
$ar=explode(',',$abc);
echo $var=$ar[$a];
}
}
But the result i get is something like :
2,1,2014,2,2,2015,2,2,2015...
I need also the ID to be shown. But i most importantly do not know how to interpret the results to put them in the db..
Try this :
$array = array ( "level-1" => 2, "quarter-1" => 1, "year-1" => 2014, "level-2" => 2, "quarter-2" => 2, "year-2" => 2015, "level-49" => 2, "quarter-49" => 2, "year-49" => 2015, "level-58" => 1, "quarter-58" => 1, "year-58"=>2016);
foreach(array_chunk($array,3,true) as $val){
foreach($val as $k=>$v){
if(strpos($k, "level") !== false){
$temp = explode("-",$k);
$id = $temp[1];
$level = $v;
}
if(strpos($k, "quarter") !== false){
$quarter = $v;
}
if(strpos($k, "year") !== false){
$year = $v;
}
}
echo "ID ".$id." Level ".$level." Quarter ".$quarter." Year ".$year;
echo "<br>";
}
Output :
ID 1 Level 2 Quarter 1 Year 2014
ID 2 Level 2 Quarter 2 Year 2015
ID 49 Level 2 Quarter 2 Year 2015
ID 58 Level 1 Quarter 1 Year 2016
I don't know to code in PHP, so i will explain you the logic for doing it.
for(int i=0;i<array.length;i=i+3)
{
//get value from array[i]; get the level-x field and retrieve the x value, then extract integers accordingly from it after keywords. In java StringTokenizer can be used for this, or regex etc
//again get value from array[i+1] and extract the integer
//get the value from array[i+2] and do the same as above.
}
If the array is shuffled, you can match it with the level-x to get the exact tuple to insert. So level-x, the x value acts as an index that groups all related value.
Related
$dates = array('2017-03-24 01:48:09', '2017-03-24 11:48:09', '2017-04-07 01:12:19', '2017-04-14 01:49:09', '2017-04-21 01:45:09', '2017-04-28 01:38:09');
if given array above..
i'm making a report to monitor user growth monthly.. i want to count the no. of entries per month
sample April 2 counts
$dates = array('2017-03-24 01:48:09', '2017-03-24 11:48:09', '2017-04-07 01:12:19', '2017-04-14 01:49:09', '2017-04-21 01:45:09', '2017-04-28 01:38:09');
$count = array();
foreach ($dates as $d) {
$count[date('m', strtotime($d))]++;
}
print_r($count);
Output will be:
Array ([03] => 2 [04] => 4 )
That is: 03 (March) has 2 values in given array.
If you need 03 as March, then use:
$count[date('F', strtotime($d))]++;
Then, output will be:
Array ([March] => 2 [April] => 4 )
<?php
$dates = array('2017-03-24 01:48:09', '2017-03-24 11:48:09', '2017-04-07 01:12:19', '2017-04-14 01:49:09', '2017-04-21 01:45:09', '2017-04-28 01:38:09');
foreach($dates as $date)
$year_months[] = date('Y-m', strtotime($date));
var_export($year_months);
var_export(array_count_values($year_months));
Output:
array (
0 => '2017-03',
1 => '2017-03',
2 => '2017-04',
3 => '2017-04',
4 => '2017-04',
5 => '2017-04',
)array (
'2017-03' => 2,
'2017-04' => 4,
)
I currently have a simple WHILE loop running as follows:
while($los = $result->fetch_row())
{
echo"<tr><td>".$los[0]."</td>";
echo"<td>".$los[1]."</td>";
echo"<td>".$los[2]."</td></tr>";
}
The contents of $los is as follows:
London => 15 => 32
Glasgow => 23 => 45
Leeds => 1 => 12
Truro => 5 => 23
All working fine but what I am struggling with is that I have a second array $outs, the contents of which are:
London => 3
Glasgow => 5
Liverpool => 2
Poole => 1
Within the first while loop I am trying to subtract the value for $outs from $los when the location name matches. I have tried adding the following inside the while loop but no joy:
if($los[0] == $outs[0]){
$los[1] = $los[1]-$outs[1];
}
But no joy, also when I have tried print_r($outs) from within the while loop, all it returns is:
Poole => 1
Poole => 1
Poole => 1
Poole => 1
I cannot fathom where I am going wrong or even if this is possible. Is the $outs array being modified as it's within the first loop? Any ideas, suggestion or pointers welcomed on how I might achieve this.
VAR DUMPS
So, as requested the contents of the two arrays are:
$los
London => 15 => 32
Glasgow => 23 => 45
Leeds => 1 => 12
Truro => 5 => 23
$outs
London => 3
Glasgow => 5
Liverpool => 2
Poole => 1
However, when I vardump or print_r for $outs when it is in the while($los = $result->fetch_row()) the contents are:
Poole => 1
Poole => 1
Poole => 1
Poole => 1
FURTHER CODE
The code to obtain the array for $outs is:
$query19 = "SELECT Country, COUNT(Country), Resp FROM `tresults_` WHERE q39 = 'Complete' GROUP BY Country;";
$result19 = $mysqli->query($query19);
while($row19 = $result19->fetch_assoc()){
$outs = $row19;
}
Please consider to post the RAW Content that was produced by var_dump and include a full but minimal script to produce the error.
What is unclear here seem to be if $outs is a (1) Key-Value Array or a if it's (2) containing sub-arrays.
Option 1:
$outs = [
"London" => 3,
"Glasgow" => 5,
"Liverpool" => 2,
"Poole" => 1
];
Option 2:
$outs = [
["London", 3],
["Glasgow", 5],
["Liverpool", 2],
["Poole", 1]
];
I did for testing assume your input rows look like this:
$citys = [
["London", 15, 32],
["Glasgow", 23, 45],
["Leeds", 1, 12],
["Truro", 5, 23]
];
Depending on this you sould adjust the if inside your while loop.
For option 1:
foreach ($citys as $los) {
if (array_key_exists($los[0], $outs)) {
$los[1] = $los[1] - $outs[$los[0]];
}
echo " <tr><td > " . $los[0] . "</td > ";
echo "<td > " . $los[1] . "</td > ";
echo "<td > " . $los[2] . "</td ></tr > ";
}
For option 2 it's a little bit more code, as we need to search for the cityname index inside $outs first. In php > 5.5 it could be done using array_column which would be easier.
foreach ($citys as $los) {
$indexInOuts = null;
foreach($outs as $index => $out){
if($los[0] === $out[0]){
$indexInOuts = $index;
break;
}
}
if($indexInOuts !== null){
$los[1] = $los[1] - $outs[$indexInOuts][1];
}
echo " <tr><td > " . $los[0] . "</td > ";
echo "<td > " . $los[1] . "</td > ";
echo "<td > " . $los[2] . "</td ></tr > ";
}
The output for both options would be like:
London 12 32
Glasgow 18 45
Leeds 1 12
Truro 5 23
And as stated in the comments you are replacing the $outs every time. So edit it to look like this:
$outs = [];
while ($row19 = $result19->fetch_assoc()) {
$outs[] = $row19;
}
Given an array and I need to to display the average after every seventh row
Array
(
[0] => Array
(
[date] => 01-03-2015
[site_id] => 1
[starting_reading] => 567
[close_reading] => 567
)
[1] => Array
(
[date] => 03-03-2015
[site_id] => 1
[starting_reading] => 567
[close_reading] => 567
)
[2] => Array
(
[date] => 08-03-2015
[site_id] => 1
[starting_reading] => 567
[close_reading] => 567
)
)
Now what i need, is to display all avg reading in 7 days like:
1 to 7
--------------------- avg=close-start----------
8 to 14
--------------------- avg=close-start----------
15 to 21
--------------------- avg=close-start----------
22 to 28
--------------------- avg=close-start----------
29 to 31
--------------------- avg=close-start----------
date starting_reading close_reading
01-03-2015
03-03-2015
--------------------- avg=close-start----------
08-03-2015
--------------------- avg=close-start----------
//$arr is as the array you described
foreach($arr as $k => $v){
//$k will be some integer 0...count($arr)-1 assuming a 'normal' index
//$v is your child array, where you can access attributes such as $v['date']
//every seventh row can be done in a number of ways, the easiest way is:
if($k % 7 == 0){
//then show new line. This will happen every time the number divides into seven. 0, 7, 14, 21... etc.
var_dump($v); will output your child of the seventh (multiple) item
}
}
Including your example for dates and averages assuming array is correctly formatted
//$arr is as the array you described
$close = 0;
$start = 0;
foreach($arr as $k => $v){
//$k will be some integer 0...count($arr)-1 assuming a 'normal' index
//$v is your child array, where you can access attributes such as $v['date']
//every seventh row can be done in a number of ways, the easiest way is:
if($k % 7 == 0){
$close = 0; //reset close
$start = 0; //reset start
}
$close += (int)$v['close_reading'];
$start += (int)$v['starting_reading'];
if(($k + 1) % 7 == 0){ //this is the last row, 6, 13, 20 etc.
echo ($k-5).' to '.($k+1);
echo 'Average: '.(($close - $start) / 7);
}
}
I was wondering if there is a better solution to loop through an array from mid to end then from start to mid. Particularly for an associative array.
So for example if there is an associative array with the keys
$dow = array(Mon => etc, Tue => etc, Wed => etc, Thr => etc .. to .. Sun => etc).
I would start searching the array from Thurs to find the next day with something specific which could be anyday but happens to be on Tues, I usually iterate from Thurs (by index) to Sunday then, reset and start again from Monday to Wed and find the target when reaching Tues.
I count the index via an id and when it reaches 6 reset the id to 0
$id = 3 // Found day is Thursday id
//Loop function starts here
$id++; // start search from one day above found day
if ($id >= 6){ //when reaching Sunday
$id = 0 // start search from monday
}
// check array here for that specific thing
So the question is to ask if there is a more simple solution than this, ie split array from index thursday to sunday and add it onto the beginning of the array and then do the loop without having to count an index or if there are any other solutions without using the count index.
You can try with array_splice:
$array = array(1,2,3,4,5,6,7);
$lastDays = array_splice($array, 3);
$firstDays = $array;
print_r(array('first days' => $firstDays, 'last days' => $lastDays));
If the day is not in $lastDays (use a boolean like $matchFound) then you would search in $firstDays.
Or just use with array_merge:
$array = array(1,2,3,4,5,6,7);
$array = array_merge(array_splice($array, 3), $array);
print_r($array);
where output is:
Array
(
[0] => 4
[1] => 5
[2] => 6
[3] => 7
[4] => 1
[5] => 2
[6] => 3
)
and then you can search with a foreach.
maybe a foreach would be more efficient. hope this helps.
$id = 3 // Found day is Thursday id
$i =0;
//Loop function starts here
foreach($dow as $idx)
if($id == $i):
else if($i > $id):
endif;
if($i == count($dow)){$i=0;}else{$i++;}
endforeach;
<?php
$dow = array("mon","tue","wed","thu","fri","sat","sun");
$pick_a_day = 2; // user input; NON zero based. monday = 1
$pick_a_day--; // make it zero based to use in code
foreach($dow as $inc => $an_element )
{
echo $dow[($pick_a_day+(count($dow))) % (count($dow))]." - loop number:".($inc+1)."\n";
$pick_a_day++;
}
?>
output
tue - loop number:1
wed - loop number:2
thu - loop number:3
fri - loop number:4
sat - loop number:5
sun - loop number:6
mon - loop number:7
Maybe not the best solution, but a solution:
function half($array) {
$h = sizeof($array) / 2;
$a1 = array_splice($array, $h);
$a2 = array_splice($array, 0);
$res = array_merge($a1, $a2);
return $res;
}
$dow = array('Mon' => 'etc1', 'Tue' => 'etc2', 'Wed' => 'etc3', 'Thr' => 'etc4', 'Fri' => 'etc5', 'Sat' => 'etc6', 'Sun' => 'etc7');
$b = half($dow);
Now you can go through with a foreach, or how would you like. Result of $b:
Array
(
[Thr] => etc4
[Fri] => etc5
[Sat] => etc6
[Sun] => etc7
[Mon] => etc1
[Tue] => etc2
[Wed] => etc3
)
I'm hoping there's an easy way to do this without tons and tons of loops.
I have a matrix in the following manner:
Weight1 Weight2 Weight3 .... WeightN
Jan 1 3 5 4
Feb 10 12 15 11
Mar 5 7 4 3
Apr 10 15 7 3
Assuming the following array:
$arrayMonths = array(
'jan' => array(1, 3, 5,4)
'feb' => array(10,12,15,11)
'mar' => array(5, 7, 4, 3)
'apr' => array(10,15,7,3)
);
What is the best way for me to get the the max for each row:
jan = 5
feb = 15
mar = 7
apr = 15
Thanks
Use the max() function:
foreach($arrayMonths as $month => $row)
{
echo $month . ": " . max($row) . "<br />";
}
foreach ($arrayMonths as $month => $array) {
// store the max of each month for later
// $max['jan'] = 5
$max[$month] = max($array);
// or print it out
echo $month. ' => '. max($array);
}
From the max() PHP Docs
If you need to go further with sorting you can check out here for more information
PHP Array Sorting
Try this:
<?php
function getMax( $v, $k ) {
global $arrayMonths;
$arrayMonths[ 'max' ][$k] = max($v);
}
$arrayMonths = array(
'jan' => array(1, 3, 5,4),
'feb' => array(10,12,15,11),
'mar' => array(5, 7, 4, 3),
'apr' => array(10,15,7,3)
);
array_walk( $arrayMonths, 'getMax' );
// Now $arrayMonths['max'] will contain all max values for each key
?>
Hope this helps.
I found the perfect algorithm: Basically use a recursive function which goes through all permutations.