PHP: Show results from array and search 3 values - php

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.

Related

Sort an array with the combiation of numbers, number_letters, letters_numbers and letters only

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));
}
});

how to Convert multidimensional array into single array

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

PHP arrays with array_merge and array_diff

I have the following code
$usersarray =array();
foreach($users as $oneitem){
$usersarray[]=$oneitem->user_id;
}
print_r($usersarray);
$resultarray =array();
foreach($result as $oneitem){
$resultarray[]=$oneitem->friend_user_id;
}
$results = implode(", ",$result);
print_r($resultarray);
echo $results;
$excluded_user = array_diff($usersarray , $resultarray);
$excluded_user =implode(", ",$excluded_user); //comma separated ids of users whom you want to exclude
echo $excluded_user;
This outputs the following for $usersarray:
Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 1 [5] => 42 [6] => 43 [7] => 44 [8] => 45 [9] => 46 [10] => 47 [11] => 48 [12] => 49 [13] => 50 [14] => 51 [15] => 52 [16] => 53 [17] => 54 [18] => 55 [19] => 56 [20] => 57 [21] => 58 [22] => 59 [23] => 60 [24] => 61 [25] => 62 [26] => 63 [27] => 64 [28] => 65 [29] => 66 [30] => 86 [31] => 103 [32] => 121 [33] => 123 [34] => 124 [35] => 125 [36] => 143 [37] => 147 [38] => 149 [39] => 150 )
But for $resultarray I get this:
Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => )
However when I echo $results I get: 64, 56, 53, 47, 44, 57, 43, 50, 1, 47, 59 which is the correct list of ID's.
It seems to be breaking down at the $excluded_user = array_diff($usersarray , $resultarray); stage. I suspect this is because of the format of $resultarray causing the array_diff() to not work properly.
Can anyone suggest why $resultarray is getting output like this? Or if this is even where the code is falling down.
Thanks in advance for any suggestions great appreciation.

php json_encode does not work properbly - "{" instead of "[" for last element

I have this array and I want to encode it into json..
Array
(
[0] => Array
(
[0] => . Hour
[1] => bteam_pvp
[2] => crackpack
[3] => tppi
[4] => agrarian_plus
[5] => agrarian2
[6] => bteam_pve
[7] => agrarian
[8] => horizons
[9] => m_lobby
[10] => m_north
[11] => m_south
[12] => m_east
[13] => hub
)
[1] => Array
(
[0] => 1
[1] => 94
[2] => 96
[3] => 96
[4] => 98
[5] => 71
[6] => 49
[7] => 64
[8] => 84
[9] => 91
[10] => 70
[11] => 97
[12] => 83
[13] => 98
)
[2] => Array
(
[0] => 2
[1] => 95
[2] => 97
[3] => 95
[4] => 97
[5] => 70
[6] => 42
[7] => 57
[8] => 94
[9] => 97
[10] => 85
[11] => 98
[12] => 90
[13] => 99
)
[3] => Array
(
[0] => 3
[1] => 94
[2] => 96
[3] => 96
[4] => 97
[5] => 63
[6] => 37
[7] => 56
[8] => 96
[9] => 97
[10] => 96
[11] => 98
[12] => 93
[13] => 99
)
[4] => Array
(
[0] => 4
[1] => 94
[2] => 96
[3] => 98
[4] => 97
[5] => 65
[6] => 26
[7] => 60
[8] => 96
[9] => 97
[10] => 94
[11] => 98
[12] => 93
[13] => 97
)
[5] => Array
(
[0] => 5
[1] => 96
[2] => 97
[3] => 99
[4] => 96
[5] => 58
[6] => 63
[7] => 66
[8] => 87
[9] => 98
[10] => 96
[11] => 95
[12] => 94
[13] => 99
)
[6] => Array
(
[0] => 6
[1] => 97
[2] => 98
[3] => 98
[4] => 96
[5] => 45
[6] => 87
[7] => 63
[8] => 82
[9] => 97
[10] => 96
[11] => 98
[12] => 95
[13] => 99
)
[7] => Array
(
[0] => 7
[1] => 96
[2] => 98
[3] => 98
[4] => 97
[5] => 65
[6] => 80
[7] => 76
[8] => 56
[9] => 98
[10] => 93
[11] => 99
[12] => 96
[13] => 99
)
[8] => Array
(
[0] => 8
[1] => 98
[2] => 98
[3] => 99
[4] => 98
[5] => 71
[6] => 88
[7] => 83
[8] => 79
[9] => 99
[10] => 95
[11] => 99
[12] => 97
[13] => 99
)
[9] => Array
(
[0] => 9
[1] => 98
[2] => 98
[3] => 99
[4] => 98
[5] => 64
[6] => 95
[7] => 78
[8] => 85
[9] => 99
[10] => 95
[11] => 99
[12] => 97
[13] => 99
)
[10] => Array
(
[0] => 10
[1] => 98
[2] => 98
[3] => 99
[4] => 98
[5] => 75
[6] => 94
[7] => 77
[8] => 95
[9] => 99
[10] => 95
[11] => 98
[12] => 97
[13] => 99
)
[11] => Array
(
[0] => 11
[1] => 98
[2] => 98
[3] => 98
[4] => 98
[5] => 73
[6] => 94
[7] => 79
[8] => 98
[9] => 98
[10] => 88
[11] => 98
[12] => 97
[13] => 99
)
[12] => Array
(
[0] => 12
[1] => 98
[2] => 98
[3] => 98
[4] => 98
[5] => 75
[6] => 94
[7] => 84
[8] => 98
[9] => 98
[10] => 81
[11] => 98
[12] => 97
[13] => 99
)
[13] => Array
(
[0] => 13
[1] => 98
[2] => 95
[3] => 97
[4] => 98
[5] => 87
[6] => 95
[7] => 83
[8] => 98
[9] => 97
[10] => 80
[11] => 98
[12] => 97
[13] => 99
)
[14] => Array
(
[0] => 14
[1] => 96
[2] => 98
[3] => 96
[4] => 98
[5] => 85
[6] => 84
[7] => 80
[8] => 95
[9] => 98
[10] => 92
[11] => 98
[12] => 97
[13] => 99
)
[15] => Array
(
[0] => 15
[1] => 88
[2] => 96
[3] => 89
[4] => 98
[5] => 79
[6] => 60
[7] => 78
[8] => 97
[9] => 98
[10] => 72
[11] => 96
[12] => 94
[13] => 99
)
[16] => Array
(
[0] => 16
[1] => 88
[2] => 94
[3] => 93
[4] => 98
[5] => 69
[6] => 50
[7] => 72
[8] => 97
[9] => 97
[10] => 50
[11] => 94
[12] => 96
[13] => 99
)
[17] => Array
(
[0] => 17
[1] => 84
[2] => 95
[3] => 70
[4] => 95
[5] => 65
[6] => 39
[7] => 61
[8] => 97
[9] => 95
[10] => 40
[11] => 91
[12] => 96
[13] => 98
)
[18] => Array
(
[0] => 18
[1] => 77
[2] => 91
[3] => 83
[4] => 94
[5] => 69
[6] => 61
[7] => 59
[8] => 85
[9] => 90
[10] => 59
[11] => 76
[12] => 90
[13] => 96
)
[19] => Array
(
[0] => 19
[1] => 74
[2] => 96
[3] => 93
[4] => 95
[5] => 59
[6] => 31
[7] => 59
[8] => 77
[9] => 94
[10] => 56
[11] => 80
[12] => 85
[13] => 96
)
[20] => Array
(
[0] => 20
[1] => 87
[2] => 95
[3] => 75
[4] => 96
[5] => 62
[6] => 22
[7] => 45
[8] => 76
[9] => 93
[10] => 73
[11] => 87
[12] => 62
[13] => 96
)
[21] => Array
(
[0] => 21
[1] => 83
[2] => 92
[3] => 71
[4] => 98
[5] => 50
[6] => 37
[7] => 56
[8] => 83
[9] => 88
[10] => 97
[11] => 93
[12] => 77
[13] => 97
)
[22] => Array
(
[0] => 22
[1] => 84
[2] => 91
[3] => 87
[4] => 97
[5] => 50
[6] => 26
[7] => 67
[8] => 56
[9] => 86
[10] => 91
[11] => 82
[12] => 66
[13] => 96
)
[23] => Array
(
[0] => 23
[1] => 91
[2] => 86
[3] => 78
[4] => 98
[5] => 47
[6] => 31
[7] => 67
[8] => 81
[9] => 83
[10] => 66
[11] => 56
[12] => 55
[13] => 88
)
[24] => Array
(
[0] => 24
[1] => 91
[2] => 81
[3] => 64
[4] => 98
[5] => 72
[6] => 37
[8] => 63
[9] => 93
[10] => 59
[11] => 92
[12] => 67
[13] => 98
)
)
So I use this:
[<?php
foreach ($gPerformance as $row) {
echo json_encode($row) . ",";
}
?>]
Unfortunately does this echo this:
[
[". Hour","bteam_pvp","crackpack","tppi","agrarian_plus","agrarian2","bteam_pve","agrarian","horizons","m_lobby","m_north","m_south","m_east","hub"],
[1,89,96,90,98,81,52,71,98,98,71,96,93,99],
[2,88,94,93,98,69,50,72,97,97,50,94,96,99],
[3,84,95,70,95,65,39,61,97,95,40,91,96,98],
[4,77,91,83,94,69,61,59,85,90,59,76,90,96],
[5,74,96,93,95,59,31,59,77,94,56,80,85,96],
[6,87,95,75,96,62,22,45,76,93,73,87,62,96],
[7,83,92,71,98,50,37,56,83,88,97,93,77,97],
[8,84,91,87,97,50,26,67,56,86,91,82,66,96],
[9,91,86,78,98,47,31,67,81,83,66,56,55,88],
[10,91,81,64,98,72,37,72,63,93,59,92,67,98],
[11,91,93,96,98,64,50,63,50,92,57,98,83,99],
[12,92,96,94,98,61,42,59,93,98,98,99,93,99],
[13,91,95,95,98,46,41,61,98,98,97,98,96,99],
[14,91,95,98,97,51,24,67,98,98,97,98,93,94],
[15,94,97,98,97,47,74,67,94,98,97,97,93,99],
[16,97,98,97,96,47,91,74,94,97,98,98,94,99],
[17,95,98,98,97,67,90,86,59,98,95,99,97,99],
[18,98,98,99,98,58,95,82,54,99,97,99,98,99],
[19,98,98,99,98,70,95,86,91,98,97,99,97,99],
[20,98,98,98,98,81,93,85,98,99,97,99,97,99],
[21,98,97,97,98,72,92,77,97,98,97,98,97,100],
[22,97,98,96,98,64,92,84,98,98,95,98,97,99],
[23,97,90,95,98,86,93,77,98,96,95,99,97,99],
{"0":24,"1":94,"2":96,"4":98,"5":82,"6":69,"8":97,"9":98,"10":96,"11":98,"12":97,"13":99}
]
Can you explain me why the last element has {} brackets? Even if I add a 25th array to it, the 24th keeps the brackets and I have no idea why.
In order to be encoded as an array literal rather than an object, the array must be a non-sparse, indexed array.
In your case, your last array is missing [7], making it sparse, and therefore encoded as an object.
As you can see, the last row is the array with assigned keys:
{"0":24,"1":94,"2":96,"4":98,"5":82,"6":69,"8":97,"9":98,"10":96,"11":98,"12":97,"13":99}
Why? Because you missed key "3", which is in the input you gave, but isn't in json output. Check your input array and keys of last row.
If you don't want to edit your input array, do it like this:
[<?php
foreach ($gPerformance as $row) {
echo json_encode(array_values($row)) . ",";
}
?>]

how to insert into array after one iteration of loop?

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());

Categories