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.
Related
I have two different arrays. One array, a, for a list of people. My other array, b, for a list of their ages. I go to sort b by number and then reverse it so it goes in descending order. I got to this part okay.
How do I sort a (a list of people's names) so that the same values are still paired up with the sorted list?
Example:
a contains Bob, Sue, Phil, and Jenny respectively
b contains 15, 12, 13, and 13 respectively.
I want my outcome to be:
a contains Bob, Jenny, Phil, and Sue respectively
b contains 15, 13, 13, and 12 respectively
http://php.net/manual/en/function.array-multisort.php
using example #1 in the reference:
$a = array('Bob', 'Sue', 'Phil', 'Jenny');
$b = array(15, 12, 13, 13);
array_multisort($a, $b);
print_r($a);
> Array
(
[0] => Bob
[1] => Jenny
[2] => Phil
[3] => Sue
)
print_r($b);
> Array
(
[0] => 15
[1] => 13
[2] => 13
[3] => 12
)
Why not just use:
$arr = array('Bob'=>15,'Sue'=>12,'Phil'=>13,'Jenny'=>13);
Then you can sort smoothly.
Hello guys I have a small question that suppose I have a string as
"Hello My name is XYZ"
Now I know I can find the length of the words as "Hello" has 5 characters and "My" has 2 characters. By using following code
$text = file_get_contents('text.txt'); // $text = 'Hello my name is XYZ';
$words = str_word_count($text, 1);
$wordsLength = array_map(
function($word) { return mb_strlen($word, 'UTF-8'); },
$words
);
var_dump(array_combine($words, $wordsLength));
But what if i want to find that the number of words with length 1 is 0. The number of words with lengths 2 is 2. The number of words with length 3 is 1 and so on till number of length 10
Note- I am considering the word length till there is a space Suppose there is a date in the data like 20.04.2016 so it should show me that the number is words with length 10 is 1.
and one more thing how do I find the average length for the words in the string.
Thank you in advance
If you use array_count_values() on the $wordsLength array it will give a count of the string lengths there are. If you use this and a template array (created using array_fill()) with the elements 1-10 and a value of 0. You will get a list of all of the word counts...
$counts = array_replace(array_fill(1, 9, 0),
array_count_values($wordsLength));
will give...
Array
(
[1] => 0
[2] => 2
[3] => 1
[4] => 1
[5] => 1
[6] => 0
[7] => 0
[8] => 0
[9] => 0
)
Hi try this it works on the date and special chars,emojis
$text = 'Hello 20.04.2016 🚩 my face😘face is XYZ';
$words =preg_split('/\s+/', $text);
$wordsLength = array_map(
function($word) { return mb_strlen($word, 'UTF-8'); } ,$words);
print_r($words);
//Get Average word Length
$avg=round(array_sum($wordsLength)/count($words),1);
//print Avg
print($avg);
?>
(Demo)
$text = ' Hello 20.04.2016 🚩 my incredibleness face😘face is XYZ ';
Generate array of continuous visible characters
$words = preg_split('/\s+/', $text, 0, PREG_SPLIT_NO_EMPTY);
array (
0 => 'Hello',
1 => '20.04.2016',
2 => '🚩',
3 => 'my',
4 => 'incredibleness',
5 => 'face😘face',
6 => 'is',
7 => 'XYZ',
)
Replace visible strings with their multibyte length notice the simpler syntax
$wordsLength = array_map('mb_strlen', $words);
array (
0 => 5,
1 => 10,
2 => 1,
3 => 2,
4 => 14,
5 => 9,
6 => 2,
7 => 3,
)
Group and count lengths
$lengthCounts = array_count_values($wordsLength);
array (
5 => 1,
10 => 1,
1 => 1,
2 => 2,
14 => 1,
9 => 1,
3 => 1,
)
Establish an array of defaults, because $lengthCounts may have gaps
$defaultCounts = array_fill_keys(range(1,10), 0);
array (
1 => 0,
2 => 0,
3 => 0,
4 => 0,
5 => 0,
6 => 0,
7 => 0,
8 => 0,
9 => 0,
10 => 0,
)
Filter the counts to remove elements/counts that are out-of-range
$filteredCounts = array_intersect_key($lengthCounts, $defaultCounts);
array (
5 => 1,
10 => 1,
1 => 1,
2 => 2,
9 => 1,
3 => 1,
)
Overwrite the defaults with found counts to prevent gaps in the output
$gaplessCounts = array_replace($defaultCounts, $filteredCounts);
array (
1 => 1,
2 => 2,
3 => 1,
4 => 0,
5 => 1,
6 => 0,
7 => 0,
8 => 0,
9 => 1,
10 => 1,
)
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)
?>
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];
}
I have two different arrays. One array, a, for a list of people. My other array, b, for a list of their ages. I go to sort b by number and then reverse it so it goes in descending order. I got to this part okay.
How do I sort a (a list of people's names) so that the same values are still paired up with the sorted list?
Example:
a contains Bob, Sue, Phil, and Jenny respectively
b contains 15, 12, 13, and 13 respectively.
I want my outcome to be:
a contains Bob, Jenny, Phil, and Sue respectively
b contains 15, 13, 13, and 12 respectively
http://php.net/manual/en/function.array-multisort.php
using example #1 in the reference:
$a = array('Bob', 'Sue', 'Phil', 'Jenny');
$b = array(15, 12, 13, 13);
array_multisort($a, $b);
print_r($a);
> Array
(
[0] => Bob
[1] => Jenny
[2] => Phil
[3] => Sue
)
print_r($b);
> Array
(
[0] => 15
[1] => 13
[2] => 13
[3] => 12
)
Why not just use:
$arr = array('Bob'=>15,'Sue'=>12,'Phil'=>13,'Jenny'=>13);
Then you can sort smoothly.