2016-05-04 12 views
-6

Ich habe folgende Datenstruktur muss im Speicher für dynamische Berechnungen im laufenden Betrieb gehalten werden, wie erzeuge ich Matrix Datenstruktur, die diese Werte in PHP halten? (Ich nehme an, ich habe, ich bin jedoch für mehrere Nennwerte Array verwendet werden unsicher, wie sie wie diese unten in praktischen Beispielen verwenden)PHP - Datenstruktur zum Speichern von Matrix von Strings und numerischen Werten

enter image description here

Irgendwelche Ideen dazu? Im Grunde i für Build in Matrix Typ Suche Klasse wie im folgenden Link, in diesem Fall ist es ein Binary Tree.

http://www.sitepoint.com/data-structures-2/

+2

sonst jemand wissen wollen, was Wasserzeichen er entfernt? –

+2

Verwenden Sie einfach ein Array, keine Notwendigkeit für eine Tabelle mit diesen kleinen Daten – Ghost

+1

stellen Sie eine gute Frage, und ich würde: http://StackOverflow.com/Help/how-to-ask –

Antwort

0

fand ich die Antwort, die ich von unten Link wurde. [http://hawkee.com/snippet/10086/]

für jeden, der an dieser Erkenntnis interessiert ist, werde ich es hier posten.

Es gibt eine zweite Lösung für dieses auch mit Array innerhalb Array. Die ich unter diesem Beitrag

<?php 
class Matrix { 
    public $arr, $rows, $cols; 

    function Matrix($row, $col) { 
     if ($row > 0 && $col > 0) { 
      $arr  = array(); 
      $this->rows = $row; 
      $this->cols = $col; 

      for ($a=0; $a < $this->rows; $a++) { 
       array_push($arr,array()); 

       for ($b=0; $b < $this->cols; $b++) { 
        array_push($arr[$a],0); 
       } 
      } 
     } 
    } 

    function SetElem($x, $y, $val) { 
     if ($x > -1 && $x < $this->rows) { 
      if ($y > -1 && $y < $this->cols) { 
       $this->arr[$x][$y] = $val; 
      } 
     } 
    } 

    function GetElem($x, $y) { 
     if ($x > -1 && $x < $this->rows) { 
      if ($y > -1 && $y < $this->cols) { 
       return $this->arr[$x][$y]; 
      } 
     } 
    } 

    function Add($matrix) { 
     if ($this->rows == $matrix->rows && $this->cols == $matrix->cols) { 
      $rslt = new Matrix($this->rows,$this->cols); 

      for ($a=0; $a < $this->rows; $a++) { 
       for ($b=0; $b < $this->cols; $b++) { 
        $rslt->SetElem($a,$b,$this->GetElem($a,$b) + $matrix->GetElem($a,$b)); 
       } 
      } 

      return $rslt; 
     } 
    } 

    function Subtract($matrix) { 
     if ($this->rows == $matrix->rows && $this->cols == $matrix->cols) { 
      $rslt = new Matrix($this->rows,$this->cols); 

      for ($a=0; $a < $this->rows; $a++) { 
       for ($b=0; $b < $this->cols; $b++) { 
        $rslt->SetElem($a,$b,$this->GetElem($a,$b) - $matrix->GetElem($a,$b)); 
       } 
      } 

      return $rslt; 
     } 
    } 

    function Multiply($matrix) { 
     if ($this->cols == $matrix->rows) { 
      $rslt = new Matrix($this->rows, $matrix->cols); 

      for ($a=0; $a < $rslt->rows; $a++) { 
       for ($b=0; $b < $rslt->cols; $b++) { 
        $total = 0; 

        for ($c=0; $c < $matrix->rows; $c++) { 
         $total += $this->GetElem($a,$c) * $matrix->GetElem($c,$b); 
        } 

        $rslt->SetElem($a,$b,$total); 
       } 
      } 

      return $rslt; 
     } 
    } 

    function ScalarMultiply($num) { 
     $rslt = new Matrix($this->rows,$this->cols); 

     for ($a=0; $a < $rslt->rows; $a++) { 
      for ($b=0; $b < $rslt->cols; $b++) { 
       $rslt->SetElem($a,$b,$this->GetElem($a,$b) * $num); 
      } 
     } 

     return $rslt; 
    } 
} 
?> 

Verbrauch:

<?php include 'Matrix.php'; 
$mat = new Matrix(2,2); 

$mat->SetElem(0,0,1); 
$mat->SetElem(0,1,2); 
$mat->SetElem(1,0,3); 
$mat->SetElem(1,1,4); 

$mat2 = new Matrix(2,2); 

$mat2->SetElem(0,0,5); 
$mat2->SetElem(0,1,6); 
$mat2->SetElem(1,0,7); 
$mat2->SetElem(1,1,8); 

$MultiplyResult = $mat->Multiply($mat2); 
$ScalarResult = $mat->ScalarMultiply(2); 
$AddResult  = $mat->Add($mat2); 
$SubtractResult = $mat->Subtract($mat2); 

zweite Lösung war alles nicht so gut, aber immer noch die Arbeit machen.

$graha = array 
    (
    array("Sun",5,5,0,4 ...), 
    array("Moon",5,3,...), 
    ... 
); 

In dieser Sie haben den Index zu halten [1 = Sonne, 2 = Mond,] woanders, um herauszufinden, welches Element es passt, wenn Elemente aus der Matrix anfordert.

Verwandte Themen