Related
I have an array
$info = array(
[0] => array(
'id' => 1,
'uid' => '677674e21aed487fd7180da4a7619a9d'
),
[1] => array(
'id' => 1,
'uid' => 'd3c98a10fe4e42fb1fe868008c0f4cc1'
),
[2] => array(
'id' => 1,
'uid' => 'd3c98a10fe4e42fb1fe868008c0f4cc1'
),
[3] => array(
'id' => 1,
'uid' => '658284e5395a29bf34d21f30a854e965'
),
[4] => array(
'id' => 1,
'uid' => '01f33ae45a463e0c1de4ad989b3ccad5'
),
[5] => array(
'id' => 1,
'uid' => '677674e21aed487fd7180da4a7619a9d'
)
)
As you can see, uid of 0th index and 5th index are same. Similarly, uid of 2nd index and 3rd index are same.
I want a PHP script by which I can randomly create one hexadecimal color code for duplicate uids. Say something like this.
$info = array(
[0] => array(
'id' => 1,
'uid' => '677674e21aed487fd7180da4a7619a9d',
'col' => 'black'
),
[1] => array(
'id' => 1,
'uid' => 'd3c98a10fe4e42fb1fe868008c0f4cc1',
'col' => 'green'
),
[2] => array(
'id' => 1,
'uid' => 'd3c98a10fe4e42fb1fe868008c0f4cc1',
'col' => 'green'
),
[3] => array(
'id' => 1,
'uid' => '658284e5395a29bf34d21f30a854e965'
),
[4] => array(
'id' => 1,
'uid' => '01f33ae45a463e0c1de4ad989b3ccad5'
),
[5] => array(
'id' => 1,
'uid' => '677674e21aed487fd7180da4a7619a9d',
'col' => 'black'
)
)
How can I do this with the most minimum execution time?
There might be various ways for doing this workout, but due to lack of proper response, I came up with this probable lengthier code. I am posting the answer here for people who might need this.
$uidArray = array(); // creating a blank array to feed each uid
$uidDuplicateArray = array(); // creating a blank array as container to hold duplicate uid(s) only
foreach($all_data as $key => $ad)
{
// iterate through each item of the list
/.................
.................. //
$uidArray[] = $ad['uid'];
}
foreach(array_count_values($uidArray) as $val => $c)
{
if($c > 1)
{
// if count value is more than 1, then its duplicate
// set the uid duplicate array with key as uid and unique color code as value
$uidDuplicateArray[$val] = sprintf('#%06X', mt_rand(0, 0xFFFFFF));
}
}
foreach($all_data as $keyAgain => $adg)
{
// iterating through each item of original data
if(isset($uidDuplicateArray[$adg['uid']]))
{
// if the uid is key of the duplicate array, feed the value to original array in a new key.
$all_data[$keyAgain]['color'] = $uidDuplicateArray[$adg['uid']];
}
}
Each comment associated with each LOC is self explanatory.
The reason I wanted this, is to mark the duplicates in UI like this:-
I've been scratching my head and failing miserably at coming up with a solution to my array structuring issue. I'm not sure exactly what part would be better to try and fix, the data being returned from SQL or the PHP array after the fact.
My SQL data is returned like this:
$i = 0;
while ( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC ) ) {
$colData[$i] = array(
'name' => $row['FULLNAME'],
'invoice' => $row['CUST_InvoiceNumber_020911544'],
array(
'service' => $row['CUST_Service_052400634'],
'date' => date_normalizer($row['CUST_ServiceDate_064616924']),
'service_amount' => $row['CUST_ServiceAmount_054855553'],
),
'do_all_for' => $row['CUST_DoAllFor_021206685'],
'memo' => $row['CUST_Memo_021614200'],
'paymenttype' => $row['CUST_PAYMENTTYPE_123838203'],
'deposit' => $row['CUST_DEPOSIT_124139703'],
'datepaid' => date_normalizer($row['CUST_DATEPAID_124941578']),
);
$i++;
}
And the resultant array has this structure:
array (
0 =>
array (
'name' => 'ABRAHAM PRETORIS',
'invoice' => '63954',
0 =>
array (
'service' => 'Tree Work',
'date' => '2015-01-22',
'service_amount' => '1305.00',
),
'do_all_for' => '4924.68',
'memo' => 'CHECK #947 $2400',
'paymenttype' => 'VISA',
'deposit' => '4429.48',
'datepaid' => '2015-02-09',
),
1 =>
array (
'name' => 'ABRAHAM PRETORIS',
'invoice' => '63954',
0 =>
array (
'service' => 'DRF',
'date' => '2015-01-22',
'service_amount' => '740.00',
),
'do_all_for' => '4924.68',
'memo' => 'CHECK #947 $2400',
'paymenttype' => 'VISA',
'deposit' => '4429.48',
'datepaid' => '2015-02-09',
),
2 =>
array (
'name' => 'ABRAHAM PRETORIS',
'invoice' => '63954',
0 =>
array (
'service' => 'Stumps',
'date' => '2015-01-26',
'service_amount' => '360.00',
),
'do_all_for' => '4924.68',
'memo' => 'CHECK #947 $2400',
'paymenttype' => 'VISA',
'deposit' => '4429.48',
'datepaid' => '2015-02-09',
),
Notice that I'm getting a new subarray for the same person because the sub-subarray (service, date & service_amount) has multiple values.
What I'm trying to accomplish is condensing the array so that I only have one array for "ABRAHAM PRETORIS" etc, but all of the different services listed as a sub array. I would like it to look like this:
array (
0 =>
array (
'name' => 'ABRAHAM PRETORIS',
'invoice' => '63954',
0 =>
array (
'service' => 'Tree Work',
'date' => '2015-01-22',
'service_amount' => '1305.00',
),
1 =>
array (
'service' => 'DRF',
'date' => '2015-01-22',
'service_amount' => '740.00',
),
2 =>
array (
'service' => 'STUMPS',
'date' => '2015-01-26',
'service_amount' => '360.00',
),
'do_all_for' => '4924.68',
'memo' => 'CHECK #947 $2400',
'paymenttype' => 'VISA',
'deposit' => '4429.48',
'datepaid' => '2015-02-09',
),
I've looked at tons of examples of nested foreach statements and php array functions but I just can't wrap my head around how to loop through and add the additional services to the array then proceed when it's a row with a different name and/or invoice number.
Thanks in advance for the help!!
First, make sure your SQL query has an order by name, invoice. That will ensure all the records you want to group are sequential.
Then you have to create a loop with some additional inner logic:
// Creates an array to hold the final array.
$result = array();
// This var will keep track of name changes.
$current_name = '';
while ( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC ) )
{
// Let's check if the name changed. This will be true for the first
// time the loop runs.
if($current_name != $row['FULLNAME'])
{
// If we are beginning, the if below will not run. But in subsequent
// records, it will add the acumulated array to the main result.
if($current_name != '') $result[] = $temp;
// The temp array will be populated with all data that DOES NOT change
// for the current name.
$temp = array('name' => $row['FULLNAME'],
'invoice' => $row['CUST_InvoiceNumber_020911544'],
'do_all_for' => $row['CUST_DoAllFor_021206685'],
'memo' => $row['CUST_Memo_021614200'],
'paymenttype' => $row['CUST_PAYMENTTYPE_123838203'],
'deposit' => $row['CUST_DEPOSIT_124139703'],
'datepaid' => date_normalizer($row['CUST_DATEPAID_124941578']),
);
// Update the current name.
$current_name = $row['FULLNAME'];
}
// The part that runs only on name changes has finished. From now on, we
// will take care of data which will be accumulated
// in a sub-array (until name changes and the block above resets it).
$temp['sub-array'][] =
array('service' => $row['CUST_Service_052400634'],
'date' => date_normalizer($row['CUST_ServiceDate_064616924']),
'service_amount' => $row['CUST_ServiceAmount_054855553']);
}
// After the loop, the last temp array needs to be added too.
$result[] = $temp;
This is the general concept: you will create a temporary array to hold the current name, inside which you will acummulate other data. Once the name changes, the acummulated data will be dumped to the main result, the temp array is reset, and a new acummulation begins.
I can't test the code right now, so it probably needs some fixes, but this approach works really well, and my point here is to show you the concept, so you can adapt it to your specific needs.
I am trying to return a collection of messages grouped by in_reply_to field, I have this code:
$result = $this->db->Message->aggregate(
array(
array(
'$project' => array('message' => 1, 'in_reply_to'=> 1, 'to_user' => 1, 'from_user' => 1)
),
array(
'$group' => array('_id' => '$in_reply_to'),
),
)
);
print_r($result);exit;
the result is:
Array (
[result] => Array (
[0] => Array (
[_id] => MongoId Object (
[$id] => 53a03d43b3f7e236470041a8
)
)
[1] => Array (
[_id] => MongoId Object (
[$id] => 53a03cbdb3f7e2e8350041bb
)
)
)
[ok] => 1
)
Ideally I'd like the entire Message object, but I did think that $project would be used to specify returns fields, even so, I dont get the fields I'm specifying.
Any help is greatly appreciated
In order to get all the messages in the thread you basically want to $push
$result = $this->db->Message->aggregate(
array(
array(
'$group' => array(
'_id' => '$in_reply_to',
'messages' => array(
'$push' => array(
'_id' => '$_id',
'message' => '$message',
'to_user' => '$to_user',
'from_user' =>'$from_user'
)
)
)
)
)
);
MongoDB 2.6 you have the $$ROOT variable that shortens this:
$result = $this->db->Message->aggregate(
array(
array(
'$group' => array(
'_id' => '$in_reply_to',
'messages' => array(
'$push' => '$$ROOT'
)
)
)
)
);
So that puts all of the related messages inside the "messages" array tied to that key.
Just as side note, while you can do this you may as well just sort the results by your "in_reply_to" field and process them that way looking for changes in the value to indicate a new thread.
Sorting with a find would be the fastest way to process, even if it does not conveniently put everything right under the one key.
If you want to get additional fields beside _id field, when using $group operator, you need to include them using some of the available accummulators like $first or $last. You can see the full list on the MongoDB $group documentation page.
The query will look like this:
$result = $this->db->Message->aggregate(
array(
array(
'$project' => array(
'message' => 1,
'in_reply_to'=> 1,
'to_user' => 1,
'from_user' => 1
)
),
array(
'$group' => array(
'_id' => '$in_reply_to',
'message' => array('$first' => '$message'),
'to_user' => ('$first' => '$to_user'),
'from_user' => ('$first' => '$from_user')
),
),
)
);
If the message, to_user and from_user values are same in all documents using $last instead of $first $last will produce the same results.
I have an array of variabel length, containing events sorted by startdate, looking like this:
Array
(
[0] => stdClass Object
(
[id] => 1978
[date] => 2012-09-29
)
[1] => stdClass Object
(
[id] => 1979
[date] => 2012-10-14
)
...etc....
I need to make a function that takes one of the event, and puts in the middle of a new array with a length of exactly seven, and puts the event's neighbours on each side.
So if event 5 passed to the function, the output should be:
[2][3][4][5][6][7][8]
If the first event is passed to the function, and the original amount of events is 12, the output should be:
[10][11][12][1][2][3][4]
If the orignal amount of events is 6, and the fifth event is passed, the output should be:
[2][3][4][5][6][1][]
So the list of events should always "wrap around" in the new array and fill it up as much as possible.
I have hacked together a solution, that involves quite a bit of steps. I dont really like it, and it got me wondering:
How would this be done in the most efficient and elegant way?
You need to change the values in this codes
define('TO_SHOW',7); // number of items to show in your case it is 7
$selected = 1; //// which one you need need at center.
and your array;
Execute this and try :
<?php
define('TO_SHOW',7); // number of items to show in your case it is 7
function change_order($arry, $sel){
$arr_cnt = count($arry);
$shift_count = $arr_cnt - (ceil(TO_SHOW/2)-$sel);
for($i=0; $i<$shift_count; $i++){
array_push($arry, array_shift($arry));
}
return array_slice($arry, 0, TO_SHOW);
}
$arr = array(array(
"id" => 1,
"date" => 2012-09-29
),
array(
"id" => 2,
"date" => 2012-09-29
),
array(
"id" => 3,
"date" => 2012-09-29
),
array(
"id" => 4,
"date" => 2012-09-29
),
array(
"id" => 5,
"date" => 2012-09-29
),
array(
"id" => 6,
"date" => 2012-09-29
),
array(
"id" => 7,
"date" => 2012-09-29
),
array(
"id" => 8,
"date" => 2012-09-29
),
array(
"id" => 9,
"date" => 2012-09-29
),
array(
"id" => 10,
"date" => 2012-09-29
),
array(
"id" => 11,
"date" => 2012-09-29
),
array(
"id" => 12,
"date" => 2012-09-29
),
array(
"id" => 13,
"date" => 2012-09-29
),
array(
"id" => 14,
"date" => 2012-09-29
)
);
$selected = 1; //// centre one
$test = change_order($arr, $selected);
echo "<pre>";
print_r($test);
?>
I have an array:
$initialarray = array(
0 = array(
'unit' => 1,
'class' => 1,
'value' => 'string1'
),
1 = array(
'unit' => 1,
'class' => 2,
'value' => 'string2'
),
2 = array(
'unit' => 1,
'class' => 2,
'value' => 'string3'
),
3 = array(
'unit' => 2,
'class' => 1,
'value' => 'string4'
)
4 = array(
'unit' => 2,
'class' => 2,
'value' => 'string5'
)
);
What would be the best way to structure it (to group the resulting sub-arrays) depending first on the 'unit' field's values, and then depending on the 'class' field's values, like so:
$resultarray = array(
// array of all the sub-arrays of 'unit' = 1
$unit[1] = array(
// array of all the sub-arrays of 'unit' = 1 and 'class' = 1
$class[1] = array(
0 = array(
'unit' => 1,
'class' => 1,
'value' => 'string1'
)
)
// array of all the sub-arrays of 'unit' = 1 and 'class' = 2
$class[2] = array(
0 = array(
'unit' => 1,
'class' => 2,
'value' => 'string2'
),
1 = array(
'unit' => 1,
'class' => 2,
'value' => 'string3'
)
)
)
// array of all the sub-arrays of 'unit' = 2
$unit[2] = array(
// array of all the sub-arrays of 'unit' = 2 and 'class' = 1
$class[1] = array(
0 = array(
'unit' => 2,
'class' => 1,
'value' => 'string4'
)
)
// array of all the sub-arrays of 'unit' = 2 and 'class' = 2
$class[2] = array(
0 = array(
'unit' => 2,
'class' => 2,
'value' => 'string5'
)
)
)
)
I have asked a similar question here and got a working answer for only one iteration, i.e. for only structuring the array by one of the fields. But I could not make the same solution work for multiple iterations, i.e. for more than one field.
Also, is there a solution to structure a multidimensional array depending on more than two fields?
I think it's not a way of asking the question. It is very simple , you can do this by playing with arrays,keys and etc.... So first you should try hard for the problem. After If you have any problem in the middle of your tries then you can ask that here. I have solved your problem here is the complete code , but next time please do some work and then only post the problem. Never ask for the code.
foreach ($initialarray as $key1=>$val1)
{
foreach ($val1 as $key2=>$val2)
{
if($key2=='unit')
{
$num=$val2;
if($val2!=$num)
$testarr['unit'.$val2]=array();
}
if($key2=='class')
{
$testarr['unit'.$num]['class'.$val2][]=$val1;
}
}
}
print_r($testarr);
I must offer a better way for you and future researchers...
You only need one loop, and you merely need to nominate the result array's key values before using [] to "push" new data into the deepest subarray.
*there is absolutely no need for any condition statements or a second loop.
Code: (Demo)
$initialarray = [
['unit' => 1, 'class' => 1, 'value' => 'string1'],
['unit' => 1, 'class' => 2, 'value' => 'string2'],
['unit' => 1, 'class' => 2, 'value' => 'string3'],
['unit' => 2, 'class' => 1, 'value' => 'string4'],
['unit' => 2, 'class' => 2, 'value' => 'string5']
];
foreach ($initialarray as $row) {
$result[$row['unit']][$row['class']][] = $row;
}
var_export($result);
Output:
array (
1 =>
array (
1 =>
array (
0 =>
array (
'unit' => 1,
'class' => 1,
'value' => 'string1',
),
),
2 =>
array (
0 =>
array (
'unit' => 1,
'class' => 2,
'value' => 'string2',
),
1 =>
array (
'unit' => 1,
'class' => 2,
'value' => 'string3',
),
),
),
2 =>
array (
1 =>
array (
0 =>
array (
'unit' => 2,
'class' => 1,
'value' => 'string4',
),
),
2 =>
array (
0 =>
array (
'unit' => 2,
'class' => 2,
'value' => 'string5',
),
),
),
)
If I may express myself in the following manner: I only see the front-end of your problem and know nothing about its back-end, e.g. "Where does the data come from?", "How is it collected and stored", etc. so my answer might not be a real help but still I'll give my "tuppence".
If you can store all that data in a relational database (in form of table(s)) it would be much more easier and faster(!) to select the needed data from the database instead of rearranging arrays, which will take some more time in comparison.
Just as an example you might then select (and store it into an array) all items which have unit = '1' and / or all items which have class = '2'. That would make life much more easier IMHO, than having all the data in a multidimensional array and then try to sort it / rearrange it. Especially if you do that based on more than one property.