查看: 722|回复: 8
|
PHP - Dynamic array 的问题
[复制链接]
|
|
我可以拿到 $count 的 value
我要 if($count==3) 等于如下的code (我引用的是static的算法, 如何写dynamic 的呢?)
$count=3 就会是3x3的 matrix
$count=4 就会是4x4的 matrix
$count=5 就会是5x5的 matrix
$count=6 就会是6x6的 matrix
但我想 if $count 等于其他 4,5,6 或 7 的时候
我的$sum (和里面的element)跟着改变
===================================
例如:
if $count 等于 2, 我就会有 $sum1, $sum2
就会像如下
$sum1 = 1 + $invert[0];
$sum2 = txtlocation[0] + 1;
===================================
例如:
if $count 等于 4, 我就会有 $sum1, $sum2, $sum3 及 $sum4
就会像如下
$sum1 = 1 + $invert[0] + $invert[1] + $invert[2];
$sum2 = txtlocation[0] + 1 + $invert[3] + $invert[4];
$sum3 = txtlocation[1] + txtlocation[2] + 1 + $invert[5];
$sum4 = txtlocation[3] + txtlocation[4] + txtlocation[5] + 1;
====================================
例如:
if $count 等于 5, 我就会有5个$sum
$sum1, $sum2, $sum3,$sum4 及 $sum5
就会像如下
$sum1 = 1 + $invert[0] + $invert[1] + $invert[2] + $invert[3];
$sum2 = txtlocation[0] + 1 + $invert[4] + $invert[5] + $invert[6];
$sum3 = txtlocation[1] + txtlocation[4] + 1 + $invert[7] + $invert[8];
$sum4 = txtlocation[2] + txtlocation[5] + txtlocation[7] + 1 + $invert[9];
$sum5 = txtlocation[3] + txtlocation[6] + txtlocation[8] + txtlocation[9] + 1;
====================================
例如:
if $count 等于 6, 我就会有6个$sum
$sum1, $sum2, $sum3, $sum4, $sum5 及 $sum6
就会像如下
$sum1 = 1 + $invert[0] + $invert[1] + $invert[2] + $invert[3] + $invert[4];
$sum2 = txtlocation[0] + 1 + $invert[5] + $invert[6] + $invert[7] + $invert[8];
$sum3 = txtlocation[1] + txtlocation[5] + 1 + $invert[9] + $invert[10] + $invert[11];
$sum4 = txtlocation[2] + txtlocation[6] + txtlocation[9] + 1 + $invert[12] + $invert[13];
$sum5 = txtlocation[3] + txtlocation[7] + txtlocation[10] + txtlocation[12] + 1 + $invert[14];
$sum6 = txtlocation[4] + txtlocation[8] + txtlocation[11] + txtlocation[13] + txtlocation[14] + 1;
====================================
以下是我的if $count=3 的 code:
if (isset($_POST['btnSubmit'])) {
$txtlocation = $_POST['txtLocation']; //is an array get from <input type='text' name='txtLocation[]'/>
$count = count($txtlocation); //get input
//get input value(upper triangle) and invert value(lower triangle)
for($num=0;$num<$count;$num++){
$invert[] = 1/$txtlocation[$num];
}
if($count==3){
//this is my 3x3 matrix
// sum1 normalization
$sum1 = 1 + $invert[0] + $invert[1];
$normalize11 = 1/$sum1;
$normalize21 = $invert[0]/$sum1;
$normalize31 = $invert[1]/$sum1;
// sum2 normalization
$sum2 = $txtlocation[0] + 1 + $invert[2];
$normalize12 = $txtlocation[0]/$sum2;
$normalize22 = 1/$sum2;
$normalize32 = $invert[2]/$sum2;
// sum3 normalization
$sum3 = $txtlocation[1] + $txtlocation[2] + 1;
$normalize13 = $txtlocation[1]/$sum3;
$normalize23 = $txtlocation[2]/$sum3;
$normalize33 = 1/$sum3;
//final result
$addition1 = ($normalize11 + $normalize12 + $normalize13) * (1/$count);
$addition2 = ($normalize21 + $normalize22 + $normalize23) * (1/$count);
$addition3 = ($normalize31 + $normalize32 + $normalize33) * (1/$count);
}
}

请帮帮忙!!
感激不尽!!!
[ 本帖最后由 kimokek 于 18-4-2007 03:26 PM 编辑 ] |
|
|
|
|
|
|
|
发表于 17-4-2007 03:42 PM
|
显示全部楼层
|
|
|
|
|
|
|

楼主 |
发表于 17-4-2007 06:17 PM
|
显示全部楼层
原帖由 vampcheah 于 17-4-2007 03:42 PM 发表
用COUNT($ARRAY_NAME)
因为我有用$n = count($location);
我已经可以拿到 $n 的 value 了。。
我要$sum的size 如何变
如何用for loop 之类的吗?
不太明白。。。可以帮帮忙解释吗?
不好意思 |
|
|
|
|
|
|
|
发表于 18-4-2007 02:51 AM
|
显示全部楼层
还抓不到你的数学方程序的 pattern. 可以说说看如果 $n = 5 和 6 要怎么算吗? |
|
|
|
|
|
|
|
发表于 18-4-2007 10:05 AM
|
显示全部楼层
其實還是不太明白你要的是那個數學方程式怎麼做,還是那個怎麼令那個SUM動態加1、2、3...
如果是後者,那你或許可以考慮用PHP的Variable Variable功能:
$sumName = "SUM" . count( $array );
$$sumName = .....
若你的count()是1,$$sumName就會是$SUM1;
若你的count()是2,$$sumName就會是$SUM2;
若你的count()是3,$$sumName就會是$SUM3;
以此類推。 |
|
|
|
|
|
|
|
发表于 18-4-2007 10:18 AM
|
显示全部楼层
如果你的問題是在矩陣(array)的話,那以下的或許會有一點幫助:
照以上的例子,你應該是用二維矩陣(2 Dimensional Array),而且一定是正比的,比如說2x2,3x3,4x4,5x5,應該不會有如2x3,2x6,4x8之類的矩陣。
那你可以這麼做:
for ( $i=0; $i<count($array); $i++ ) {
for ( $j=0; $j<count($array[$i]); $j++ ) {
array[$i][$j]
}
}
那個array[$i][$j]就是你的,當然這只是舉例罷了(用於正比或非正比二維矩陣),實際的程式還須詳加分析你的方程式。
[ 本帖最后由 itplanet 于 18-4-2007 10:26 AM 编辑 ] |
|
|
|
|
|
|
|

楼主 |
发表于 18-4-2007 03:31 PM
|
显示全部楼层
至 goatstudio :
我已经在第一楼更新我程序的 pattern, 我把 $n 改成 $count 了
帮忙看看~ 多谢多谢!
至itplanet :
请看看我在第一楼的更新。。希望你能明白
我里面的txtLocation 及 txtInvert 要如何随着改变?感激!
[ 本帖最后由 kimokek 于 18-4-2007 03:32 PM 编辑 ] |
|
|
|
|
|
|
|
发表于 18-4-2007 04:34 PM
|
显示全部楼层
<?php
$array = array();
$sum = array();
// Fill Array with values
$array[] = array( 1, 1, 2, 4, 7, 1 );
$array[] = array( 1, 1, 3, 5, 8, 2 );
$array[] = array( 2, 6, 1, 6, 9, 3 );
$array[] = array( 3, 7, 0, 1, 0, 4 );
$array[] = array( 4, 8, 1, 3, 1, 5 );
$array[] = array( 5, 9, 2, 4, 5, 1 );
// Calculate SUMs
for ( $i=0; $i<count($array); $i++ ) {
$sum[] = 0;
for ( $j=0; $j<count($array[$i]); $j++ ) {
// $j comes before $i because we want to add
// by (vertical) column, not (horizontal) row.
$sum[$i] += $array[$j][$i];
}
}
// Display Results
for ( $i=0; $i<count($sum); $i++ )
print "SUM" . $i . " = " . $sum[$i] . "<br>";
?>
[ 本帖最后由 itplanet 于 18-4-2007 04:50 PM 编辑 ] |
|
|
|
|
|
|
|

楼主 |
发表于 26-4-2007 02:07 AM
|
显示全部楼层
至itplanet :
多谢多谢帮忙!
哎。。我已经尽力了。。
最后还是有问题~
我改用别的方法了。。
多谢大家!!!! |
|
|
|
|
|
|
| |
本周最热论坛帖子
|