php smarty section 循环自定义函数返回的数组

php smarty section 循环自定义函数返回的数组,第1张

数组名字为$arclist
<table>
{section loop=$arclist name=row}
<tr>
<td>$arclist[row]row;</td>
<td>$arclist[row]typeid;</td>
</tr>
{/section}
</table>

数组名其实是个指针,你可以返回一个指针。如:
int fun(int a,int b)这样就可以。
还有一种替代方式是在输入参数里加入一个数组,在函数里对数组进行修改,函数运行后数组内容也改变了。

//一、由函数参数带回固定的二维数组
void func(int ppout[][2])
{
ppout[0][0] = 1;
ppout[0][1] = 2;
ppout[1][0] = 3;
ppout[1][1] = 4;
}
int _tmain(int argc, _TCHAR argv[])
{
int pp[2][2];
func(pp);
cout << pp[0][0] << pp[0][1] << endl;
cout << pp[1][0] << pp[1][1] << endl;
system("pause"); //让窗口暂停,便于看结果
return 0;
}
//二、由函数参数带回动态二维数组
void func(int pp, int rows, int columns)
{
int i, j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < columns; j++)
{
pp[i][j] = (i + 1) 10 + j + 1;
}
}
}
int _tmain(int argc, _TCHAR argv[])
{
int rows = 3, columns = 2;
int pp;
int i, j;
//申请内存
pp = new int[rows];

for (j = 0; j < rows; j++)
pp[j] = new int[columns];
func(pp, rows, columns);
for (i = 0; i < rows; i++)
{
for (j = 0; j < columns; j++)
{
cout << pp[i][j] << " ";
}
cout << endl;
}
//释放内存
for (j = 0; j < rows; j++)
delete[] pp[j];
delete[] pp;
system("pause"); //让窗口暂停,便于看结果
return 0;
}
//如果非要从函数的返回值中带回二维数组,这是最好的方式
//三、由函数带回动态二维数组
int func(int rows, int columns)
{
int i, j;
int pp;
//申请内存
pp = new int[rows];
for (j = 0; j < rows; j++)
pp[j] = new int[columns];
for (i = 0; i < rows; i++)
{
for (j = 0; j < columns; j++)
{
pp[i][j] = (i + 1) 10 + j + 1;
}
}
return pp;
}
int _tmain(int argc, _TCHAR argv[])
{
int rows = 3, columns = 2;
int pp;
int i, j;
pp = func(rows, columns);
for (i = 0; i < rows; i++)
{
for (j = 0; j < columns; j++)
{
cout << pp[i][j] << " ";
}
cout << endl;
}
//释放内存
for (j = 0; j < rows; j++)
delete[] pp[j];
delete[] pp;
system("pause"); //让窗口暂停,便于看结果
return 0;
}
//四、由函数带回二维数组
//这种方式有问题,程序中已经说明,不要采取此方式
const int rows = 3, columns = 2;
int (func())[columns]
{
int i, j;

static int pp[rows][columns]; //如果不是静态的,函数退出后其内存就变得无效。
for (i = 0; i < rows; i++)
{
for (j = 0; j < columns; j++)
{
pp[i][j] = (i + 1) 10 + j + 1;
}
}
return pp;
}
int _tmain(int argc, _TCHAR argv[])
{

int (pp)[columns];
int i, j;
//pp = new (int[columns])[rows];
pp = func();
for (i = 0; i < rows; i++)
{
for (j = 0; j < columns; j++)
{
cout << pp[i][j] << " ";
}
cout << endl;
}
system("pause"); //让窗口暂停,便于看结果
return 0;
}

这个容易,你先在里面定义一个数组,
然后将数组的值赋给函数就行了
如:
function
aa()
dim
bb(10)'
定义一个数组
bb(0)=0'给数组赋值
bb(1)=1
bb(2)=2

aa=bb'把数组返回给函数
end
function
不知是不是要这种效果

1、我们首先定义一个二级指针和一个行列变量[int array,row,column;]。

2、然后我们编写进入行和列的语句,代码如图所示。

3、接下来我们可以用一维数组打开一维一维数组。

4、接下来,我们使用[array [i] =(int )malloc(sizeof(int) column);]来为数组再次生成包含该数组的新数组。

5、然后我们可以为它赋值并输出[代码如图所示]。

6、运行程序后我们可以看到这种效果。

方法一:设置全局变量,这样在形参中改变全局变量,在实参中也会有效。所以可以在形参中改变多个全局变量的值,那么在实参中就相当于返回多个值。
方法二:如果用数组名作为形参,那么改变数组内容,比如排序,或者进行加减运算,回到实参时依然是有效的。这样也会返回一组值。
方法三:可以利用指针变量,这个原理和方法二是一样的,因为数组名本身就是数组首元素的地址。就不多说了。 用结构体封装不同类型数据,或者用数组表示同类型数据,然后返回结构体指针或者数组名,本质都是用指针来返回多个参数
方法四:如果学过C++,可以引用参数


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13395410.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-28
下一篇 2023-07-28

发表评论

登录后才能评论

评论列表(0条)

保存