controleren of 2 values bestaan in een array
Ik probeer 4 unieke verbindingen in een array te krijgen maar denk dat d econtrole niet goed werkt.
Iemand een idee wat er fout gaat?
Code (php)
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
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
<?php
$_SESSION['antwoorden'] = array ();
function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
do {
do {
$source = rand (0, 3);
$target = rand (0, 3);
$verbinding = (array("source" => $source, "target" => $target));
} while (in_array_r($verbinding, $_SESSION['antwoorden']) === true);
// zet de verbindingen in de antwoorden array
array_push ($_SESSION['antwoorden'], $verbinding);
} while (count($_SESSION['antwoorden']) < 4);
?>
$_SESSION['antwoorden'] = array ();
function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
do {
do {
$source = rand (0, 3);
$target = rand (0, 3);
$verbinding = (array("source" => $source, "target" => $target));
} while (in_array_r($verbinding, $_SESSION['antwoorden']) === true);
// zet de verbindingen in de antwoorden array
array_push ($_SESSION['antwoorden'], $verbinding);
} while (count($_SESSION['antwoorden']) < 4);
?>
Gewijzigd op 27/05/2020 14:05:21 door Sietsko Bos
Tot nu toe is dit nog best een karig 'glazen bol' topic.
In plaats van $_SESSION['antwoorden'] gebruik $antwoorden = [];
Dan voor het resultaat :
Toevoeging op 27/05/2020 14:28:14:
Zo dus
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
$antwoorden = [];
do {
do {
$source = rand (0, 3);
$target = rand (0, 3);
$verbinding = ( array( "source" => $source, "target" => $target) );
} while ( in_array_r( $verbinding, $antwoorden ) === true );
// zet de verbindingen in de antwoorden array
array_push ( $antwoorden, $verbinding );
} while ( count( $antwoorden ) < 4 );
echo '<pre>' . print_r( $antwoorden , TRUE ) . '<pre>';
?>
$antwoorden = [];
do {
do {
$source = rand (0, 3);
$target = rand (0, 3);
$verbinding = ( array( "source" => $source, "target" => $target) );
} while ( in_array_r( $verbinding, $antwoorden ) === true );
// zet de verbindingen in de antwoorden array
array_push ( $antwoorden, $verbinding );
} while ( count( $antwoorden ) < 4 );
echo '<pre>' . print_r( $antwoorden , TRUE ) . '<pre>';
?>
Wat maakt het verschil met $antwoorden = []; en $_SESSION['antwoorden'] ????
Code (php)
1
2
3
2
3
Warning: Invalid argument supplied for foreach() in checkneedle.php on line 4 Warning: array_push() expects parameter 1 to be array, null given in checkneedle.php on line 26 Warning: count(): Parameter must be an array or an object that implements Countable in checkneedle.php on line 28 Warning: Invalid argument supplied for foreach() in checkneedle.php on line 4 Warning: array_push() expects parameter 1 to be array, null given in checkneedle.php on line 26 Warning: count(): Parameter must be an array or an object that implements Countable in checkneedle.php on line 28 Warning: Invalid argument supplied for foreach() in checkneedle.php on line 4 Warning: array_push() expects parameter 1 to be array, null given in checkneedle.php on line 26 Warning: count(): Parameter must be an array or an object that implements Countable in checkneedle.php on line 28 Warning: Invalid argument supplied for foreach() in checkneedle.php on line 4 Warning: array_push() expects parameter 1 to be array, null given in checkneedle.php on line 26 Warning: count(): Parameter must be an array or an object that implements Countable in checkneedle.php on line 28 Warning: Invalid argument supplied for foreach() in checkneedle.php on line 4 Warning: array_push() expects parameter 1 to be ar.....
Pagina na pagina
Gewijzigd op 27/05/2020 14:46:26 door Adoptive Solution
Nog steeds zijn de 4 verbindingen niet uniek.
Code (php)
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
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
Array
(
[0] => Array
(
[source] => 3
[target] => 0
)
[1] => Array
(
[source] => 0
[target] => 2
)
[2] => Array
(
[source] => 1
[target] => 1
)
[3] => Array
(
[source] => 3
[target] => 1
)
)
(
[0] => Array
(
[source] => 3
[target] => 0
)
[1] => Array
(
[source] => 0
[target] => 2
)
[2] => Array
(
[source] => 1
[target] => 1
)
[3] => Array
(
[source] => 3
[target] => 1
)
)
2x een source 3 en target 1
Gewijzigd op 27/05/2020 15:03:33 door Sietsko Bos
Thanks Thomas, weer wat geleerd dat er meerdere wegen naar een goed script leiden :)