how to insert into array after one iteration of loop? - php

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

Related

Generate Range in used PHP and Skip already

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.

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

How to find the most occurring values from a collection?

Hi I have a collection of numbers and I want to find the most occurring number and then the 2nd most occurring and then the third most occurring till 10 and store the result in a array.
The collection looks like this
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => 12
[1] => 194
[2] => 241
[3] => 4
[4] => 29
[5] => 4
[6] => 12
[7] => 15
[8] => 21
[9] => 31
[10] => 281
[11] => 4
[12] => 6
[13] => 4
[14] => 2
[15] => 6
[16] => 4
[17] => 4
[18] => 4
[19] => 15
[20] => 4
[21] => 4
[22] => 13
[23] => 10
[24] => 8
[25] => 8
[26] => 2
[27] => 2
[28] => 2
[29] => 17
[30] => 4
[31] => 20
[32] => 2
[33] => 4
[34] => 20
[35] => 6
)
)
So I want to find the most occurring one and so on till 10th most occurring number.
use array_count_values, here is your reference link http://www.w3schools.com/php/func_array_count_values.asp
<?php
$a=array("A","Cat","Dog","A","Dog");
print_r(array_count_values($a));
?>
Output
Array ( [A] => 2 [Cat] => 1 [Dog] => 2 )

How to reverse an array [duplicate]

This question already has answers here:
Reverse array in php
(6 answers)
Closed 8 years ago.
So this is my array, I have tried rsort and array_reverse but its sorting it numerically and that's not what I want. I just want it in the opposite order.
[15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16]
You need to add true to your array_reverse:
array array_reverse ( array $array [, bool $preserve_keys = false ] )
In other words, try something like this:
array_reverse( $yourArray, true);
This way your keys are preserved and you achieve what you need.
array_reverse should work. I just did this test:
$array = array(15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16);
$rArray = array_reverse($array);
print_r($array);
print_r($rArray);
and the results were:
//for $array
Array ( [0] => 15 [1] => 14 [2] => 13 [3] => 12 [4] => 11 [5] => 10 [6] => 9 [7] => 8 [8] => 7 [9] => 6 [10] => 5 [11] => 4 [12] => 3 [13] => 2 [14] => 1 [15] => 30 [16] => 29 [17] => 28 [18] => 27 [19] => 26 [20] => 25 [21] => 24 [22] => 23 [23] => 22 [24] => 21 [25] => 20 [26] => 19 [27] => 18 [28] => 17 [29] => 16 )
//for $rArray
Array ( [0] => 16 [1] => 17 [2] => 18 [3] => 19 [4] => 20 [5] => 21 [6] => 22 [7] => 23 [8] => 24 [9] => 25 [10] => 26 [11] => 27 [12] => 28 [13] => 29 [14] => 30 [15] => 1 [16] => 2 [17] => 3 [18] => 4 [19] => 5 [20] => 6 [21] => 7 [22] => 8 [23] => 9 [24] => 10 [25] => 11 [26] => 12 [27] => 13 [28] => 14 [29] => 15 )
In order to reverse an array simply put it in a stack (or at least use that logic).
FILO (First in last out) thus you have swapped the order.
A Stack Overflow question around php stacks can be found here:
PHP Stack Implementation
Are you familiar with other languages? I don't use php much.
Simply create a new object and add the end to it. Ex
new Array a
a.push(oldArray.removelast())...
The do-it-yourself-and-more-fun solution:
$oldArray = array(15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16);
$length = count($oldArray);
$start= $length - 1;
$newArray=array();
for ($x = $start; $x >=0; $x--)
{
array_push($newArray,$oldArray[$x]);
}
print_r($newArray);
Prints:
Array ( [0] => 16 [1] => 17 [2] => 18 [3] => 19 [4] => 20 [5] => 21 [6] => 22 [7] => 23 [8] => 24 [9] => 25 [10] => 26 [11] => 27 [12] => 28 [13] => 29 [14] => 30 [15] => 1 [16] => 2 [17] => 3 [18] => 4 [19] => 5 [20] => 6 [21] => 7 [22] => 8 [23] => 9 [24] => 10 [25] => 11 [26] => 12 [27] => 13 [28] => 14 [29] => 15 )

Categories