Show on last array - php

I have this code:
for($i=1;$i<=date("j");$i++) {
$DataGraphLinesAff .= ($i == 1 ? '['.$i.', '.$InfosMembre['imp_'.$i.''].'],
' : '['.$i.', '.$InfosMembre['imp_'.$i.''].'],
');
}
which displays:
[1, 0],
[2, 0],
[3, 0],
[4, 0],
[5, 0],
[6, 0],
[7, 0],
[8, 0],
[9, 0],
[10, 0],
[11, 0],
[12, 0],
[13, 34],
how change code to display last array without ,
How to display ['8', 0], ?

You can use an array:
for($i=1;$i<=date("j");$i++) {
$DataGraphLinesAff[] = '['.$i.', '.$InfosMembre['imp_'.$i.''].']';
}
$DataGraphLinesAff = implode(",\n", $DataGraphLinesAff);

for($i=1;$i<=date("j");$i++) {
$DataGraphLinesAff .= ($i == 1 ? '['.$i.', '.$InfosMembre['imp_'.$i.''].'],
' : '['.$i.', '.$InfosMembre['imp_'.$i.''].']'.(($i==date("j"))?'':',
'));
}

you can trim the last comma from your string:
rtrim($DataGraphLinesAff, ",")
http://php.net/rtrim

You can substr to remove last character from string,
for($i=1;$i<=date("j");$i++) {
$DataGraphLinesAff .= ($i == 1 ? '['.$i.', '.$InfosMembre['imp_'.$i.''].'],
' : '['.$i.', '.$InfosMembre['imp_'.$i.''].'],
');
}
$DataGraphLinesAff = substr(trim($DataGraphLinesAff), 0, -1);

Related

How can i coloring Rubix Cube By three-dimensional array in php

I want in a three-dimensional array, I just want to paint the cells of the array that are on the outside of the array, like the image below.
I want by function color() get an array (call by reference) change the outer Rubix of the array convert to 1 and interior Rubix convert to 0
i create function color() But it does not work Rubik's walls do not change
$matrix = [
[
[3, 5, 13, 56],
[0, 1, 165, 1],
[-8, 78, 5, 8],
[6, 5, 23, 45]
],
[
[1, 17, 5, 3],
[1, 5, 1, 65],
[6, 5, 5, -4],
[0, 4, 3, 90]
],
[
[9, 9, 8, 0],
[3, 5, 4, 8],
[0, 5, 3, 9],
[1, 4, 5, 7]
]
];
function color(&$matrix){
for ($i = 0; $i < count($matrix); ++$i) {
echo 'layer ' . ($i + 1) . ':' . PHP_EOL;
foreach ($matrix as $j) {
if($i == 1){
$y=0;
foreach ($j as $k) {
if($y == 0 )
echo $f = 1 . ' ';
elseif($y == 1)
echo $f = 0 . ' ';
elseif($y == 2)
echo $f = 1 . ' ';
$y++;
}
}else{
foreach ($j as $k) {
echo $k = 1 . ' ';
}
}
echo PHP_EOL;
}
}
}
how solving coloring Rubic Cube By three-dimensional array?????
<?php
function color(&$ls) {
foreach ($ls as $xs => $xl) {
foreach ($xl as $ys => $yl) {
foreach ($yl as $zs => $cell) {
$ls[$xs][$ys][$zs] = (int)($xs == 0 || $xs == count($ls) - 1 || $ys == 0 || $ys == count($xl) - 1 or $zs == 0 or $zs == count($yl) - 1);
}
}
}
}

PHP: Nested loop with only unique pairs

I need to create an array of only unique pairs so there are no duplicates.
The problem is that when just iterating over each other, for some reason last pair usually is duplicate.
So here's a quick example:
<?php
$teams = [
['id' => 1],
['id' => 2],
['id' => 3],
['id' => 4],
];
foreach ($teams as $team_a) {
foreach ($teams as $team_b) {
if ($team_a['id'] !== $team_b['id']) {
$pairs[] = [$team_a['id'], $team_b['id']];
}
}
}
?>
Returns something like this:
0: [1, 2]
1: [1, 3]
2: [1, 4]
3: [2, 1]
4: [2, 3]
5: [2, 4]
6: [3, 1]
7: [3, 2]
8: [3, 4]
...
So you can see that some pairs are the same, like [1, 2] and [2, 1]. And after the half iterations there are only repeats.
What would be the most efficient way to iterate like this and be sure that there are only unique pairs?
Thanks!
Obviously the duplicate always appears when key(team_a) > key(team_b)
<?php
$teams = [
['id' => 1],
['id' => 2],
['id' => 3],
['id' => 4],
];
foreach ($teams as $offset => $team_a) {
foreach (array_slice($teams, $offset+1) as $team_b) {
$pairs[] = [$team_a['id'], $team_b['id']];
}
}
You could to do this with recursion instead of two for each loops.
<?php
$teams = [1, 2, 3, 4, 5, 6, 7, 8, 9 , 10];
getMatchup($teams);
function getMatchUp($teams, $matches = [], $start = 0) {
// Check if the whole array has been checked
if ($start == count($teams)) {
return var_dump($matches);
}
// Check every option considering a certain start point
for ($x = $start; $x < count($teams); $x++) {
// As long the team is not the same, add to matches
if ($start !== $x) {
$matches[] = [$teams[$start] => $teams[$x]];
}
}
// First team has been matched up, start matching the second team and so on..
getMatchup($teams,$matches, $start + 1);
}
Running example:
http://sandbox.onlinephpfunctions.com/code/4277d966c4ceacdbe7024d14fb03fe05fd760471

Finding linking path between two points in array php

I would like to find possible options from an array that connects two points.
Eg.
$startPoint = 1;
$endPoint = 5;
$points = [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 5] [4, 3], [1, 4], [1, 3], [1, 5], [1, 6]];
function should return
$possibleOptions = [[1, 5], [[1, 4], [4, 5]], [[1, 6], [6, 5]], [[1, 2], [2, 3], [3, 4], [4, 5]]]
Would be nice to add a possible option limit eg. if maximum of 3 combinations are allowed, then the function would return
$possibleOptions = [[1, 5], [[1, 4], [4, 5], [[1, 6], [6, 5]]
as [[[1, 2], [2, 3], [3, 4], [4, 5]]] is a total of 4 combinations.
I have tried looping through the array and finding a direct match first. If no direct match is found i loop through points again and create a new array with all the points that have start point as first value. I then check if the new array has an element where the last value is the endpoint. However, i feel like it is repetitive code and could be optimized as I have to repeat the code for the the max amount of combinations. Just not sure how...
Here is an example of my code:
$results = [];
$startPoint = 1;
$endPoint = 3;
$points = [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [4, 3], [1, 4], [1, 3]];
foreach ($points as $point) {
if ($point[0] == $startPoint && $point[1] == $endPoint) {
$results[] = $point;
}
}
$newStartPoints = [];
foreach ($points as $point) {
if ($point[0] == $startPoint) {
$newStartPoints[] = $point;
}
}
foreach ($points as $point) {
foreach ($newStartPoints as $startPoint) {
if ($point[0] == $startPoint[1] && $point[1] == $endPoint) {
$results[] = [$startPoint, $point];
}
}
}
print_r($results);
Here is a solution:
$startPoint = 1;
$endPoint = 5;
$max_combinations = 3;
$points = [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [4, 3], [1, 4], [1, 3], [1, 5]];
$result = [];
for ($i = $startPoint; $i <= $endPoint; $i++) {
$sub_result = [];
if (in_array([$startPoint, $i], $points)) {
$sub_result[] = [$startPoint, $i];
for ($j = $i; $j <= $endPoint; $j++) {
if (in_array([$j, $j + 1], $points) && $j + 1 <= $endPoint) {
$sub_result[] = [$j, $j + 1];
}
}
}
if (count($sub_result) != 0 && (count($sub_result) <= $max_combinations || $max_combinations === -1)) {
$result[] = $sub_result;
}
}
sort($result);
print_r($result);
The first four variable declarations can be modified according to your needs.
Note that setting $max_combinations equal to -1 it will mean that it will match all paths rather than having a limit.
Live demo

Add value from one element to other in php

This is probably a duplicate, but I couldn't find the answer I needed, maybe my wording is wrong.
Anyway, I have a two-dimensional array with hundreds of values, what I need is to insert a value from second element to the first element.
Example;
I have four elements in an array:
[0] = 1, [1] = 9, [2] = 9, [3] =5
I need to put these into a single element, so that it would turn
into this: [0] = 1995.
I have a feeling there might be something I could do with foreach, if so, maybe someone can explain to me, in detail, how that would exactly work?
OR
Maybe there's a function I'm not aware of.
You can use the following, using implode:
$arr = [1, 9, 9, 5];
$val = implode($arr);
unset($arr);
$arr[0] = $val;
demo: https://ideone.com/VTb7vO
To use the solution using implode on the whole multidimensional array, you can use the following:
$arr = [[1, 9, 9, 5], [1, 9, 9, 6], [1, 9, 9, 7], [1, 9, 9, 8]];
foreach ($arr as $key => $value) {
$val = implode($value);
$arr[$key] = $val;
}
demo: https://ideone.com/X6vueO
another, much shorter solution could be the following:
$arr = [[1, 9, 9, 5], [1, 9, 9, 6], [1, 9, 9, 7], [1, 9, 9, 8]];
$arr = array_map('implode', $arr);
demo: https://ideone.com/0ju8he
To concatenate each inner array of numbers you can use implode on each of it.
$newArray = array_map('implode', $array);
If it is executed on the array [[1, 2, 3], [1, 3], [1, 1, 1]] it will create the array ['123', '13', '111']. demo
$arr = [2,3,4];
$var = implode($arr, '');
var_dump( $var );
Two dimensional:
$cars = array
(
array(22,18),
array(15,13),
array(5,2),
array(17,15)
);
foreach ($cars as $val) {
$var1 = implode($val, '');
var_dump( $var1 );
}
$var= "";
foreach($arr as $element) {
$var .= $element;
}

Regrouping values in a bi-dimensional array in php

How the good php developer I am not would solve this problem?
I spent quite some time figuring how to shorten a bi-dimensional array.
$expanded = [ [1, 1], [1, 4], [3, 5], [1, 3], [4, 1], [4, 2], [1, 2], [4, 7], [3, 5] ];
into :
$shortened = [ [1, 10], [4, 10], [3, 10];
What I want is, if the first value of two arrays is the same, merge them !
This is what got me the closest to what I want :
$expanded = [ [1, 1], [1, 4], [3, 5], [1, 3], [2, 1], [2, 2], [1, 2], [2, 7], [3, 5] ];
$len = count($expanded);
$shortened[0] = $expanded[0];
for ($i = 0; $i < $len; $i++) {
for ($j = 1; $j < $len-$i; $j++) {
if ($expanded[$i][0] == $expanded[$i+$j][0]) {
$shortened[$i][0] = $expanded[$i][0];
$shortened[][1] = $shortened[$i][1] + $expanded[$i+$j][1];
} else {
$shortened[$i+1] = $expanded[$i+$j];
}
}
}
$shortened = array_reduce(
$expanded,
function (array $carry, array $item) {
list($one, $two) = $item;
if (! isset($carry[$one])) {
$carry[$one] = [ $one, 0 ];
}
$carry[$one][1] += $two;
return $carry;
},
[]
);
I got this far in a few minutes:
$expanded = [ [1, 1], [1, 4], [3, 5], [1, 3], [4, 1], [4, 2], [1, 2], [4, 7], [3, 5] ];
foreach ($expanded as $part)
{
$temp[$part[0]] += $part[1];
}
foreach ($temp as $key => $value)
{
$shortened[] = [$key, $value];
}
echo '<pre>';
print_r($shortened);
echo '</pre>';
And this is the version I really used, because my PHP < 5.4:
$expanded = array(array(1, 1),
array(1, 4),
array(3, 5),
array(1, 3),
array(4, 1),
array(4, 2),
array(1, 2),
array(4, 7),
array(3, 5));
foreach ($expanded as $part)
{
$temp[$part[0]] += $part[1];
}
foreach ($temp as $key => $value)
{
$shortened[] = array($key, $value);
}
echo '<pre>';
print_r($shortened);
echo '</pre>';
The shorted version would look like this:
foreach ($expanded as $p) $t[$p[0]] += $p[1];
foreach ($t as $k => $v) $shortened[] = [$k,$v];

Categories