PHP arrays with array_merge and array_diff - php

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.

Related

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

get value back of array number in php

I have a array like this :
Array
(
[0] => 395
[1] => 0
[2] => 395
[3] => 395
[4] => 39
[5] => 17
[6] => 11
[7] => 35
[8] => 21
[9] => 11
[10] => 11
[11] => 0
[12] => 0
[13] => 0
[14] => 0
[15] => 0
[16] => 0
[17] => 375
[18] => 0
[19] => 0
[20] => 0
[21] => 0
[22] => 22
[23] => 215
[24] => 215
[25] => 42
[26] => 163
[27] => 163
[28] => 61
[29] => 61
[30] => 134
[31] => 134
)
Now, i get the maximum Value of that array with this code :
echo max($similar);
For the array i said, the output will be : 395 that is in the array[0] and array[2] and array[3].
Now, i want to know How can i give this number (395) and get the location of that in the array ?
For example,
I need a function like this :
echo_value_from_num(395); // Output :: 0
The output is zero, mean first time 395 appeard in the [0] of array.
How we can get that number in the array with that Values ?
The function array_serach (http://no2.php.net/array_search) will return the key for a certain element.
In your example, it would be something like this:
$max = max($similar);
$key = array_search($max,$similar);
This will give you the locations (keys) with the highest values,
$max = array_keys($array, max($array));

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

Can someone spot the error with this array?

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.

Going through PHP array

I have a php array I would like get a specific data from it.
[vxs_data] => Array
(
[0] => Array
(
[data] => Array
(
[0] => fsafas
[1] => 603
[2] => 39
[3] => 81
[4] => 12
[5] => 43
[6] => 186
[7] => 97
[8] => 129
)
)
[1] => Array
(
[data] => Array
(
[9] => fsdfsa
[10] => 60
[11] => 30
[12] => 184
[13] => 12
[14] => 7
[15] => 176
[16] => 132
[17] => 119
)
)
[2] => Array
(
[data] => Array
(
[18] => fsafsa
[19] => 60
[20] => 3121
[21] => 18
[22] => 11
[23] => 0
[24] => 199
[25] => 140
[26] => 117
)
)
[3] => Array
(
[data] => Array
(
[27] => dada
[28] => 60
[29] => 27
[30] => 11
[31] => 22
[32] => 1
[33] => 22
[34] => 157
[35] => 98
)
)
[4] => Array
(
[data] => Array
(
[36] => ASKLMSDAS
[37] => 60
[38] => 232
[39] => 11
[40] => 23
[41] => 4
[42] => 32
[43] => 141
[44] => 98
)
)
The content of array about that. I would like to get that data in to table (td) so it would be like this:
<tr>
<td>ASKLMSDAS</td><td>33</td>...
</tr>
<tr>
<td>dada</td><td>33</td>...
</tr>
So I would like a make link of those first data columns like "ASKLMSDAS" "dada". So I need to do some sort of if clause maybe and foreach?
Thanks so much and sorry about my english.
Not exactly sure what you're going for, but heres a pretty generally way of making a table from the contents of 2d array. Hopefully you can tweak it to do what you want.
echo "<table>";
foreach($smliiga_data as $row_k => $row_v)
{
echo "<tr><td><strong>$row_k</strong></td>";
foreach($row_v['data'] as $k=>$v)
{
$str = is_int($v) ? "$k: $v" : "<a href='#'>$k: $v</a>"; //this makes link for the ones that are not numbers
echo "<td>$str</td>";
}
echo "</tr>";
}
echo "</table>";
This should work:
foreach ($mliiga_data as $row){
$linktext = $row[data][0];
//
// Dispaly linktext
//
}
$array = array_reverse($array);
foreach($array as $childArray)
{
echo '<tr><td><a href="#">';
echo current($childArray['data']); // write other html as you want, this will give you the element what you want
echo '</a></td><td>33</td>...</tr>';
}

Categories