I have a total number 47 which start from 0
I need to generate a range for eg: if I need to generate 4 number range I would like to get result 0-4 and for another 5-9
What I want is to generate similar but want exclude already using range eg: 0-4 or 5-9 and in range of 47.
Need to generate 6 count range from 0-47 range.
Available Numbers as below in array
Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 [11] => 11 [12] => 12 [13] => 13 [14] => 14 [15] => 15 [16] => 16 [17] => 17 [18] => 18 [19] => 19 [20] => 20 [21] => 21 [22] => 22 [23] => 23 [24] => 24 [25] => 25 [26] => 26 [27] => 27 [28] => 28 [29] => 29 [30] => 30 [31] => 31 [32] => 32 [33] => 33 [34] => 34 [35] => 35 [36] => 36 [37] => 37 [38] => 38 [39] => 39 [40] => 40 [41] => 41 [42] => 42 [43] => 43 [44] => 44 [45] => 45 [46] => 46 [47] => 47
print_r($cores);
$used ='';
$dediq=mysqli_query($db,"SELECT * FROM `tblcomputs` WHERE `serverid` = '".$serverid."' AND `status` = 'Active'");
while ($dedi = mysqli_fetch_array($dediq)) {
if ($dedi['cp'] !='no'){
$used .= $dedi['cp'];
}
}
$required_rangecount ='6';
//here i'm consufed how to get this done.
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 an array ($items): http://vilavaleaprahovei.ro/kimea/allMarks.php
How can I search if 3 values: 'marca', 'diametru' and 'latime' are found (not just one or just two of them, should be founded all three) and to return it?
I was triyng like that:
foreach($items as $key => $value) {
if(($key['marca'] == "ALCAR STAHLRAD") && ($value['diametru'] == 15) && ($value['latime']== 7)) {
echo $key['marca'];
echo $value['diametru'];
echo $value['latime'];
} else {
echo "Not found any product";
}
}
The array: $items
Array
(
[ALCAR STAHLRAD] => Array
(
[diametru] => Array
(
[0] => 15
[6] => 16
[7] => 14
[15] => 13
[17] => 17
[270] => 20
)
[latime] => Array
(
[0] => 6
[7] => 5
[8] => 4
[25] => 7
[303] => 3
)
[pcd] => Array
(
[0] => 4
[1] => 5
[27] => 6
[175] => 3
)
[pcd1] => Array
(
[0] => 100
[1] => 112
[6] => 114
[11] => 108
[13] => 120
[27] => 205
[29] => 118
[44] => 98
[54] => 130
[58] => 105
[69] => 115
[77] => 180
[94] => 139
[111] => 200
[127] => 160
[141] => 127
[170] => 110
[298] => 170
)
[et] => Array
(
[0] => 40
[1] => 47
[2] => 50
[3] => 38
[5] => 43
[8] => 39
[13] => 51
[15] => 45
[16] => 52
[21] => 42
[22] => 48
[27] => 117
[28] => 60
[31] => 31
[32] => 108
[33] => 41
[35] => 27
[38] => 46
[40] => 37
[45] => 35
[48] => 36
[51] => 34
[54] => 66
[66] => 44
[68] => 20
[71] => 68
[77] => 0
[92] => 26
[97] => 49
[98] => 62
[99] => 5
[101] => 24
[111] => 110
[118] => 32
[120] => 29
[126] => 30
[135] => 55
[157] => 56
[169] => 23
[175] => 22
[178] => 107
[190] => 33
[200] => 83
[223] => 53
[232] => 54
[264] => 28
[272] => 18
[274] => -1
[324] => 121
[327] => 25
[409] => 105
[458] => 75
[465] => 132
)
[cb] => Array
(
[0] => 60
[1] => 57
[6] => 66
[7] => 54
[11] => 63
[13] => 65
[15] => 69
[18] => 64
[20] => 67
[27] => 161
[29] => 71
[34] => 56
[44] => 58
[54] => 89
[59] => 72
[69] => 70
[71] => 78
[77] => 138
[94] => 92
[98] => 84
[99] => 108
[111] => 142
[130] => 106
[179] => 73
[215] => 74
[298] => 130
[362] => 100
[366] => 95
[379] => 59
[509] => 93
)
)
[ENZO] => Array
(
[diametru] => Array
(
[0] => 16
[2] => 17
[3] => 15
[10] => 14
[13] => 18
)
[latime] => Array
(
[0] => 6
[1] => 7
[10] => 5
[34] => 8
)
[pcd] => Array
(
[0] => 5
[5] => 4
)
[pcd1] => Array
(
[0] => 114
[5] => 108
[7] => 112
[9] => 110
[10] => 100
[12] => 115
[21] => 98
[29] => 105
)
[et] => Array
(
[0] => 48
[1] => 40
[5] => 46
[7] => 45
[9] => 41
[10] => 35
[13] => 50
[14] => 16
[17] => 15
[18] => 32
[21] => 28
[22] => 38
[23] => 44
[33] => 42
[40] => 24
[47] => 37
[54] => 25
[60] => 47
)
[cb] => Array
(
[0] => 72
[1] => 71
[5] => 70
[9] => 65
[10] => 60
[11] => 57
[16] => 63
[21] => 58
[29] => 56
[59] => 66
)
)
[DEZENT] => Array
(
[diametru] => Array
(
[0] => 17
[1] => 16
[6] => 15
[24] => 18
[48] => 14
[77] => 19
[132] => 20
)
[latime] => Array
(
[0] => 7
[6] => 6
[21] => 8
[65] => 5
[101] => 4
[132] => 9
)
[pcd] => Array
(
[0] => 5
[8] => 4
[115] => 6
)
[pcd1] => Array
(
[0] => 114
[3] => 112
[4] => 108
[8] => 100
[12] => 110
[21] => 120
[24] => 130
[25] => 127
[81] => 105
[93] => 115
[103] => 98
[115] => 139
[131] => 160
[152] => 118
)
[et] => Array
(
[0] => 48
[2] => 38
[3] => 35
[5] => 53
[6] => 47
[9] => 40
[11] => 50
[15] => 45
[21] => 30
[22] => 32
[29] => 43
[47] => 28
[53] => 46
[55] => 44
[76] => 55
[81] => 41
[90] => 42
[98] => 37
[102] => 54
[115] => 20
[131] => 60
[154] => 49
[155] => 0
[158] => 31
[165] => 51
[168] => 68
[172] => 15
[173] => 25
[179] => 39
[222] => 16
[226] => 34
[252] => 52
[317] => 18
[377] => 29
[397] => 33
[406] => 36
[467] => 57
[519] => 27
[531] => 22
[570] => 23
[877] => 24
[1221] => 62
)
[cb] => Array
(
[0] => 72
[3] => 70
[5] => 66
[6] => 57
[8] => 60
[12] => 65
[19] => 71
[81] => 56
[94] => 67
[103] => 58
[115] => 106
[140] => 74
[168] => 78
[170] => 73
[221] => 63
[293] => 93
[346] => 54
[635] => 89
[1221] => 84
)
)
[DOTZ] => Array
(
[diametru] => Array
(
[0] => 17
[2] => 15
[3] => 18
[5] => 19
[6] => 16
[44] => 20
)
[latime] => Array
(
[0] => 7
[1] => 8
[2] => 6
[50] => 9
)
[pcd] => Array
(
[0] => 5
[2] => 4
[35] => 6
)
[pcd1] => Array
(
[0] => 112
[2] => 108
[5] => 120
[7] => 114
[9] => 100
[18] => 105
[35] => 139
[49] => 127
[62] => 98
[91] => 115
[95] => 130
[107] => 165
)
[et] => Array
(
[0] => 38
[1] => 45
[2] => 25
[5] => 35
[11] => 20
[12] => 48
[18] => 41
[21] => 40
[26] => 30
[31] => 34
[35] => 12
[43] => 0
[102] => 36
[107] => 8
[109] => 28
[113] => 33
[125] => 15
[158] => 32
[161] => 42
[164] => -12
[192] => 13
[204] => 24
[260] => 5
[471] => 6
)
[cb] => Array
(
[0] => 70
[2] => 65
[5] => 73
[7] => 71
[9] => 60
[18] => 56
[19] => 72
[35] => 110
[53] => 66
[62] => 58
[82] => 57
[92] => 74
[95] => 84
[107] => 122
[113] => 100
[126] => 67
[182] => 106
[204] => 93
[250] => 111
)
)
[AEZ] => Array
(
[diametru] => Array
(
[0] => 16
[1] => 17
[5] => 18
[7] => 19
[15] => 20
[115] => 21
[139] => 22
)
[latime] => Array
(
[0] => 7
[1] => 8
[14] => 9
[103] => 6
[115] => 11
[122] => 10
)
[pcd] => Array
(
[0] => 5
[263] => 4
)
[pcd1] => Array
(
[0] => 100
[1] => 120
[2] => 108
[3] => 115
[5] => 139
[6] => 112
[7] => 114
[15] => 130
[157] => 127
[179] => 110
[209] => 105
)
[et] => Array
(
[0] => 35
[1] => 45
[2] => 48
[3] => 40
[5] => 30
[13] => 38
[14] => 46
[15] => 50
[22] => 42
[27] => 51
[29] => 34
[30] => 20
[35] => 25
[44] => 47
[56] => 29
[63] => 37
[67] => 32
[73] => 52
[82] => 39
[88] => 44
[99] => 43
[100] => 28
[104] => 33
[107] => 18
[109] => 23
[135] => 17
[151] => 54
[162] => 53
[171] => 49
[179] => 31
[183] => 36
[189] => 57
[237] => 56
[296] => 41
[381] => 27
[433] => 24
[485] => 14
[593] => 26
[648] => 12
[740] => 16
)
[cb] => Array
(
[0] => 60
[1] => 73
[2] => 70
[5] => 95
[7] => 72
[8] => 71
[14] => 74
[23] => 63
[27] => 66
[30] => 67
[42] => 57
[69] => 65
[209] => 56
[236] => 64
)
)
)
Note that on your code, you are trying to access $key as an array, but it's an string: $key['marca']. Array keys can not be arrays.
Due to your array structure, you probably will need to do this (if I understood well your question)
// The items that you are searching
$marca = 'ALCAR STAHLRAD';
$diametru = 15;
$latime = 7;
// The default is that nothing was found.
$found = false;
// First we check if there's an item with the given $marca
if (array_key_exists($marca, $items)) {
$item = $items[$marca];
// Now we check if diametru and latime exists.
// I stored it on variables to make easier to understand.
// ----------
// `in_array()` search for values inside the array.
// If the value that you are searching is not a item,
// but an array key, then use `array_key_exists`.
$diametruExists = in_array($diametru, $item['diametru']);
$latimeExists = in_array($diametru, $item['diametru']);
// If both $diametruExists and $latimeExists are true
// we set that $found is true.
if ($diametruExists && latimeExists) {
$found = true;
}
}
if ($found) {
echo $marca;
echo $diametru;
echo $latime;
} else {
echo "Not found any product";
}
The downside of this solution is that you can't get the other values like pcd, just check if the given combination exists.
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.