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>';
}
Related
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.
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));
I have the following array structure. with the variable $sortie
Array
(
[0] => 480
[1] => 483
[2] => 497
[3] => 481
[4] => 478
[5] => 475
[6] => 476
[7] => 477
[8] => 498
[9] => 496
[10] => 502
[11] => 499
[12] => 494
[13] => 503
[14] => 493
[15] => 500
[16] => 484
[17] => 501
[18] => 495
[19] => 485
[20] => 489
[21] => 490
[22] => 488
[23] => 487
[24] => 486
)
I'm trying to achieve something by assigning them on a new array using the following code.
$release = array();
foreach ($sortie as $key_true => $value_true) {
$release[$key_true] = $value_true;
echo $key_true.'---'.$value_true.'<br>';
}
So far the echo results are going as expected with the correct order based on $sortie
however the $release array is not following the same ordering not assigning the $key_true to the $release array. Would appreciate any help why it's doing this.
EDIT
Sorry almost forgot, result of $release the values are the original key and original value
Array
(
[0] => 0---480
[1] => 1---483
[2] => 10---502
[3] => 11---499
[4] => 12---494
[5] => 13---503
[6] => 14---493
[7] => 15---500
[8] => 16---484
[9] => 17---501
[10] => 18---495
[11] => 19---485
[12] => 2---497
[13] => 20---489
[14] => 21---490
[15] => 22---488
[16] => 23---487
[17] => 24---486
[18] => 3---481
[19] => 4---478
[20] => 5---475
[21] => 6---476
[22] => 7---477
[23] => 8---498
[24] => 9---496
)
The best practice is change array to this structure:
$sortie = [480, 483, ...];
$release = [];
foreach($sortie as $val) {
$release[$val] = true;
}
And then you can use php array sorting functions
Try putting in $release == array();, it may work around for you as far as assigning is concerned
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 have an associative array in php.
the content of associative array looks like this:
Array
(
[0] => Array
(
[0] => 3
[1] => 1
[2] => 0
[3] => 50074494
[4] => 25013372
[5] => 2
[6] => 474
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 985
[12] => 34951
[13] => 18143
[14] => 4
[15] => 2
[16] => 94
[17] => 1
[18] => 1.26
[19] => 7.9
[20] => 2013-06-27 10:19:21
)
[1] => Array
(
[0] => 5
[1] => 1
[2] => 0
[3] => 50078122
[4] => 25000164
[5] => 2
[6] => 463
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 860
[12] => 28290
[13] => 16944
[14] => 4
[15] => 1
[16] => 94
[17] => 1
[18] => 1.13
[19] => 7.1
[20] => 2013-06-27 10:19:51
)
[2] => Array
(
[0] => 4
[1] => 1
[2] => 0
[3] => 50078630
[4] => 24995538
[5] => 2
[6] => 155
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 616
[12] => 23203
[13] => 4892
[14] => 3
[15] => 1
[16] => 95
[17] => 0
[18] => 1.04
[19] => 6.5
[20] => 2013-06-27 10:20:21
)
)
I would like to be able to assign the inner array values to a variable. I need variable to look like this:
echo $variable
3 1 0 50074494 25013372 2 474 .. 2013-06-27 10:19:21
.
.
I have this code so far:
$variable;
foreach ($lines as $key => $value) {
foreach ($value as &$val)
{
$variable=$variable . $val . ' ';
}
echo $variable;
echo "\n";
}
with this code it looks like I am getting 3 times of variable. Any ideas what I am doing wrong here?
If you have an array, and you want to store the values in a space-separated string, you could do this:
$string = implode(' ', $array);
echo $string;
So your loop might look like this:
foreach ($lines as $value) {
$value[20] = '"'. $value[20] .'"'; // from comments
echo implode(' ', $value) ."\n";
}
I would recommend the use of implode instead of foreach. Also, it doesn't seem like you need the value of $key:
foreach ($lines as $value) {
echo implode(" ", $value);
echo "\n";
}
Also, I'm not sure I quite understood your question? What do you mean by "3 times of variable"?