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 )
Related
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.
I have array named olmali
$olmali = $_POST['result'];
And
print_r($olmali);
Result is below:
Array (
[0] => 1
[1] => 1
[2] => 20
[3] => 2
[4] => 3
[5] => 5
[6] => 6
[7] => 7
[8] => 9
[9] => 8
[10] => 10
[11] => 11
[12] => 13
[13] => 12
[14] => 12
[15] => 14
[16] => 15
[17] => 16
[18] => 17
[19] => 17
[20] => 19
[21] => 20
)
I want to use SQL UPDATE command and I expect:
id test
1 1
2 1
3 20
4 2
5 3
6 ....and goes on
How can I resolve this problem? Is there any way and how can I do it. PHP array to column row in MySQL table with UPDATE SQL command like that
I would suggest to start from the end with a for loop and reassign every key with a value +1. Then, you just have to remove the first index of the array with unset(). See the code below.
$olmali = [
0 => 1,
1 => 1,
2 => 20,
3 => 2,
4 => 3,
5 => 5,
6 => 6,
7 => 7,
8 => 9,
9 => 8,
10 => 10,
11 => 11,
12 => 13,
13 => 12,
14 => 12,
15 => 14,
16 => 15,
17 => 16,
18 => 17,
19 => 17,
20 => 19,
21 => 20
];
for($i = count($olmali); $i > 0; $i--) {
$olmali[$i] = $olmali[$i - 1];
}
unset($olmali[0]);
print_r($olmali);
Output:
Array ( [1] => 1 [2] => 1 [3] => 20 [4] => 2 [5] => 3 [6] => 5 [7] => 6 [8] => 7 [9] => 9 [10] => 8 [11] => 10 [12] => 11 [13] => 13 [14] => 12 [15] => 12 [16] => 14 [17] => 15 [18] => 16 [19] => 17 [20] => 17 [21] => 19 [22] => 20 )
Assuming
$myArray = array (
[0] => 1
[1] => 1
[2] => 20
[3] => 2
[4] => 3
[5] => 5
[6] => 6
[7] => 7
[8] => 9
[9] => 8
[10] => 10
[11] => 11
[12] => 13
[13] => 12
[14] => 12
[15] => 14
[16] => 15
[17] => 16
[18] => 17
[19] => 17
[20] => 19
[21] => 20
);
then once you have a db connection using a PDO prepare binding and execute
$stmt = $conn->prepare("Update my_tabe
set my_column = :my_value
where my_id_comn = :my_id +1" );
$stmt->bindParam(':my_value', $value);
$stmt->bindParam(':my_id', $key);
foreach ($myArray as $key => $value){
$stmt->execute();
}
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 )
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 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.