i have array data like this.
[0] => Array (
[id] => 1
[id_requestor] => 1
[jam_input] => 2015-06-20 06:00:00
[jam_pakai] => 2015-06-28 08:00:00
[total_poin] => 30
)
[1] => Array (
[id] => 2
[id_requestor] => 2
[jam_input] => 2015-06-20 07:00:00
[jam_pakai] => 2015-06-28 08:00:00
[total_poin] => 10
)
[2] => Array (
[id] => 3
[id_requestor] => 3
[jam_input] => 2015-06-20 06:30:00
[jam_pakai] => 2015-06-28 08:00:00
[total_poin] => 5
)
In above data, there is total_poin. This total_poin that i use later.
I want to sort total_poin array descending.
But i wont to use php built in array function.
Cause i have to use method from research paper like this.
for i=0 to i<main queue.size
if jobi+1 length > jobi length then
add jobi+1 in front of job i in the queue
end if
if main queue.size = 0 then
add job i last in the main queue
end if
And here is my implementation :
function LJFAlgorithm($time_str) {
$batchData = getBatch($time_str);
$ljf = [];
for ($i=0; $i < count($batchData)-1; $i++) {
echo $batchData[$i+1]['total_poin'] ." >= ". $batchData[$i]['total_poin'] . " = " . ($batchData[$i+1]['total_poin'] >= $batchData[$i]['total_poin']);
if ($batchData[$i+1]['total_poin'] >= $batchData[$i]['total_poin']) {
echo ", Add " . $batchData[$i+1]['total_poin'] . " in front of " . $batchData[$i]['total_poin'];
} else {
echo ", Add " . $batchData[$i]['total_poin'] . " in front of " . $batchData[$i+1]['total_poin'];
}
echo '<br/>';
}
print_r($ljf);
}
But it not work perfectly, i get one data missing.
Here is my output code :
10 >= 30 = , Add 30 in front of 10
5 >= 10 = , Add 10 in front of 5
5 value not added in array.
How to fix that?
Thank you very much.
Try this
for ($i=0; $i <= count($batchData)-1; $i++)
Related
I have a multidimensional array search
my array looks like this
Array
(
[0] => Array
(
[id] => 1
[location] => 3
[location_fees] => 3
[gross_percentage] => 25
[transaction_percentage] => 0
[user_name] => admin
[user_id] => 1
[gross] => yes
[transaction] => no
)
[1] => Array
(
[id] => 2
[location] => 5
[location_fees] => 5
[gross_percentage] => 0
[transaction_percentage] => 24
[user_name] => admin
[user_id] => 1
[gross] => no
[transaction] => yes
)
[2] => Array
(
[id] => 3
[location] => 2
[location_fees] => 5
[gross_percentage] => 10
[transaction_percentage] => 0
[user_name] => admin
[user_id] => 1
[gross] => yes
[transaction] => no
)
)
i use the following php, i know it can probably be done cleaner or a lot less code so if you have any ideas how to get the same result i am def all about learning and i am all ears!
so here is the PHP i use:
$key = false;
$search = ['gross' => 'yes'];
foreach ($results as $k => $v) {
if ($v['gross'] == $search['gross'] ) {
$key = $k;
$location_fees = array_search('yes', array_column($results, 'gross','location_fees'));
echo "The location fees: ". $location_fees ." % <br><br>";
$gross_percentage = array_search('yes', array_column($results, 'gross', 'gross_percentage'));
echo "The gross_percentage is: ".$gross_percentage ."% <br><br>";
} else {
$tran_perc = array_search('yes', array_column($results, 'transaction', 'transaction_percentage'));
echo "The locations percentage is: ".$tran_perc ." % <br>";
$the_loc = array_search('yes', array_column($results, 'transaction', 'location'));
echo "The location is: ".$the_loc ." <br>";
$location_fees = array_search('no', array_column($results, 'gross','location_fees'));
echo "The location fees: ". $location_fees ." % <br><br>";
}
}
and my results on the page look like this:
key 0
The location fees: 3 %
The gross_percentage is: 25%
key 1
The locations percentage is: 24 %
The location is: 5
The location fees: %
key 2
The location fees: 8 %
The gross_percentage is: 25%
it refuses to show the locations fee from key #1
NOW the strange parts is, if i change the location fees to say 3, it then shows up. but it will not with the number "5" which is also the location #
Is there a reason this is a conflict with JUST that number "5"? Notice Key "0" has location of "3" and location fees of "3" and it does not cause any issues.
I have been stuck on this for hours, it will work for keys # 0 and #2 without any issues. Any ideas?
The reason you are running into this issue is your call to array_column:
array_column($results, 'gross','location_fees')
this causes the gross values from $results to be re-indexed by the location_fees values, which for your data would result in something like
[3 => 'yes', 5 => 'no', 5 => 'yes']
As you can see, you have two 5 numeric keys, which is not valid, so the second one overwrites the first and you end up with
[3 => 'yes', 5 => 'yes']
and your array_search for no fails, hence you get no results. You will have this problem everywhere you get a replicated value.
I'm not sure why you are taking this approach anyway. It seems you have the numbers you want in $v anyway:
$search = ['gross' => 'yes'];
foreach ($results as $k => $v) {
echo "key $k<br>" . PHP_EOL;
if ($v['gross'] == $search['gross'] ) {
$location_fees = $v['location_fees'];
echo "The location fees: ". $location_fees ." % <br><br>" . PHP_EOL;
$gross_percentage = $v['gross_percentage'];
echo "The gross_percentage is: ".$gross_percentage ."% <br><br>" . PHP_EOL;
} else {
$tran_perc = $v['transaction_percentage'];
echo "The locations percentage is: ".$tran_perc ." % <br>" . PHP_EOL;
$the_loc = $v['location'];
echo "The location is: ".$the_loc ." <br>" . PHP_EOL;
$location_fees = $v['location_fees'];
echo "The location fees: ". $location_fees ." % <br><br>" . PHP_EOL;
}
}
Output:
key 0<br>
The location fees: 3 % <br><br>
The gross_percentage is: 25% <br><br>
key 1<br>
The locations percentage is: 24 % <br>
The location is: 5 <br>
The location fees: 5 % <br><br>
key 2<br>
The location fees: 5 % <br><br>
The gross_percentage is: 10% <br><br>
Demo on 3v4l.org
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 5 years ago.
I need to sort the multidimensional array with output by value. I can not group the nested arrays by date and output it with the title (date).
There is an array:
Array (
[0] => (
[ID] => 959
[title] => title
[post_date] => 2018-01-01 10:17:49
)
[1] => (
[ID] => 960
[title] => title
[post_date] => 2018-01-01 10:17:49
)
[2] => (
[ID] => 961
[title] => title
[post_date] => 2018-01-02 10:17:49
)
[3] => (
[ID] => 962
[title] => title
[post_date] => 2014-01-02 10:17:49
)
[4] => (
[ID] => 963
[title] => title
[post_date] => 2014-01-03 10:17:49
)
)
As a result, I need to get this.
There is a date as the header and all arrays in which this date.
Result
- 2018-01-01 -
id: 959 Title
id: 560 Title
- 2018-01-02 -
id: 961 Title
id: 562 Title
- 2018-01-02 -
id: 963 Title
.....
have you done with part about sorting? I assume you are. So the only thing left is "grouping".
But actually you don't need to group data. Just iterate sorted items one by one and remember date from previous element. Once dates for current element and for previous one differ - you output "header". Something like that:
$data = [[a=> 2, b=> 1], [a=> 2, b=> 4], [a=> 3, b=> 1], [a=> 4, b=> 0]];
$previous_a = null;
foreach($data as $item) {
if ($item['a'] != $previous_a) {
echo 'header --- '.$item['a'].'<br />';
}
$previous_a = $item['a'];
echo $item['b'].'<br />';
}
You could try something like this :
$aggreg = [] ;
foreach ($array as $item) {
$day = $item['post_date'];
// if you want to group by day, just use $day = substr($day,0,10) ;
$aggreg[$day][] = 'id: ' . $item['ID'] . " " . $item['title'] ;
}
krsort($aggreg) ; // or ksort() or just comment this line.
// then build the output :
$output = "" ;
foreach ($aggreg as $day => $data) {
$output .= "- " .substr($day,0,10) . " -\n\n" . implode("\n", $data) . "\n\n" ;
}
print $output ;
Will outputs :
- 2018-01-02 -
id: 961 title
- 2018-01-01 -
id: 959 title
id: 960 title
- 2014-01-03 -
id: 963 title
- 2014-01-02 -
id: 962 title
NB If you have to output this in HTML, please change \n to <br>
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;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This is a little bit confusing on why this won't work. I can do a work around, but any ideas? It's to match an uploaded CSV to a configuration in a database. Returns only the first 5 columns. The rest, user specified ones (should print Echo) do not show up.
$csv_col while looping =
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
$getConfiguration[0] =
Array
(
[config_key] => 22
[config_client_ident] => 105
[config_s_timestamp] => 1378008000
[config_e_timestamp] =>
[config_kpi_1] => 81
[config_kpi_2] => 82
[config_kpi_3] => 83
[config_kpi_4] => 84
[config_kpi_5] =>
[config_kpi_6] =>
[config_kpi_7] =>
[config_kpi_8] =>
[config_kpi_9] =>
[config_kpi_10] =>
[config_kpi_11] =>
[config_kpi_12] =>
[config_kpi_13] =>
[config_kpi_14] =>
[config_kpi_15] =>
[config_kpi_16] =>
[config_kpi_17] =>
[config_kpi_18] =>
[config_kpi_19] =>
[config_kpi_20] =>
[config_kpi_21] =>
[config_kpi_22] =>
[config_kpi_23] =>
[config_kpi_24] =>
[config_kpi_25] =>
[config_kpi_26] =>
[config_kpi_27] =>
[config_kpi_28] =>
[config_kpi_29] =>
[config_kpi_30] =>
[config_kpi_31] =>
[config_kpi_32] =>
[config_kpi_33] =>
[config_kpi_34] =>
[config_kpi_35] =>
[config_kpi_36] =>
[config_kpi_37] =>
[config_kpi_38] =>
[config_kpi_39] =>
[config_kpi_40] =>
[config_kpi_41] =>
[config_kpi_42] =>
[config_kpi_43] =>
[config_kpi_44] =>
[config_kpi_45] =>
[config_kpi_46] =>
[config_kpi_47] =>
[config_kpi_48] =>
[config_kpi_49] =>
[config_kpi_50] =>
)
$csvRead[0]
Array
(
[0] => Data Date
[1] => Agent IDENT
[2] => Client IDENT
[3] => Location IDENT
[4] => Program IDENT
[5] => xx VSAT
[6] => xx SWSAT
[7] => xx NSODS
[8] => xx SWDSAT
)
Returned from query
Array
(
[0] => Array
(
[key_id] => 84
[key_enabled] => 1
[key_name] => xx SWDSAT
[key_desc] => xx CSAT Somewhat Dissatisfied
)
)
// Build the check arary
$checkArray = array("Data Date", "Agent IDENT", "Client IDENT", "Location IDENT", "Program IDENT");
$fileVerification = true;
$config_col = 1;
// Loop through the 50 available columns
while($config_col <= 50){
$csv_col = $config_col - 1;
print $csv_col . " ";
// Make sure something is in those columns
if($getConfiguration[0]['config_kpi_'.$config_col] != ''){
// There is, now lets query and get those names.
$getNamesQuery[] = "SELECT * FROM kpi_keys WHERE key_id=".$getConfiguration[0]['config_kpi_'.$config_col];
$getNamesResult = dbInsert($getNamesQuery);
// Compare the first 5 columns
if($csv_col < 5){
if($csvRead[0][$csv_col] == $checkArray[$csv_col]){
$output .= '<div class="messageGood"> - '.$csvRead[0][$csv_col].' column verified.</div>';
} else {
$output .= '<div class="messageBad"> - Column "'.$csvRead[0][$csv_col].'" did not match "'.$checkArray[$csv_col].'" in the current configuration.</div>';
$fileVerification = false;
}
} else {
// Now lets compare the customizable columns. We're going to do names and ID's to make sure.
print "Echo";
}
$getNamesQuery = null;
}
$config_col++;
}
// Now we check the information
print "<pre>";
print_r($getConfiguration[0]);
print_r($csvRead[0]);
print_r($getNamesResult);
print "</pre>";
Answer (Thanks Wrikken)
// Loop through the 50 available columns
while($config_col <= 50){
$csv_col = $config_col - 1;
$partOne = '';
$partTwo = '';
// Make sure something is in those columns
// Compare the first 5 columns
if($csv_col < 5){
if($csvRead[0][$csv_col] == $checkArray[$csv_col]){
$partOne .= '<div class="messageGood"> - '.$csvRead[0][$csv_col].' column verified.</div>';
} else {
$partOne .= '<div class="messageBad"> - Column "'.$csvRead[0][$csv_col].'" did not match "'.$checkArray[$csv_col].'" in the current configuration.</div>';
$fileVerification = false;
}
}
if($getConfiguration[0]['config_kpi_'.$config_col] != ''){
print $csv_col . " ";
// There is, now lets query and get those names.
$getNamesQuery[] = "SELECT * FROM kpi_keys WHERE key_id=".$getConfiguration[0]['config_kpi_'.$config_col];
$getNamesResult = dbInsert($getNamesQuery);
// Now lets compare the customizable columns. We're going to do names and ID's to make sure.
$partTwo = "Comparison";
$getNamesQuery = null;
}
$config_col++;
}
$output .= $partOne . $partTwo;
while($csv_col < 5){
if($csvRead[0][$csv_col] == $checkArray[$csv_col]){
$output .= '<div class="messageGood"> - '.$csvRead[0][$csv_col].' column verified.</div>';
if only runs one time,if you want to concatenate use while, it will run..until the end.
I've got a php script with following array (date,task,actor,hh:mm).
Array
(
[0] => 2013-01-29|Making movies|Laurel|07:30
[1] => 2013-01-29|Making movies|Hardy|00:30
[2] => 2013-01-29|Learning PHP|Hardy|07:00
[3] => 2013-01-29|Singing autographs|Keaton|07:30
[4] => 2013-01-29|Making movies|Lloyd|07:30
[5] => 2013-01-28|Learning PHP|Laurel|07:30
[6] => 2013-01-28|Making movies|Hardy|07:30
[7] => 2013-01-28|Learning PHP|Keaton|07:30
[8] => 2013-01-28|Making movies|Lloyd|07:30
[9] => 2013-01-27|Learning PHP|Laurel|05:30
[10] => 2013-01-27|Making movies|Laurel|02:30
[11] => 2013-01-27|Learning PHP|Hardy|07:30
[12] => 2013-01-27|Making movies|Keaton|07:30
[13] => 2013-01-27|Making movies|Lloyd|07:30
)
I'd like to create a filter that lists the tasks and sums the time values of each task, for example:
Learning PHP (<-selected option)
2013-01-29 Hardy 07:00
2013-01-28 Laurel 07:30
2013-01-28 Keaton 07:30
2013-01-27 Laurel 05:30
2013-01-27 Hardy 07:30
=======================
TOTAL 35:00
What would be the way to make this happen?
Any suggestions or next steps are more than welcome.
This would be easier to filter if you had a multidimensional array. Something like this would work for the current structure though:
$task = "Learning PHP";
foreach($arr as $k=>$v) {
$pieces = explode("|", $v);
if($pieces[1]==$task) {
$total += (int) str_replace(':','',$pieces[3]);
echo $pieces[0] . " " . $pieces[2] . " " . $pieces[3];
}
}
echo "Total: " . $total;