I have faced with the problem, I need to normalize/sort in natural order values in array after some item has been removed.
Consider following example. Initial array
{ [313]=> int(2) [303]=> int(1) [295]=> int(3) [290]=> int(4) }
Sorted array
{ [303]=> int(1) [313]=> int(2) [295]=> int(3) [290]=> int(4) }
Consider case when we are removing first item, array should look like this now
{ [313]=> int(1) [295]=> int(2) [290]=> int(3) }
In case of item inside the array range for example 295 (3) it should be
{ [303]=> int(1) [313]=> int(2) [290]=> int(3) }
I hope you get an idea.
But my function doesn't do this correctly.
I've implemented part of this sorting, here is the code, but maybe there are other ways to do this easier ?
const MIN_VALUE = 1;
public function sort_items(&$items_map)
{
if (!empty($items_map)) {
asort($items_map);
var_dump($items_map);
$first_item = reset($items_map);
if ($first_item > self::MIN_VALUE) {
$normalize_delta = $first_item - self::MIN_VALUE;
$prev_item_id = null;
foreach ($items_map as $id => $part) {
$items_map[$id] = $part - $normalize_delta;
if (!empty($prev_item_id)) {
$difference = $items_map[$id] - $items_map[$prev_item_id];
if ($difference > 1) {
$items_map[$id] = $items_map[$id] - ($difference - 1);
}
}
$prev_item_id = $id;
}
}
}
return $items_map;
}
I would be grateful for any help.
Thanks
UPDATE
To clarify.
I want items not to be just sorted in the correct order, but to be in natural order, for example
Sequence 1,3,5,6,7,9 should be transformed into 1,2,3,4,5,6 but keeping keys the same.
2,3,7,9 => 1,2,3,4
Please see my example above with real word case.
If you need to use a custom sort algorithm, use usort to do so. From PhP the documentation :
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
So you just need to provide those integers if you are in case an item is "greater" or "lower", and usort will do the job for you.
In your case, it could lead to this function :
<?php
function sort_items_map($a, $b)
{
$value = 0;
if( $a < $b )
{
$value = -1;
}
else if( $a > $b )
{
$value = 1;
}
else if( $a == $b )
{
$value = 0;
}
return $value;
}
$items_map = [1, 3, 1, 7]; // or fill it with your own values
usort($items_map, "sort_items_map");
?>
Related
I have created the following script for creating an array_uintersect alogrithm.
function compare2D($topic, $nomination): int
{
if (is_array($nomination)) {
return in_array($topic, $nomination) ? 0 : 1;
}
else if (is_array($topic)) {
return in_array($nomination, $topic) ? 0 : 1;
}
return strcmp($topic, $nomination);
}
$arr = [['REMOTE', 'REMOTE_PREMIUM'], 'SECURE'];
$topic = ['SECURE', 'REMOTE'];
var_dump(array_uintersect($topic, $arr, 'compare2D'));
When I run it, the result is
array(1) {
[0] =>
string(6) "SECURE"
}
Where it should be returning
array(2) {
[0] =>
string(6) "SECURE"
[1] =>
string(6) "REMOTE"
}
I found that this is only dependent on the array in 2nd argument to _uintersect having a sub-array at index 0 (first item).
When I move the sub-array to any position except for the 1st, i.e:
$arr = ['SECURE', ['REMOTE', 'REMOTE_PREMIUM']];
// or
$arr = ['SOMEVALUE', ['REMOTE', 'REMOTE_PREMIUM'], 'SECURE'];
...the intersection works fine, and I get the intended result above.
Does anyone know some algorithmic rules of php intersection that I am not aware of?
I think you can switch the logic of returning 1 and 0, so return 1 when the value is in the array as you want to find the intersection.
For the last part, using strcmp will return 0 if the strings are equal. In that case you can check if they are equal, and if they are, then return 1.
function compare2D($topic, $nomination): int
{
if (is_array($nomination)) {
return in_array($topic, $nomination) ? 1 : 0;
}
if (is_array($topic)) {
return in_array($nomination, $topic) ? 1 : 0;
}
return strcmp($topic, $nomination) === 0 ? 1 : 0;
}
$topic = ['SECURE', 'REMOTE'];
$arr = [['REMOTE', 'REMOTE_PREMIUM'], 'SECURE'];
$res = array_uintersect($topic, $arr, 'compare2D');
var_dump($res);
Output
array(2) {
[0]=>
string(6) "SECURE"
[1]=>
string(6) "REMOTE"
}
Php demo | Php demo with more nested arrays
Its my first time working with multidimensional arrays in php. I need to change the second number in each sub array.
What I want is to check if the Id in the array matches the Id from the database. When the two match I want to change the 2nd entry in the sub array by adding a number to it. If the Id from the query does not match anything in the list I want a new sub array to be pushed to the end of the array with the values of Id and points_description.
Also, if its helpful, my program right now does find the matches. The only thing is, it does not update the 2D array.
$array = array(array());
while ($row_description = mysqli_fetch_array($query_description)) {
$check = 1;
$is_match = 0;
foreach ($array as $i) {
foreach ($i as $value) {
if ($check == 1) {
if ($row_description['Id'] == $value) {
//$array[$i] += $points_description;
$is_match = 1;
}
}
$check++;
$check %= 2; //toggle between check and points
}
}
if ($is_match == 0) {
array_push($array, array($row_description['Id'], $points_description));
}
}
I feel like Im doing this so wrong. I just want to go through my 2D array and change every second value. The expected output should be a print out of all the Ids and their corresponding point value
I hope this is helpful enough.
Example: $row_description['Id'] = 2 and $array = array(array(2,1), array(5,1) , array(6,1))
output should be $array = array(array(2,4), array(5,1) , array(6,1))
if $row_description['Id'] = 3 and $array = array(array(2,1), array(5,1) , array(6,1))
output should be $array = array(array(2,4), array(5,1) , array(6,1),array(3,3))
By default PHP will copy an array when you use it in a foreach.
To prevent PHP from creating this copy you need to use to reference the value with &
Simple example :
<?php
$arrFoo = [1, 2, 3, 4, 5,];
$arrBar = [3, 6, 9,];
Default PHP behavior : Make a copy
foreach($arrFoo as $value_foo) {
foreach($arrBar as $value_bar) {
$value_foo *= $value_bar;
}
}
var_dump($arrFoo);
/* Output :
array(5) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(4)
[4]=>
int(5)
}
*/
ByReference : Don't create the copy :
foreach($arrFoo as &$value_foo) {
foreach($arrBar as $value_bar) {
$value_foo *= $value_bar;
}
}
var_dump($arrFoo);
/* Output :
array(5) {
[0]=>
int(162)
[1]=>
int(324)
[2]=>
int(486)
[3]=>
int(648)
[4]=>
&int(810)
}
*/
firstly, I very much apologize for my poorly written title of this question. So please someone with native English, change the title appropriately. My question is rather simple, it follows:
I am using integer to store multiple types of one item. For example:
TYPE A = 1
TYPE B = 2
TYPE C = 4
TYPE D = 8
TYPE E = 16
etc...
Now the item in DB has type value 14, that means that it has been assigned to TYPE B+C+D. If it would have type value for example 9, it would mean it has been assigned to TYPE A+D.
I need a function which I would supply with single type integer and this function would return array of integer types.
I could iterate through all the integers and compare them with the number, but that's what I am using now, but I am looking for some more effective way, if it exists ?
Thanks in advance for your help.
Here's a function without any loops (mostly done for the fun of it :) ):
function getTypes($int)
{
$types = array('Type A','Type B','Type C','Type D','Type E');//defining types
$t = array_reverse(str_split(decbin($int)));//converting $int to an array of bits
$types = array_slice($types,0,ceil(log($int,2)));//slicing the array to the same size as number of bits in the $int
$t = array_combine($types,$t);// Creating a new array where types are keys and bits are values
return array_keys($t,1);// returning an array of keys which have a value of 1
}
However it doesn't mean that it is efficient. If you use a bitmask you better of checking values using bitwise operators like bitwise and (&).
For example if you want to check if your integer contains Type D and Type E you should do
if ($integer & 8 & 16)
To check for each individual type I would us a loop with bitshift operator
function getTypes($int)
{
$result = array();
$types = array('Type A','Type B','Type C','Type D','Type E');
foreach($types as $type)
{
if ($int & 1)//checking if last bit is 1 (exactly the same as if($int%2) )
$result[]=$type;
$int>>=1;//shifting integer's bits to the right (exactly the same as $int = $int / 2)
}
return $result;
}
How's this? http://codepad.org/AzgdPsL1
To explain what's going on:
I create a $types array of all the valid types that exist between the range of 1 to $max_bit bits.
Looping while the number is greater than 0, it gets bitwise ANDed with 1. If it turns out that evaluates to true, this means that the LSB is set, so the type at the head of the $type array applies to this number. The current type is added to the return array.
The number is then shifted to the right by one bit.
<?php
function num2type( $num)
{
$max_bit = 5;
$types = array_combine( range( 1, $max_bit), range( ord( 'A'), ord( 'A') + $max_bit - 1));
$return = array();
while( $num > 0)
{
$current_type = array_shift( $types);
if( $num & 0x1)
{
$return[] = chr( $current_type);
}
$num = $num >> 1;
}
return $return;
}
var_dump( num2type( 8)); // array(1) { [0]=> string(1) "D" }
var_dump( num2type( 31));
var_dump( num2type( 14));
Output (for 31):
array(5) {
[0]=>
string(1) "A"
[1]=>
string(1) "B"
[2]=>
string(1) "C"
[3]=>
string(1) "D"
[4]=>
string(1) "E"
}
Output for 14:
array(3) {
[0]=>
string(1) "B"
[1]=>
string(1) "C"
[2]=>
string(1) "D"
}
function check_flag($field, $bit)
{
return (($field | $bit) === $field) ? TRUE : FALSE;
}
$types = array('A' => 1, 'B' => 2, 'C' => 4, 'D' => 8, 'E' => 16);
$db_value = 14;
var_dump(check_flag($db_value, $types['B']));
... just make sure that you cast the value fetched from the database to integer.
Edit: Now that I read you need all of the types that are set, here's some more logic:
$set = array();
foreach ($types as $key => $value)
if (check_flag($db_value, $value)) $set[] = $key;
$a = 10;
$scan = 1;
$result = array();
while ($a >= $scan){
if ($a & $scan)
$result[] = $scan;
$scan<<=1; //just a bit shift
}
var_dump($result);
I need some help/direction in setting up a PHP script to randomly pair up items in an array.
The items should be randomly paired up each time.
The items should not match themselves ( item1-1 should not pair up with item1-1 )
Most of the items have a mate (ie. item1-1 and item1-2). The items should not be paired with their mate.
I've been playing around with the second script in this post but, I haven't been able to make any progress. Any help is appreciated.
Very simple approach, but hopefully helpful to you:
(mates, if grouped in an array (e.g. array('a1', 'a2')), will not be paired.)
function matchUp($array) {
$result = array();
while($el = array_pop($array)) {
shuffle($array);
if (sizeof($array) > 0) {
$candidate = array_pop($array);
$result[] = array(
array_pop($el),
array_pop($candidate)
);
if (sizeof($el) > 0) {
$array[] = $el;
}
if (sizeof($candidate) > 0) {
$array[] = $candidate;
}
}
else {
$result[] = array(array_pop($el));
}
}
return $result;
}
$array = array(
array('a1', 'a2'),
array('b1', 'b2'),
array('c1'),
array('d1'),
array('e1', 'e2'),
array('f1'),
array('g1', 'g2'),
);
Update:
foreach(matchUp($array) as $pair) {
list($a, $b) = $pair + array(null, null);
echo '<div style="border: solid 1px #000000;">' . $a . ' + ' . $b . '</div>';
}
With the randomness, there is no guarantee that a full correct solution will be reached.
Certain problem sets are more likely to be solved than others. Some will be impossible.
You can configure how many times it will try to achieve a good solution. After the specified number of tries it will return the best solution it could find.
function pairUp (array $subjectArray) {
// Config options
$tries = 50;
// Variables
$bestPaired = array();
$bestUnpaired = array();
for($try = 1; $try <= 50; $try++) {
$paired = array();
$unpaired = array();
$toBePaired = $subjectArray;
foreach($subjectArray as $subjectIndex => $subjectValue) {
// Create array without $thisValue anywhere, from the unpaired items
$cleanArray = array();
foreach($toBePaired as $index => $value) {
if($value != $subjectValue) {
array_push($cleanArray, array(
'index' => $index,
'value' => $value
));
}
}
sort($cleanArray); // reset indexes in array
// See if we have any different values left to match
if(count($cleanArray) == 0) {
array_push($unpaired, $subjectValue);
continue;
}
// Get a random item from the clean array
$randomIndex = rand(0,count($cleanArray)-1);
// Store this pair
$paired[$subjectIndex] = $subjectValue . '-' . $cleanArray[$randomIndex]['value'];
// This item has been paired, remove it from unpairedItems
unset($toBePaired[$cleanArray[$randomIndex]['index']]);
sort($toBePaired);
}
// Decide if this is our best try
if(count($paired) > count($bestPaired)) {
$bestPaired = $paired;
$bestUnpaired = $unpaired;
}
// If we had no failures, this was a perfect try - finish
if(count($unpaired) == 0) { $break; }
}
// We're done, send our array of pairs back.
return array(
'paired' => $bestPaired,
'unpaired' => $bestUnpaired
);
}
var_dump(pairUp(array('a','b','c','d','e','a','b','c','d','e')));
/*
Example output:
array(2) {
["paired"]=>
array(10) {
[0]=>
string(3) "a-b"
[1]=>
string(3) "b-c"
[2]=>
string(3) "c-d"
[3]=>
string(3) "d-e"
[4]=>
string(3) "e-a"
[5]=>
string(3) "a-b"
[6]=>
string(3) "b-e"
[7]=>
string(3) "c-d"
[8]=>
string(3) "d-c"
[9]=>
string(3) "e-a"
}
["unpaired"]=>
array(0) {
}
}
*/
Case 1: if all elements had a mate
If all elements had a mate, the following solution would work, although I don't know if it would be perfectly random (as in, all possible outputs having the same probability):
Shuffle the list of elements, keeping mates together
original list = (a1,a2),(b1,b2),(c1,c2),(d1,d2)
shuffled = (c1,c2),(d1,d2),(a1,a2),(b1,b2)
Shift the second mate to the right. The matches have been formed.
shifted = (c1,b2),(d1,c2),(a1,d2),(b1,a2)
(Edit1: if applied exactly as described, there is no way a1 ends up matched with b1. So, before shifting, you may want to throw a coin for each pair of mates to decide whether they should change their order or not.)
Case 2: if only some elements have a mate
Since in your question only some elements will have a mate, I guess one could come up with the following:
Arbitrarily pair up those elements who don't have a mate. There should be an even number of such elements. Otherwise, the total number of elements would be odd, so no matching could be done in the first place.
original list = (a1,a2),(b1,b2),c1,d1,e1,f1 // c1,d1,e1 and f1 don't have mates
list2 = (a1,a2),(b1,b2),(c1,d1),(e1,f1) // pair them up
Shuffle and shift as in case 1 to form the matches.
shuffled = (e1,f1),(a1,a2),(c1,d1),(b1,b2)
shifted = (e1,b2),(a1,f1),(c1,a2),(b1,d1)
Again, I don't know if this is perfectly random, but I think it should work.
(Edit2: simplified the solution)
(Edit3: if the total number of elements is odd, someone will be left without a match, so pick an element randomly at the beginning to leave it out and then apply the algorithm above).
I need an advice.
I need to scrap and parse text file (using for currency exchange rates). Here we are, a small snippet from this file:
c057z110323
h057z110323
a057z110323
b012z110323
c058z110324
h058z110324
a058z110324
c059z110325
h059z110325
a059z110325
c060z110328
h060z110328
a060z110328
c061z110329
h061z110329
a061z110329
c062z110330
h062z110330
a062z110330
b013z110330
c063z110331
h063z110331
a063z110331
c064z110401
h064z110401
a064z110401
c065z110404
h065z110404
a065z110404
c066z110405
h066z110405
a066z110405
c067z110406
h067z110406
a067z110406
b014z110406
c068z110407
h068z110407
a068z110407
c069z110408
h069z110408
a069z110408
As you may see there's a lot of lines (in original file there are about 80000 of lines (few lines per day are being added).
String format is as following:
A000112233
where
A - type
000 - number of the file (created this year)
11 - year
22 - month
33 - day
I'm getting 25 latest lines from from the file using following snippet:
$file = "http://www.nbp.pl/kursy/xml/dir.txt";
$data = file($file);
$count = count($data);
for($i = $count - 25; $i < $count; $i++)
{
if( substr($data[$i], 0, 1) === 'a' )
{
$latest[] = $data[$i];
}
}
I need to get only lines starting with "a". The output array looks as following:
array(8) {
[0]=>
string(13) "a062z110330
"
[1]=>
string(13) "a063z110331
"
[2]=>
string(13) "a064z110401
"
[3]=>
string(13) "a065z110404
"
[4]=>
string(13) "a066z110405
"
[5]=>
string(13) "a067z110406
"
[6]=>
string(13) "a068z110407
"
[7]=>
string(13) "a069z110408
"
}
Now I need to compare every array element to get the latest item from the latest working day before current date. I'm acheiving it this way:
$i = 1;
foreach($latest as $row)
{
$plural = ($i > 1) ? 's' : null;
if( substr(trim($row), -6) === date("ymd", strtotime("-" . $i . " day" . $plural) )
{
$filename = $row;
break;
}
$i++;
}
It's working quite OK, however I'm facing one big problem. I'm unable to sort $latest array by the latest six characters. I tried doing this using sort(), rsort(). None of them worked good for me.
Can anybody help me with this issue or maybe has better approach to do what I'm looking for.
When you do
for($i = $count - 25; $i < $count; $i++)
{
if( substr($data[$i], 0, 1) === 'a' )
{
$latest[] = $data[$i];
}
}
use date as a key in $latest array:
for($i = $count - 25; $i < $count; $i++)
{
if( substr($data[$i], 0, 1) === 'a' )
{
$key = (int) substr($data[$i], -6);
$latest[$key] = $data[$i];
}
}
Then you can sort by key like:
ksort($latest);
You need to use a custom sort method. You can use usort to write your own comparison function : http://php.net/manual/en/function.usort.php
From the manual
function cmp($a, $b) {
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
$a = array(3, 2, 5, 6, 1);
usort($a, "cmp");
The comparison function must return an
integer less than, equal to, or
greater than zero if the first
argument is considered to be
respectively less than, equal to, or
greater than the second.
Since you're only asking how to sort an array of strings by their last six characters:
Use usort:
function sortfunc($a, $b) {
return strcmp(substr($a, -6), substr($b, -6));
}
usort($latest, 'sortfunc');
You may need to trim() your lines first or the newlines and/or carriage return characters will be part of the last 6 characters.