Addition to values in a foreach-loop - php

So I have an array.
$numbers1 = [31, 60, 54, 7, 13, 2, 9, 68, 5, 2, 9, 68, 5, 2];
What I have to do is use a foreach-loop to sum each item with 20 and then place them in a new Array, $newArray. This is what I've come up with so far.
$numbers1 = [31, 60, 54, 7, 13, 2, 9, 68, 5, 2, 9, 68, 5, 2];
foreach ($numbers1 as &$value) {
$newArray = $value + 20;
}
But it doesn't seem to be working, as I receive the answer 22 instead of the array with the sum of the numbers. I know I have to echo it out, but I have to do that later in the exercise. I appreciate the help.

Your question basically works. Replace $newArray with $value as so:
$numbers1 = [31, 60, 54, 7, 13, 2, 9, 68, 5, 2, 9, 68, 5, 2];
foreach ($numbers1 as &$value) {
$value += 20;
}
Then if you need it in a new array use add the following line afterwards:
$newArray = $numbers1;
Since you are passing $value by reference you can use the $value += 20 line.
If you don't want the pointless array reassignment you can do the following:
$numbers1 = [31, 60, 54, 7, 13, 2, 9, 68, 5, 2, 9, 68, 5, 2];
foreach ($numbers1 as value) {
$newArray[] = $value + 20;
}

General solution, not suitable for this exact question
Use array_map instead(for better codestyle):
$newArray = array_map(function ($x) {
return $x + 20;
}, [31, 60, 54, 7, 13, 2, 9, 68, 5, 2, 9, 68, 5, 2]);
This will assign the function to each entry and thus will increase each value by 20.

Related

Finding lottery winners

Pretty sure my syntax is off, been a while since I've used PHP - any help is appreciated! Kind of a mix match of C at the moment. This is essentially a loop that's going through comparing both arrays before being passed to a second loop that throws in the rule that is required to win the 'lottery'
$pickedBalls = [1, 2, 3, 4, 5, 6];
$playerBalls = [
"Harry" => [6, 7, 8, 9, 10, 11],
"Jack" => [4, 5, 6, 7, 8, 9],
"Andrew" => [9, 10, 11, 12, 13, 14],
"Paul" => [15, 16, 17, 18, 19, 20],
"Andrew2" => [21, 22, 23, 24, 25, 26]
];
$results = [];
foreach ($playerBalls as $key => $value) {
for ($i = 0; $i < 6; $i++) {
for ($j = 0; $j < 6; $j++) {
if ($pickedBalls[$i] == $value[$j])
$results[$key]++;
}
}
}
I don't intend for up/downvote.
I recommend You to learn PHP essentials (YouTube: PHP tutorials for beginner)
About checking lottery winners - it can be done just intersecting 2 arrays and checking intersection size:
$drawnNumbers = [4, 8, 15, 16, 23, 42];
$ticketsPurchases = [
"Harry" => [3, 2, 31, 5, 12, 44],
"Jack" => [8, 42, 13, 23, 1, 49],
"Andrew" => [8, 17, 19, 22, 25, 31],
"Paul" => [11, 16, 20, 29, 31, 38],
"Andrew" => [17, 18, 20, 22, 31, 47]
];
$results = [];
$winners = [];
$winningCondition = 3;
foreach ($ticketPurchases as $player => $numbers)
{
$winningNumbers = array_intersect($numbers, $drawnNumbers); // intersection of player's ticket numbers and drawn numbers
$winningNumbersCount = sizeof($winningNumbers); // intersection size
$won = $winningNumbersCount >= $winningCondition; // if intersection size GTE winning condition (3)
$results[$player] = compact('player', 'winningNumbers', 'winningNumbersCount', 'won');
if($won) $winners[] = $player;
}
echo "\nRESULTS:\n";
print_r($results);
echo "\nWINNERS:\n";
echo implode("\n", $winners);
This might be what you are after
$pickedBalls = [4, 8, 15, 16, 23, 42];
$playerBalls = [
"Harry" => [3, 2, 31, 5, 12, 44],
"Jack" => [8, 42, 13, 23, 1, 49],
"Andrew" => [8, 17, 19, 22, 25, 31],
"Paul" => [11, 16, 20, 29, 31, 38],
"Andrew2" => [17, 18, 20, 22, 31, 47]
];
$results = [];
foreach ($playerBalls as $person => $balls) {
if ( !isset($results[$person]) ) {
$results[$person] = 0;
}
foreach( $balls as $ball) {
if (in_array($ball, $pickedBalls)) {
$results[$person]++;
}
}
}
foreach ( $results as $name => $balls ) {
if ( $balls >= 3) {
echo $name . ' is a winner';
}
}
RESULTS
Jack is a winner

PHP arrays neighbors

I need a little help. I don't want code or the solution, only a guide of HOW do it.
If I have a array:
$array = [[9, 2, 2, 2, 3, 5], [9, 8, 3, 2, 4, 5], [9, 7, 2, 2, 4, 3], [9, 9, 2, 4, 4, 3], [9, 2, 3, 4, 3, 5]];
And I have to return
$return = [[0, 1, 1, 1, 0, 0], [0, 0, 0, 1, 0, 0], [0, 0, 1, 1, 0, 1], [0, 0, 1, 0, 0, 1], [0, 1, 0, 0, 1, 0 ]];
The logic is:
If index has a neighbors less than him, is 0, otherwise is 1.
Thanks everything!
Make two for loops, one inside the other.
The first for loop will loop over $array and give you an index, let's call it $i.
The second for loop will iterate over the elements of each of the arrays in side $array, looping over $array[$i].

How to limit an array and split it into more arrays? PHP

I would like to do this...
I got this array with 50 elements
$data = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50);
and I would like to add the first 20 elements into another array, 20 should be the limit, if it is greater than 20 add the next 20 into another array and do this operation to only get array with 20 elements
I tried with this
$number = count($data);
$pieces = array_chunk($data, ceil($number / 2));
And I get only two sub-arrays from that, I'm lost, I need some ideas about how to achieve this, thanks.
That's because you're only trying to get 2 subarrays, since you're taking the total number and dividing it by 2. You're on the right track, you just need to specify the number of elements you want in each subarray:
$pieces = array_chunk($data, 20);

Which is better performance vise, shuffle and then array_slice, or array_rand?

So lets take this code:
shuffle($pushed_products_items);
$pushed_products_items = array_slice($pushed_products_items, 0, 7);
Versus this:
$pushed_products_items = array_rand($pushed_products_items, 7);
Which one would be better performance vise?
It would seem logical that the array_rand would be better, but if one thing I learned in my short career, not always things are the way it seems, and I am not really fond of testing this in production :)
Well I wrote the following tests:
<?php
$start = microtime(true);
$arr = array(12, 554, 54, 68, 54, 564, 45, 545, 87, 878, 5454, 545, 55, 9898, 98, 87, 21, 21, 54, 54, 87, 98, 54, 54, 99,);
for($i = 0; $i < 1000000; $i++)
{
$tmp = array_rand($arr, 7);
}
echo microtime(true) - $start;
And this:
<?php
$start = microtime(true);
$arr = array(12, 554, 54, 68, 54, 564, 45, 545, 87, 878, 5454, 545, 55, 9898, 98, 87, 21, 21, 54, 54, 87, 98, 54, 54, 99,);
for($i = 0; $i < 1000000; $i++)
{
shuffle($arr);
$tmp = array_slice($arr, 0, 7);
}
echo microtime(true) - $start;
So shuffle + array_slice on avg takes 5.2s for 1mil iterations, and array_rand takes avg 3.2 seconds.
So, array_rand is indeed better.

Modulus PHP Problem

I have a problem, I am trying to calculate what the lowest prime is of a number but I do not understand the result that PHP is giving me.
If I have this number
$number = 600851475143;
Then I modulus it:
$primes = array( 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97);
foreach($primes as $key=>$value) {
if($number % $value == 0 ) {echo $value; break; }
}
Why is it that $value = 3? If $value = 3, that means that 600851475143 / 3 should be an integer, but its not. So I do not understand why that if() evaluates to true?
See this bug listing here
% does not not work for numbers over 2^31 (32-bit) or 2^63 (64-bit). Use BCMOD instead.
Using PHP 5.2.8, it fails as described in the question. It seems like the modulo operator doesn't work on big integers. You should consider using bc_mod (modulus with arbitrary precision):
<?php
$number = 600851475143;
$primes = array(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97);
foreach($primes as $key=>$value)
{
if($number % $value == 0 ) {echo $value."<br/>"; }
if(bcmod($number, $value) == 0) {echo "bcmod ".$value."<br/>"; }
}
?>
The above code prints:
3
29
bcmod 71
I might be misunderstanding it, but modulo gives you the rest of a division, so e.g. 600851475143 / 3 is 200283825047 rest 2 and this is what it gives you back.

Categories