get value back of array number in php - 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));

Related

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.

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

Looping over Dynamic Multi-dimensional Array with PHP

I am having difficulty with an multidimensional array. Here is a portion of the array. It's dynamic in that the top level array can have any number of elements and the child arrays, can have any number of elements as well. But each child array will contain the same amount of elements. If one child array has 16 elements, they will all have 16 elements, and if one has 20 elements, they will all have 20 elements.
[1] => Array
(
[1] => 0
[2] => 25
[3] => 38
[4] => 50
[5] => 63
[6] => 75
[7] => 85
[8] => 88
[9] => 100
[10] => 113
[11] => 125
[12] => 138
[13] => 150
[14] => 163
[15] => 175
[16] => 188
)
[2] => Array
(
[1] => 48
[2] => 37.22
[3] => 52.56
[4] => 63.17
[5] => 74.45
[6] => 87.98
[7] => 96.11
[8] => 98.36
[9] => 111.67
[10] => 132.20
[11] => 146.87
[12] => 160.85
[13] => 174.39
[14] => 187.70
[15] => 203.04
[16] => 215.90
)
What I am trying to do is to extract the data in a format like:
ProductCode: [2][1][1][2], Width: [2][1], Height: [1][2], Price: [2][2].
Can someone point me in the right direction of how I can loop over this to extract the data like I need to?
I ended up creating a stored procedure in SQL 2008 to handle this instead of using PHP.

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

Error returned with array_count_values after rounding and multiplying

I would like to be able to use array_count_values in the following way:
$roundedans = 10 * round($groupdiff, 1);
$diffarray[] = $roundedans;
print_r(array_count_values($diffarray));
The first two lines are part of a while loop that inserts $roundedans into the array and array_count_values is called outside of the loop.
When I try to run the code I get this error:
Warning: array_count_values() [function.array-count-values]: Can only count STRING and INTEGER values! in...
This is even though I've converted all of the values to an integer by multiplying by 10.
Any help would be appreciated.
This is the output from just print_r:
Array ( [0] => 6 [1] => 15 [2] => 8 [3] => 13 [4] => -60 [5] => 1 [6] => -61 [7] => 7 [8] => 49 [9] => 26 [10] => -3 [11] => -66 [12] => 20 [13] => 10 [14] => 6 [15] => -94 [16] => -1 [17] => -6 [18] => -19 [19] => -1 [20] => 48 [21] => -4 [22] => 45 [23] => 21 [24] => -11 [25] => 19 [26] => 1 [27] => -10 [28] => 4 [29] => -14 [30] => 26 [31] => -1 [32] => -20 [33] => 8 [34] => -17 [35] => -2 [36] => -6)
No non-integers.
Use:
$diffarray[] = intval($roundedans);
As round() always returns float, the type wouldn't change if you multiply it with 10.
intval() will change the type.

Categories