PHP - Set first array's values to second array's iterations - php

I'm trying to make one array set to the iterations of another array. I'm working on a hash algorithm that takes in a user value of the order they want the array. It takes their code and breaks it down into 40 blocks of binary to be converted into hexadecimal. So far I'm able to change the iteration order, but it only takes the last value of the first array and sets as the value for each iteration of the second array.
The first array looks like this (Showing only 10 of the 40 to save space):
Array
(
[0] => 0111
[1] => 1000
[2] => 0110
[3] => 0010
[4] => 0011
[5] => 0001
[6] => 0011
[7] => 0010
[8] => 0011
[9] => 0101
)
The second one is like this:
Array
(
[3] => 0101
[2] => 0101
[1] => 0101
[6] => 0101
[5] => 0101
[4] => 0101
[9] => 0101
[8] => 0101
[7] => 0101
[0] => 0101
)
And here is the PHP code:
$arrayDump = $test->binarySplit($name);
$ordered = array();
$orderKey = array(3, 2, 1, 6, 5, 4, 9, 8, 7, 12, 11, 10, 15, 14, 13, 18, 17, 16, 21, 20, 19, 24, 23, 22, 27, 26, 25, 30, 29, 28, 33, 32, 31, 36, 35, 34, 39, 38, 37, 0);
foreach ($orderKey as $key) {
for ($i = $key; $i < count($arrayDump); $i++) {
$ordered[$key] = $arrayDump[$i];
}
}
The class call above isn't too important for this problem that I can tell. The $arrayDump is the first array; $ordered is the second. As you can tell, the second array changes the iteration to be what I want, but it only contains the last value from the first array. I threw it through a loop to try and get each value, but I'm at a loss. Any help would be appreciated.

You don't need the second loop, try this:
foreach ($orderKey as $key => $value) {
$ordered[$key] = $arrayDump[$value];
}

Related

Merge 2 arrays in random positions

How would someone merge 2 arrays where $_array_1 is scattered but $_array_2 maintains its original order globally?
I have 2 arrays
$_array_1 = array( a, b, c );
$_array_2 = array( 1, 2, 3, 4, 5, 6 );
$_merged_array = array( c, 1, a, 2, 3, 4, 5, b, 6 );
I know this must be possible but don't have a clue where to start. What is the most efficient method and cleanest?
My interpretation of your post is: Insert every value of array 1 on a random position in array 2. This can be achieved with the following code:
$_array_1 = array( 290, 188, 519 );
$_array_2 = array( 213, 702, 231, 173, 632, 711 );
foreach($_array_1 as $val) {
array_splice($_array_2, rand(0, count($_array_2)), 0, $val);
}
print_r($_array_2); // For example: Array ( [0] => 519 [1] => 213 [2] => 290 [3] => 702 [4] => 231 [5] => 173 [6] => 632 [7] => 188 [8] => 711 )
I don't see how the end result makes sense, but here's what you can do:
Copy the second array in the merged array,
Insert the values in the first array at random positions in the merged array using $insert_position = mt_rand(0, count($merged_array)).
Note that it's not an error that I didn't do count($merged_array) - 1, because we also want the numbers to be randomly added at the end of the array.

Get parameters from hex string with defined lengths in php

How to get the 7 variables from this string "c0dbdc000081aa02000000000001c0", defining lengths for each one, some times it comes with voids in Zeros.
Definition to get the data from string:
0xc0->[SLIP Start char:C0]
0xdb,0xdc[C0]->Flag|Version:c,0
0x00->Reserved:0
0x00,0x81->Packet length:129
0xaa,0x02->Packet command:AA02[hex]
0x00,0x00->CRC check:0[hex]
0x00,0x00,0x00,0x01->Serial number:1
0xc0->[SLIP End char:C0]
Example: this are 3 strings with the same data, but 2 of them are shorter in the "Packet length" value, they dont have the 2 leading zeros. (added some spacing to show the missing zeros)
"c0000000 9aaa02000000000029c0"
"c0000000 85aa0200000000000ac0"
"c0dbdc000081aa02000000000001c0"
The code i have works with the last one, but the first ones will get messed up because of missing Zeros. Any ideas on how to manage this?
$inputSample = "C0DBDC000081AA02000000000001C0";
$header = array(
"start" => 2,
"flagVersion" => 4,
"reserved" => 2,
"packetLenght" => 4,
"packetCommand" => 4,
"CRCcheck" => 4,
"serialNumber" => 8,
"end" => 2
);
print_r(ParseIrregularString($inputSample, $header));
function ParseIrregularString($string, $lengths) {
$parts = array();
foreach ($lengths as $StringKey => $position) {
$parts[$StringKey] = substr($string, 0, $position);
$string = substr($string, $position);
}
return $parts;
}
Good Result "C0DBDC000081AA02000000000001C0"
Array
(
[start] => C0
[flagVersion] => DBDC
[reserved] => 00
[packetLenght] => 0081
[packetCommand] => AA02
[CRCcheck] => 0000
[serialNumber] => 00000001,
[end] => C0
)
Bad Result "c00000009aaa02000000000029c0"
Array
(
[start] => c0
[flagVersion] => 0000
[reserved] => 00
[packetLenght] => 9aaa
[packetCommand] => 0200
[CRCcheck] => 0000
[serialNumber] => 000029c0
[end] =>
)

PHP: Most frequent value in array

So I have this JSON Array:
[0] => 238
[1] => 7
[2] => 86
[3] => 79
[4] => 55
[5] => 92
[6] => 55
[7] => 7
[8] => 254
[9] => 9
[10] => 75
[11] => 238
[12] => 89
[13] => 238
I will be having more values in the actual JSON file. But by looking at this I can see that 238 and 55 is being repeated more than any other number. What I want to do is get the top 5 most repeated values in the array and store them in a new PHP array.
$values = array_count_values($array);
arsort($values);
$popular = array_slice(array_keys($values), 0, 5, true);
array_count_values() gets the count of the number of times each item appears in an array
arsort() sorts the array by number of occurrences in reverse order
array_keys() gets the actual value which is the array key in the results from array_count_values()
array_slice() gives us the first five elements of the results
Demo
$array = [1,2,3,4,238, 7, 86, 79, 55, 92, 55, 7, 254, 9, 75, 238, 89, 238];
$values = array_count_values($array);
arsort($values);
$popular = array_slice(array_keys($values), 0, 5, true);
array (
0 => 238,
1 => 55,
2 => 7,
3 => 4,
4 => 3,
)
The key is to use something like array_count_values() to tally up the number of occurrences of each value.
<?php
$array = [238, 7, 86, 79, 55, 92, 55, 7, 254, 9, 75, 238, 89, 238];
// Get array of (value => count) pairs, sorted by descending count
$counts = array_count_values($array);
arsort($counts);
// array(238 => 3, 55 => 2, 7 => 2, 75 => 1, 89 => 1, 9 => 1, ...)
// An array with the first (top) 5 counts
$top_with_count = array_slice($counts, 0, 5, true);
// array(238 => 3, 55 => 2, 7 => 2, 75 => 1, 89 => 1)
// An array with just the values
$top = array_keys($top_with_count);
// array(238, 55, 7, 75, 89)
?>

Convert decimal to hex using PHP

How to convert decimal value to hex using php?
Dec : 68-55-230-06-04-89
I want hex value like this
Hex : 44-37-E0-06-04-59 but instead of this its display 44-37-E0-6-4-59
echo $test = dechex(68)."-".dechex(55)."-".dechex(230)."-".dechex(06)."-".dechex(04)."-".dechex(89);
It's give me output : 44-37-E0-6-4-59 // without 06-04
I want output something like 44-37-E0-06-04-59
You can try sprintf
sprintf('%02X-%02X-%02X-%02X-%02X-%02X', 68, 55, 230, 6, 4, 89);
For getting 44-37-E0-06-04-59 just add 0 as below,
sprintf('%02X-%02X-%02X-%02X-%02X-%02X', 68, 55, 230, 06, 04, 89);
$dec= "68-55-230-06-04-89";
$arr= split("-",$dec);
foreach($arr as $key => $num)
{
$arr[$key]=sprintf('%02X',dechex ($num));
}
//required results Array ( [0] => 44 [1] => 37 [2] => e6 [3] => 6 [04] => 4 [05] => 59 )
print_r($arr);
something like that should work if you assume that there are maximum of FF between the dashes:
$foo = explode("-", $dec);
foreach ($foo as $dec_value) {
echo ($dec_value > 16) ? dechex($dec_value) : "0".dechex($dec_value);
}

php importance of a number in a set of numbers

I have a set of numbers e.g.
$input = array(1, 4, 7, 4, 9, 4, 8, 6, 2, 8, 7, 7, 4, 5, 3);
I am trying to work out the importance of each number based on the following rule:
As the sequence gets longer the numbers get less significant, and each time a number is mentioned then it will improve the relevance (how much depends on its position in the
sequence).
I am expecting something like:
Array(
'4' => 90%
'1' => 75%
'7' => 60%
....
)
So 4 is the most inportant, followed by 1 and then 7 etc. Note that the output is completely fabricated but gives in indication that 4 should be the most important. I believe I want some kind of linear solution.
Is this more of what you were thinking? Answer based on stillstanding
$numbers = array(1, 4, 7, 4, 9, 4, 8, 6, 2, 8, 7, 7, 4, 5, 3);
$weight = array();
$count = count($numbers);
for ($i=0; $i<$count; $i++) {
if (!isset($weight[$numbers[$i]])) $weight[$numbers[$i]] = 1;
$weight[$numbers[$i]] += $count + pow($count - $i, 2);
}
$max = array_sum($weight);
foreach ($weight as &$w) {
$w = ($w / $max) * 100;
}
arsort($weight);
result:
Array
(
[4] => 34.5997286296
[7] => 17.3677069199
[1] => 16.3500678426
[8] => 10.0407055631
[9] => 9.29443690638
[6] => 5.42740841248
[2] => 4.40976933514
[5] => 1.35685210312
[3] => 1.15332428765
)
$numbers=array(1, 4, 7, 4, 9, 4, 8, 6, 2, 8, 7, 7, 4, 5, 3);
$weight=array();
$count=count($numbers);
for ($i=0; $i<$count; $i++) {
if (!isset($weight[$numbers[$i]]))
$weight[$numbers[$i]]=1;
$weight[$numbers[$i]]*=$count-$i;
}
var_dump($weight);
Result:
Array
(
[1] => 15
[4] => 5040
[7] => 260
[9] => 11
[8] => 54
[6] => 8
[2] => 7
[5] => 2
[3] => 1
)
This algorithm is fairly simplistic, but I think it accomplishes what you're looking for.
Given that you have the sequence you described above and it is stored in an array called $sequence
$a = array();
for($i=0;$i<count($sequence);$i++)
{
//calculate the relevance = 1/position in array
$relevance = 1/($i+1);
//add $relevance to the value of $a[$sequence[$i]]
if(array_key_exists((string)$sequence[$i],$a))
$a[(string)$sequence[$i]] += $relevance;
else
$a[(string)$sequence[$i]] = $relevance;
}
return $a;

Categories