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 11 months ago.
Improve this question
Is there a way to generate 65 random number in an array between the numbers 24 - 51 in php. Thats just an example of what I'm trying to do. I keep finding just range of two number or picking one number but not generating 65 numbers, that will repeat, between a range of numbers.
to generate 65 random number in an array between the numbers 24 - 51 you may use for loop and mt_rand function.
$arr = [];
$n = 65;
$r_min = 24;
$r_max = 51;
for ($i = 1; $i <= $n; $i++) {
$arr[] = mt_rand($r_min, $r_max);
}
print_r($arr);
will print for example: (output will change every time of course)
Array
(
[0] => 50
[1] => 37
[2] => 44
[3] => 48
[4] => 31
[5] => 30
[6] => 29
[7] => 51
[8] => 34
[9] => 32
[10] => 41
[11] => 25
[12] => 41
[13] => 26
[14] => 51
[15] => 42
[16] => 31
[17] => 44
[18] => 51
[19] => 41
[20] => 25
[21] => 46
[22] => 26
[23] => 45
[24] => 26
[25] => 26
[26] => 26
[27] => 33
[28] => 32
[29] => 48
[30] => 29
[31] => 39
[32] => 46
[33] => 40
[34] => 25
[35] => 49
[36] => 51
[37] => 45
[38] => 49
[39] => 45
[40] => 38
[41] => 30
[42] => 46
[43] => 48
[44] => 49
[45] => 24
[46] => 50
[47] => 26
[48] => 50
[49] => 35
[50] => 46
[51] => 25
[52] => 49
[53] => 42
[54] => 45
[55] => 42
[56] => 39
[57] => 37
[58] => 42
[59] => 43
[60] => 46
[61] => 33
[62] => 51
[63] => 50
[64] => 24
)
I am trying to sort and array, however, unable to get the array as intended.
I have an array like:
Array
(
[0] => 5
[1] => 7
[2] => 8
[3] => 9
[4] => 10
[5] => 11
[6] => 12
[7] => 13
[8] => 14
[9] => 15
[10] => 16
[11] => 17
[12] => 18
[13] => 19
[14] => 20
[15] => 21
[16] => 23
[17] => 24
[18] => 26
[19] => 28
[20] => 29
[21] => 30
[22] => 31
[23] => 32
[24] => 33
[25] => 34
[26] => 35
[27] => 36
[28] => 37
[29] => 38
[30] => 39
[31] => 40
[32] => 41
[33] => 42
[34] => 44
[35] => 46
[36] => 48
[37] => 50
[38] => 52
[39] => 54
[40] => A
[41] => B
[42] => 3
[43] => 4
[44] => 6
[45] => A1
[46] => B1
[47] => 1A
[48] => 22
[49] => 25
[50] => 27
[51] => 1B
)
When I run asort($arr) it returns:
Array
(
[47] => 1A
[51] => 1B
[42] => 3
[43] => 4
[0] => 5
[44] => 6
[1] => 7
[2] => 8
[3] => 9
[4] => 10
[5] => 11
[6] => 12
[7] => 13
[8] => 14
[9] => 15
[10] => 16
[11] => 17
[12] => 18
[13] => 19
[14] => 20
[15] => 21
[48] => 22
[16] => 23
[17] => 24
[49] => 25
[18] => 26
[50] => 27
[19] => 28
[20] => 29
[21] => 30
[22] => 31
[23] => 32
[24] => 33
[25] => 34
[26] => 35
[27] => 36
[28] => 37
[29] => 38
[30] => 39
[31] => 40
[32] => 41
[33] => 42
[34] => 44
[35] => 46
[36] => 48
[37] => 50
[38] => 52
[39] => 54
[40] => A
[45] => A1
[41] => B
[46] => B1
)
It is sorting normally, however, I am willing to have it sort according to combination such as Numbers, NumbersLetters, LettersNumbers, Letters.
For instance, this array should looks like:
Array
(
[42] => 3
[43] => 4
[0] => 5
[44] => 6
[1] => 7
[2] => 8
[3] => 9
[4] => 10
[5] => 11
[6] => 12
[7] => 13
[8] => 14
[9] => 15
[10] => 16
[11] => 17
[12] => 18
[13] => 19
[14] => 20
[15] => 21
[48] => 22
[16] => 23
[17] => 24
[49] => 25
[18] => 26
[50] => 27
[19] => 28
[20] => 29
[21] => 30
[22] => 31
[23] => 32
[24] => 33
[25] => 34
[26] => 35
[27] => 36
[28] => 37
[29] => 38
[30] => 39
[31] => 40
[32] => 41
[33] => 42
[34] => 44
[35] => 46
[36] => 48
[37] => 50
[38] => 52
[39] => 54
[47] => 1A
[51] => 1B
[45] => A1
[46] => B1
[40] => A
[41] => B
)
You can use user defined sort with uasort, check the Demo
uasort($array,function($a,$b){
if(is_numeric($a) && is_numeric($b)){
return $a - $b;
}elseif(is_numeric($a)){
return -1;
}elseif(is_numeric($b)){
return 1;
}else{
$length = max(strlen($a),strlen($b));
return strcmp(str_pad($a,$length,"z",STR_PAD_LEFT),str_pad($b,$length,"z",STR_PAD_LEFT));
}
});
I have a dynamic, multi-dimensional array as shown below:
Array (
[1] => Array ( [0] => 63 [1] => 60 [2] => 67 [3] => 58 [4] => 35 [5] => 47 [6] => 30 [7] => 47 [8] => 61 [9] => 63 [10] => 56 [11] => 56 [12] => 44 [13] => 38 [14] => 36 [15] => 45 [16] => 39 [17] => 55 [18] => 60 [19] => 45 [20] => 37 [21] => 45 [22] => 63 [23] => 62 [24] => 50 [25] => 47 [26] => 46 [27] => 37 [28] => 69 [29] => 35 [30] => 33 [31] => 65 [32] => 63 [33] => 50 [34] => 69 [35] => 43 [36] => 65 [37] => 64 [38] => 45 [39] => 65 [40] => 43 [41] => 30 [42] => 51 [43] => 28 [44] => 33 [45] => 53 [46] => 67 [47] => 28 [48] => 47 [49] => 42 [50] => 49 )
[2] => Array ( [0] => 30 [1] => 30 [2] => 27 [3] => 29 [4] => 29 [5] => 29 [6] => 27 [7] => 28 [8] => 30 [9] => 29 [10] => 29 [11] => 29 [12] => 29 [13] => 28 [14] => 30 [15] => 29 [16] => 29 [17] => 28 [18] => 30 [19] => 27 [20] => 28 [21] => 27 [22] => 29 [23] => 30 [24] => 30 [25] => 28 [26] => 30 [27] => 29 [28] => 30 [29] => 27 [30] => 27 [31] => 27 [32] => 29 [33] => 29 [34] => 30 [35] => 28 [36] => 29 [37] => 29 [38] => 28 [39] => 30 [40] => 28 [41] => 28 [42] => 28 [43] => 28 [44] => 30 [45] => 27 [46] => 28 [47] => 27 [48] => 30 [49] => 27 [50] => 29 ) )
I want to convert it to this output array:
Array (
[0] => 63,30
.....
[50] => 49,29
)
This code will give you the answer you want. It finds the keys of the first element of the array using array_keys then uses array_column to fetch the values for that key for each entry in the array. The result from array_column is implode'd to give the desired value for the new array:
$newarray = array();
$keys = array_keys(array_values($array)[0]);
foreach ($keys as $key) {
$newarray[$key] = implode(',', array_column($array, $key));
}
print_r($newarray);
Output:
Array (
[0] => 63,30
[1] => 60,30
[2] => 67,27
[3] => 58,29
...
[50] => 49,29
)
Demo on 3v4l.org
I have array of related products and each has some websites I want to store all of them in just one array My problem is that when the foreach loop goes to other related product it will not store the other one and also I used $web[] and it will show me the right one but in two dimensional array because I initialized it as an array and then again I am inserting it into other array. the reason why I have $web= array(); is that it is part of my code that is included to other code so I should empty my array so I used this method.the code and the outputs are below:
<?php
echo "-------related----------<br>";
echo 'Productid: '.$productid."<br>";
$_product = Mage::getModel('catalog/product')->load($productid);
$web= array();
foreach ($_product->getRelatedProducts() as $_product)
{
echo 'Related Website ids for: '.$_product->getSku().'<br>';
echo '<pre>website IDs in related:<br>';
echo "=============";
$web+=$_product->getWebsiteIds();
echo "=============";
print_r($_product->getWebsiteIds());
echo "=====inside array ====";
print_r($web);
echo "<br>webcount in related:".print_r(count($web))."<br>";
}
echo'<br>';
echo "Array OF ALL RELATED PRODUCTS:";
foreach($web as $key => $value) {
echo "<pre>";
echo $key. "=>". $value;
}
echo "<br><br>COUNT".count($web);
echo '</pre>';
echo "-------------------------";
?>
output:
-------related----------
Productid: 78110
Related Website ids for: XXXXXX
website IDs in related:
==========================Array
(
[0] => 1
[1] => 3
[2] => 4
[3] => 13
[4] => 14
[5] => 16
[6] => 17
[7] => 18
[8] => 19
[9] => 20
[10] => 21
[11] => 23
[12] => 24
[13] => 25
[14] => 26
[15] => 27
[16] => 28
[17] => 29
[18] => 30
[19] => 31
[20] => 34
[21] => 35
[22] => 36
[23] => 38
[24] => 40
[25] => 41
[26] => 46
[27] => 47
[28] => 48
[29] => 50
[30] => 51
[31] => 75
)
=====inside array ====Array
(
[0] => 1
[1] => 3
[2] => 4
[3] => 13
[4] => 14
[5] => 16
[6] => 17
[7] => 18
[8] => 19
[9] => 20
[10] => 21
[11] => 23
[12] => 24
[13] => 25
[14] => 26
[15] => 27
[16] => 28
[17] => 29
[18] => 30
[19] => 31
[20] => 34
[21] => 35
[22] => 36
[23] => 38
[24] => 40
[25] => 41
[26] => 46
[27] => 47
[28] => 48
[29] => 50
[30] => 51
[31] => 75
)
32
webcount in related:1
Related Website ids for: YYYYYY
website IDs in related:
==========================Array
(
[0] => 0
[1] => 50
[2] => 51
)
=====inside array ====Array
(
[0] => 1
[1] => 3
[2] => 4
[3] => 13
[4] => 14
[5] => 16
[6] => 17
[7] => 18
[8] => 19
[9] => 20
[10] => 21
[11] => 23
[12] => 24
[13] => 25
[14] => 26
[15] => 27
[16] => 28
[17] => 29
[18] => 30
[19] => 31
[20] => 34
[21] => 35
[22] => 36
[23] => 38
[24] => 40
[25] => 41
[26] => 46
[27] => 47
[28] => 48
[29] => 50
[30] => 51
[31] => 75
)
32
webcount in related:1
Array OF ALL RELATED PRODUCTS:
0=>1
1=>3
2=>4
3=>13
4=>14
5=>16
6=>17
7=>18
8=>19
9=>20
10=>21
11=>23
12=>24
13=>25
14=>26
15=>27
16=>28
17=>29
18=>30
19=>31
20=>34
21=>35
22=>36
23=>38
24=>40
25=>41
26=>46
27=>47
28=>48
29=>50
30=>51
31=>75
COUNT32
-------------------------
as you see for the second related product I have 3 items but again it didn't make any differences for the array
Though your question is not exactly clear to me, but I think you want all the website id list in one single one dimensional array.
In that case you can use
$web = array_merge($web, $_product->getWebsiteIds());
I am populating an array with variables from a table.
For some reason, it doesn't like the indexes 08 or 09.
When I have
$numCorrectArray = array(01=>$q01TotalCorrect, 02=>$q02TotalCorrect, 03=>$q03TotalCorrect, 04=>$q04TotalCorrect, 05=>$q05TotalCorrect, 06=>$q06TotalCorrect, 07=>$q07TotalCorrect, 08=>$q08TotalCorrect, 09=>$q09TotalCorrect, 10=>$q10TotalCorrect, 11=>$q11TotalCorrect, 12=>$q12TotalCorrect, 13=>$q13TotalCorrect, 14=>$q14TotalCorrect, 15=>$q15TotalCorrect, 16=>$q16TotalCorrect, 17=>$q17TotalCorrect, 18=>$q18TotalCorrect, 19=>$q19TotalCorrect, 20=>$q20TotalCorrect, 21=>$q21TotalCorrect, 22=>$q22TotalCorrect, 23=>$q23TotalCorrect, 24=>$q24TotalCorrect, 25=>$q25TotalCorrect, 26=>$q26TotalCorrect, 27=>$q27TotalCorrect, 28=>$q28TotalCorrect, 29=>$q29TotalCorrect, 30=>$q30TotalCorrect, 31=>$q31TotalCorrect, 32=>$q32TotalCorrect, 33=>$q33TotalCorrect, 34=>$q34TotalCorrect, 35=>$q35TotalCorrect, 36=>$q36TotalCorrect, 37=>$q37TotalCorrect, 38=>$q38TotalCorrect, 39=>$q39TotalCorrect, 40=>$q40TotalCorrect);
print_r spits out
Array ( [1] => 60 [2] => 69 [3] => 38 [4] => 69 [5] => 70 [6] => 47 [7] => 39 [0] => 70 [10] => 56 [11] => 37 [12] => 32 [13] => 24 [14] => 48 [15] => 72 [16] => 65 [17] => 26 [18] => 50 [19] => 55 [20] => 36 [21] => 40 [22] => 49 [23] => 37 [24] => 33 [25] => 66 [26] => 64 [27] => 68 [28] => 54 [29] => 59 [30] => 25 [31] => 58 [32] => 58 [33] => 58 [34] => 48 [35] => 70 [36] => 51 [37] => 67 [38] => 54 [39] => 62 [40] => 45 )
Meanwhile, when I do
$numCorrectArray = array(01=>$q01TotalCorrect, 02=>$q02TotalCorrect, 03=>$q03TotalCorrect, 04=>$q04TotalCorrect, 05=>$q05TotalCorrect, 06=>$q06TotalCorrect, 07=>$q07TotalCorrect, 88=>$q08TotalCorrect, 99=>$q09TotalCorrect, 10=>$q10TotalCorrect, 11=>$q11TotalCorrect, 12=>$q12TotalCorrect, 13=>$q13TotalCorrect, 14=>$q14TotalCorrect, 15=>$q15TotalCorrect, 16=>$q16TotalCorrect, 17=>$q17TotalCorrect, 18=>$q18TotalCorrect, 19=>$q19TotalCorrect, 20=>$q20TotalCorrect, 21=>$q21TotalCorrect, 22=>$q22TotalCorrect, 23=>$q23TotalCorrect, 24=>$q24TotalCorrect, 25=>$q25TotalCorrect, 26=>$q26TotalCorrect, 27=>$q27TotalCorrect, 28=>$q28TotalCorrect, 29=>$q29TotalCorrect, 30=>$q30TotalCorrect, 31=>$q31TotalCorrect, 32=>$q32TotalCorrect, 33=>$q33TotalCorrect, 34=>$q34TotalCorrect, 35=>$q35TotalCorrect, 36=>$q36TotalCorrect, 37=>$q37TotalCorrect, 38=>$q38TotalCorrect, 39=>$q39TotalCorrect, 40=>$q40TotalCorrect);
print_r spits out
Array ( [1] => 60 [2] => 69 [3] => 38 [4] => 69 [5] => 70 [6] => 47 [7] => 39 [88] => 66 [99] => 70 [10] => 56 [11] => 37 [12] => 32 [13] => 24 [14] => 48 [15] => 72 [16] => 65 [17] => 26 [18] => 50 [19] => 55 [20] => 36 [21] => 40 [22] => 49 [23] => 37 [24] => 33 [25] => 66 [26] => 64 [27] => 68 [28] => 54 [29] => 59 [30] => 25 [31] => 58 [32] => 58 [33] => 58 [34] => 48 [35] => 70 [36] => 51 [37] => 67 [38] => 54 [39] => 62 [40] => 45 )
Notice how it handles keys 08 and 09 vs when I change them to 88 and 99 in the second example.
Can anyone spot what I am doing wrong?
Preppending a number with a 0 in PHP forces the number to be interpreted as an octal value. There is no 8 and 9 in octal notation.
Why not just use 1, 2, 3, etc? Or if you really need it to be two digits, use strings as key, i.e. '01', '02', '03'...
0... is octal numbers, there's no 08 or 09 so PHP treats them as 0.