2016-05-04 7 views
-6

私は以下のデータ構造を動的計算のためにメモリに保持する必要がありますが、PHPでこの値を保持する行列データ構造を作成するにはどうすればよいですか?PHP - 文字列と数値の行列を保持するデータ構造

enter image description here

その上の任意のアイデアは?基本的に私は、マトリクス状にビルドを探しています(私はしかし、私は下のこの1のような実用的な例でそれらを使用する方法がわからないよ多宗派の配列を使用する必要があります推測します)下のリンクのようなクラス、その場合はバイナリツリーです。

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

+2

他の誰かが彼が削除したウォーターマークを知りたいですか? –

+2

ちょうど配列を使用して、その小さなデータを持つテーブルの必要はありません – Ghost

+1

良い質問をして、私はだろう:http://stackoverflow.com/help/how-to-ask –

答えて

0

私は私がリンクの下から探していた答えを見つけました。 [http://hawkee.com/snippet/10086/]

この検索に興味のある方は、ここに投稿します。

配列内の配列を使用する2番目の解決策もあります。私はこの下に掲載しますどの、

<?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; 
    } 
} 
?> 

使用法:

<?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); 

第2の解決策は、すべての良いことにはなかったが、それでも作業を行います。あなたは行列の要素を要求するとき、それが一致する要素を見つけるためにどこかに[1 =日、2 =月、]インデックスを維持する必要があり、この中

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

関連する問題