Selecting only even numbers in php output [duplicate] - php

This question already has answers here:
How to filter an array by a condition
(9 answers)
Closed 6 years ago.
I am trying to select and display only the even number in a separate output from this below
function toms($c,$first = 0,$second = 1)
{
$toms = [$first,$second];
for($i=1;$i<$c;$i++)
{
$toms[] = $toms[$i]+$toms[$i-1];
}
return $toms;
}
echo "<pre>";
print_r(toms(33));
?>
currently this outputs
array
(
[0] => 0
[1] => 1
[2] => 1
[3] => 2
[4] => 3
[5] => 5
[6] => 8
[7] => 13
[8] => 21
[9] => 34
[10] => 55
[11] => 89
[12] => 144
[13] => 233
[14] => 377
[15] => 610
[16] => 987
[17] => 1597
[18] => 2584
[19] => 4181
[20] => 6765
[21] => 10946
[22] => 17711
[23] => 28657
[24] => 46368
[25] => 75025
[26] => 121393
[27] => 196418
[28] => 317811
[29] => 514229
[30] => 832040
[31] => 1346269
[32] => 2178309
[33] => 3524578
)
Anyone know how I can display only the even numbers returned, so I would want to have 2, 8, 34 and so on
thank you

You can use array_filter
print_r(array_filter(toms(33), function($number){
return $number % 2 == 0;
}));
Or if you want to filter out 0:
print_r(array_filter(toms(33), function($number){
return $number != 0 && $number % 2 == 0;
}));
A bit more readable:
$isEvenNumber = function($number) {
return $number % 2 == 0;
}
$numbers = toms(33);
$filtered_numbers = array_filter($numbers, $isEvenNumber);
var_dump($filtered_numbers);

Related

Split array into a unspecific number of chuncks

I have a string that contains the given answers from a qiuz by a user. This string is saved on the db table and it has this form.
,P1,1,2,3,5,8,9,,P2,1,3,4,5,6,8,9,,P3,1,2,3,4,6,8,9,,P4,1,,P5a,b,,P5b,d,,P5c,a,,P5d,c,,P6,4,,P7,2,,P8a,hc,,P8b,df,,P8c,bg,,P8d,e,,P9,4,,P10,3,,P11,4,,P12,2,,P13,3,,P14,3,,P15a,acejg,,P15b,dfhib,,P16,1,3,,P17,2,,P18,1,,P19,3,,P20,3,5,6
Ater getting the string i explode it, and the form becomes like this.
Array
(
[0] => P1
[1] => 1
[2] => 2
[3] => 3
[4] => 5
[5] => 8
[6] => 9
[7] => P2
[8] => 1
[9] => 3
[10] => 4
[11] => 5
[12] => 6
[13] => 8
[14] => 9
[15] => P3
[16] => 1
[17] => 2
[18] => 3
[19] => 4
[20] => 6
[21] => 8
[22] => 9
[23] => P4
[24] => 1
[25] => P5a
[26] => b
[27] => P5b
[28] => d
[29] => P5c
[30] => a
[31] => P5d
[32] => c
[33] => P6
[34] => 4
[35] => P7
[36] => 2
[37] => P8a
[38] => hc
[39] => P8b
[40] => df
[41] => P8c
[42] => bg
[43] => P8d
[44] => e
[45] => P9
[46] => 4
[47] => P10
[48] => 3
[49] => P11
[50] => 4
[51] => P12
[52] => 2
[53] => P13
[54] => 3
[55] => P14
[56] => 3
[57] => P15a
[58] => acejg
[59] => P15b
[60] => dfhib
[61] => P16
[62] => 1
[63] => 3
[64] => P17
[65] => 2
[66] => P18
[67] => 1
[68] => P19
[69] => 3
[70] => P20
[71] => 3
[72] => 5
[73] => 6
)
each question starts with a "P". So question 1 will be "P1" question 2 "P2" and so on.
Each given answer is between the "P" until next "P".
So the answers given from the user for the question number 1 are " 1 | 2 | 3 | 5 | 8 | 9 ".
I was trying to insert the values to another array with a foreach loop but i could not find any way to seperate the answers.
My desire output is.
Array
(
[P1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 5
[4] => 8
[5] => 9
)
[P2] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 6
[4] => 8
[5] => 9
)
)
Please tell me if you have any suggestions.Thank you very much
assuming we ve everything inside $p
$res= array();
foreach($p as $key => $value){
if(strpos($value, "P") !== false){
$currentP = $value;
$res[$currentP] = array();
}elseif($value){
$res[$currentP][] = $value;
}
}
WORKING PHP FIDDLE tested with v5 and v7
This seems to work :
<?php
$string = ',P1,1,2,3,5,8,9,,P2,1,3,4,5,6,8,9,,P3,1,2,3,4,6,8,9,,P4,1,,P5a,b,,P5b,d,,P5c,a,,P5d,c,,P6,4,,P7,2,,P8a,hc,,P8b,df,,P8c,bg,,P8d,e,,P9,4,,P10,3,,P11,4,,P12,2,,P13,3,,P14,3,,P15a,acejg,,P15b,dfhib,,P16,1,3,,P17,2,,P18,1,,P19,3,,P20,3,5,6';
$array = explode(',', $string);
$return = array();
$index = '';
foreach ($array as $v) {
if (trim($v) == '') // Skip empty values
continue;
if (substr($v, 0, 1) == 'P') {
$index = $v;
if (!isset($return[$index]))
$return[$index] = array();
} else {
$return[$index][] = $v;
}
}
var_dump($return);
?>
this could be an approach
<?php
$string = ',,P1,1,2,3,5,8,9,,P2,1,3,4,5,6,8,9,,P3,1,2,3,4,6,8,9,,P4,1,,P5a,b,,P5b,d,,P5c,a,,P5d,c,,P6,4,,P7,2,,P8a,hc,,P8b,df,,P8c,bg,,P8d,e,,P9,4,,P10,3,,P11,4,,P12,2,,P13,3,,P14,3,,P15a,acejg,,P15b,dfhib,,P16,1,3,,P17,2,,P18,1,,P19,3,,P20,3,5,6';
$sepAns = explode(',,',$string);
echo $sepAns[2];
?>
Note that I added a , at the beginning to maintain integrity of ',,'
$str = ',P1,1,2,3,5,8,9,,P2,1,3,4,5,6,8,9,,P3,1,2,3,4,6,8,9,,P4,1,,P5a,b,,P5b,d,,P5c,a,,P5d,c,,P6,4,,P7,2,,P8a,hc,,P8b,df,,P8c,bg,,P8d,e,,P9,4,,P10,3,,P11,4,,P12,2,,P13,3,,P14,3,,P15a,acejg,,P15b,dfhib,,P16,1,3,,P17,2,,P18,1,,P19,3,,P20,3,5,6';
$arr = explode( ',', $str );
$structuredArr = array();
foreach ( $arr AS $element ) {
if ( ( strpos( $element, 'P' ) === FALSE ) === FALSE ) {
$key = $element;
$structuredArr[ $key ] = array();
} else {
if ( isset( $key ) ) {
if ( !empty( $element ) ) {
$structuredArr[ $key ][] = $element;
}
}
}
}

Array merge failed

maybe it's simple as it look, but it driving me crazy.
Here's my array :
$result =
Array
(
[0] => Array
(
[0] => 4
[1] => 4
[2] => 4
[3] => 4
[4] => 4
[5] => 4
[6] => 4
[7] => 4
[8] => 4
[9] => 4
[10] => 40
)
[1] => Array
(
[0] => 5
[1] => 4
[2] => 4
[3] => 4
[4] => 4
[5] => 4
[6] => 4
[7] => 4
[8] => 4
[9] => 4
[10] => 41
)
[2] => Array
(
[0] => 5
[1] => 5
[2] => 5
[3] => 5
[4] => 5
[5] => 5
[6] => 5
[7] => 5
[8] => 5
[9] => 5
[10] => 50
)
)
this is what i want transformed into :
after array_merge:
$result =
Array
(
[0] => 4
[1] => 4
[2] => 4
[3] => 4
[4] => 4
[5] => 4
[6] => 4
[7] => 4
[8] => 4
[9] => 4
[10] => 40
[11] => 5
[12] => 4
[13] => 4
[14] => 4
[15] => 4
[16] => 4
[17] => 4
[18] => 4
[19] => 4
[20] => 4
[21] => 41
[22] => 5
[23] => 5
[24] => 5
[25] => 5
[26] => 5
[27] => 5
[28] => 5
[29] => 5
[30] => 5
[31] => 5
[32] => 50
)
this is the code :
<?php
$result = array();
?>
#foreach ($detail_ratings as $detail_rating)
<?php $result[] = json_decode($detail_rating); ?>
#endforeach
<?php
$result = array_merge($result[0],$result[1],$result[2]);
?>
{{print_r($result)}}
how do i make it automatically without manually using this code :
$result = array_merge($result[0],$result[1],$result[2]);
this is what i already did :
foreach ($result as $key => $value) {
$values[] = array_merge($value,$result[$key]);
}
Use this
$result=array();
foreach($values as $value) {
$result = array_merge($result,$value);
}
Maybe this can help :-
function nameit(array $arr)
{
$newarr = array();
foreach($arr as $a => $b)
{
$newarr[] = $b;
}
return $newarr;
}
Try this:
$final_array = array_merge($array1,$array2);

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 )

PHP value=arraykey + arraykey + arraykey [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I Have 3 Arrays
Array 1 :
Array ( [0] => 0 [1] => 256 [2] => 512 [3] => 768 [4] => 1024 [5] => 1280 [6] => 1536 [7] => 1792 [8] => 2048 [9] => 2304 [10] => 2560 [11] => 2816 [12] => 3072 [13] => 3328 [14] => 3584 [15] => 3840 [16] => 4096 [17] => 4352 [18] => 4608 [19] => 4864 [20] => 5120 [21] => 5376 [22] => 5632 [23] => 5888 )
Array 2 :
Array ( [0] => 0 [1] => 65536 [2] => 131072 [3] => 196608 [4] => 262144 [5] => 327680 [6] => 393216 [7] => 458752 [8] => 524288 [9] => 589824 [10] => 655360 [11] => 720896 [12] => 786432 [13] => 851968 [14] => 917504 [15] => 983040 [16] => 1048576 [17] => 1114112 [18] => 1179648 [19] => 1245184 [20] => 1310720 [21] => 1376256 [22] => 1441792 [23] => 1507328 [24] => 1572864 [25] => 1638400 [26] => 1703936 [27] => 1769472 [28] => 1835008 [29] => 1900544 [30] => 1966080 [31] => 2031616 )
Array 3 :
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 )
Given a number X which I know was calculated as a1[i] + a2[j] + a3[k] how can I calculate i, j and k?
Examples:
X = 458752 => i=0, j=7, k=0 which is 458752 = 0 + 458752 + 0
X = 131586 => i=2, j=2, k=2 which is 131586 = 256 + 131072 + 2
X = 65793 => i=1, j=1, k=1 which is 65793 = 256 + 65536 + 1
How about:
foreach(array(458752, 131586, 65793) as $x) {
$j = intval($x / 65536);
$x = $x % 65536;
$i = intval($x / 256);
$k = $x % 256;
echo "x=$x, i=$i, j=$j, k=$k\n";
}
output:
x=0, i=0, j=7, k=0
x=514, i=2, j=2, k=2
x=257, i=1, j=1, k=1
This
$value = $array1[0]+$array1[7]+$array1[0]
should work.
Array indexing begins from 0, so $array[1] is the technically the second element in an array.
First you try to post your questions clearly.
Your first array count is mismatched with other two arrays. So if the 3 arrays count must be equal, easily you can get the values. if you need to customize addition value, hard coded the array values and try addition.
Method1: All the 3 arrays count is same.
<?php
$first = Array (0 => 0, 1 => 256, 2 => 512, 3 => 768, 4 => 1024, 5 => 1280, 6 => 1536, 7 => 1792, 8 => 2048, 9 => 2304, 10 => 2560, 11 => 2816, 12 => 3072, 13 => 3328, 14 => 3584, 15 => 3840, 16 => 4096, 17 => 4352, 18 => 4608, 19 => 4864, 20 => 5120, 21 => 5376, 22 => 5632, 23 => 5888 );
$second = Array (0 => 0, 1 => 65536, 2 => 131072, 3 => 196608, 4 => 262144, 5 => 327680, 6 => 393216, 7 => 458752, 8 => 524288, 9 => 589824, 10 => 655360, 11 => 720896, 12 => 786432, 13 => 851968, 14 => 917504, 15 => 983040, 16 => 1048576, 17 => 1114112, 18 => 1179648, 19 => 1245184, 20 => 1310720, 21 => 1376256, 22 => 1441792, 23 => 1507328);
$third = 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);
$result = array();
foreach($first as $key => $values){
echo '<br>'.$values.'+'.$second[$key].'+'.$third[$key];
$result[] = $values+$second[$key]+$third[$key];
}
print_r($result);
?>
Method 2: (Please avoid this method)
$first_result = $first[0]+$second[7]+$third[0];
$second_result = $first[2]+$second[2]+$third[2];
$third_result = $first[1]+$second[1]+$third[1];

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