<?php

/* 
 * Przykładowy kod do książki: Algorytmy i struktury danych w języku PHP 7
 * 
 * Autor: Mizanur Rahman <mizanur.rahman@gmail.com>
 * 
 */


$array = [1,2,3,4,5];
$mixedArray = [];
$mixedArray[0] = 200;
$mixedArray['nazwa'] = "Tablica mieszana";
$mixedArray[1] = 10.65;
$mixedArray[2] = ['Jestem', 'kolejną', 'tablicą'];


$array = [10,20,30,40,50];
$array[] = 70; 
$array[] = 80;

$arraySize = count($array);
for($i = 0;$i<$arraySize;$i++) {
   echo "Pozycja ".$i." przechowuje wartość ".$array[$i]."\n";
}

$startMemory = memory_get_usage();

$array = [];
$array[10] = 100;
$array[21] = 200;
$array[29] = 300;
$array[500] = 1000;
$array[1001] = 10000;
$array[71] = 1971;

foreach($array as $index => $value) {
   echo "Pozycja ".$i." przechowuje wartość ".$array[$i]."\n";
}

echo memory_get_usage() - $startMemory, " bajtów\n";
 

$studentInfo = [];
$studentInfo['Nazwisko'] = "Andrzejewski";
$studentInfo['Wiek'] = 11;
$studentInfo['Klasa'] = 6;
$studentInfo['Numer'] = 71;
$studentInfo['Kontakt'] = "info@andrzejewski.pl";

foreach($studentInfo as $key => $value) {
   echo $key.": ".$value."\n";
}


$players = [];
$players[] = ["Nazwisko" => "Ronaldo", "Wiek" => 31, "Kraj" => "Portugalia", "Drużyna" => "Real Madrid"];
$players[] = ["Nazwisko" => "Messi", "Wiek" => 27, "Kraj" => "Argentyna", "Drużyna" => "Barcelona"];
$players[] = ["Nazwisko" => "Neymar", "Wiek" => 24, "Kraj" => "Brazylia", "Drużyna" => "Barcelona"];
$players[] = ["Nazwisko" => "Rooney", "Wiek" => 30, "Kraj" => "Anglia", "Drużyna" => "Man United"];

foreach($players as $index => $playerInfo) {
   echo "Informacje o zawodniku nr ".($index+1)."\n";
   foreach($playerInfo as $key => $value) {
      echo $key.": ".$value."\n";
   }
   echo "\n";
}


$startTime = microtime();
$startMemory = memory_get_usage();
$array = [];   // new SplFixedArray(10000);
//$array = range(1, 100000);
for($i = 0;$i<10000;$i++)
   $array[$i] = $i;
echo memory_get_usage() - $startMemory, ' bajtów';
echo "\n".microtime() - $startTime, ' mikrosekund';


$graph = [];
$nodes = ['A', 'B', 'C', 'D', 'E'];
foreach ($nodes as $xNode) {
   foreach ($nodes as $yNode) {
      $graph[$xNode][$yNode] = 0;  
   }
}

$graph["A"]["B"] = 1;
$graph["B"]["A"] = 1;
$graph["A"]["C"] = 1;
$graph["C"]["A"] = 1;
$graph["A"]["E"] = 1;
$graph["E"]["A"] = 1;
$graph["B"]["E"] = 1;
$graph["E"]["B"] = 1;
$graph["B"]["D"] = 1;
$graph["D"]["B"] = 1;


foreach ($nodes as $xNode) {
   foreach ($nodes as $yNode) {
      echo $graph[$xNode][$yNode] . "\t";
   }
   echo "\n";
}
