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].
Related
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.
Okay, so I got this multidimensional array in my project with some different values in it. For example:
$myArray = array
(
array("Array1", 42, 57, 12, 27),
array("Array2", 44, 59, 14, 29),
array("Array3", 46, 1, 16, 31),
array("Array4", 47, 2, 17, 32),
array("Array5", 48, 3, 18, 33),
array("Array6", 51, 6, 21, 36),
array("Array7", 53, 8, 23, 38),
array("Array8", 55, 10, 25, 40),
array("Array9", 57, 12, 27, 42),
array("Array10", 59, 14, 29, 44),
);
I want to extract only a part of myArray, and "disable" the other part of the array. So I only want this output:
$myArray = array
(
array("Array1", 42, 57, 12, 27),
array("Array2", 44, 59, 14, 29),
array("Array3", 46, 1, 16, 31),
array("Array4", 47, 2, 17, 32),
array("Array5", 48, 3, 18, 33)
);
Is there any way of achieving this in php?
array_slice() could help you.
$myArray= array_splice($myArray, 0, 5);
print '<pre>';
print_r($myArray);
print '</pre>';
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
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);
I am in need of help on how to generate multiple JSON files from data from a text file. I have a JSON file "file1.json" with following structure:
{
"level": [
6, 6, 4, 4, 2, 2, 3, 3, 6, 6,
0, 2, 3, 3, 6, 6, 4, 4, 3, 3,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
]
}
I have a text file with similar data but not in json format and separated by a line:
8, 8, 8, 8, 8, 8, 8, 8,
0, 6, 6, 4, 4, 2, 2, 3,
2, 2, 3, 3, 6, 6, 4, 4,
0, 0, 0, 0, 0, 0, 0, 0,
6, 6, 4, 4, 2, 2, 3, 3,
0, 6, 6, 4, 4, 2, 2, 4,
0, 0, 0, 3, 3, 3, 0, 0,
0, 0, 0, 0, 4, 0, 0, 0,
0, 7, 7, 7, 7, 7, 7, 0,
0, 0, 1, 1, 1, 1, 1, 0,
0, 0, 1, 1, 1, 1, 1, 0,
0, 0, 1, 1, 1, 1, 1, 0,
I am looking for a solution to help extract the data and create a json file in the above format with file name incremental like file2.json, file3.json etc but have not been able to find a solution to this as I have over 600 of these file to create.
Based on online search, it appears PHP might do but I have no knowledge of PHP. Any help or pointing me to a possible solution would be appreciated.
Not tested with the foreach loop, but by setting the $content static.
<?
$path = "/path/to/files/";
$files = array_diff(scandir($path), array('..', '.'));
foreach($file in $files)
{
$content = file_get_contents($path . $file, true);
/*
$content = "8, 8, 8, 8, 8, 8, 8, 8,
0, 6, 6, 4, 4, 2, 2, 3,
2, 2, 3, 3, 6, 6, 4, 4,
0, 0, 0, 0, 0, 0, 0, 0,";
*/
$content = preg_replace('/[^\d,]/i', '', $content);
$_content = array_filter(explode(',', $content));
echo json_encode(array('level' => array_values($_content)));
}
?>