如何在 PHP 中使用 3 维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4108057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:59:42  来源:igfitidea点击:

How to use 3 dimensional array in PHP

phparrays

提问by Guilgamos

I'm doing some image processing in php and normally I never use array in php before.

我在 php 中做一些图像处理,通常我以前从未在 php 中使用过数组。

I have to keep the value of rgb value of hold image in 3 dimensional array.

我必须将保持图像的 rgb 值保留在 3 维数组中。

For example, rgbArray[][][]

例如, rgbArray[][][]

the first []is represent th weight, the second[]use to keep height and the last one is use to keep either red,greed or blue. How can i create an array in php that can keep this set of value.

第一个[]代表体重,第二个[]用于保持高度,最后一个用于保持红色,贪婪或蓝色。我如何在 php 中创建一个可以保留这组值的数组。

Thank you in advance.

先感谢您。

回答by Parris Varney

I think you're looking for a two dimensional array:

我认为你正在寻找一个二维数组:

$rgbArray[$index] = array('weight'=>$weight, 'height'=>$height, 'rgb'=>$rgb);

But here is a 3 dimensional array that could make sense for what you're asking.

但这里有一个 3 维数组,可以满足您的要求。

$rgpArray[$index] = array('red'=>array('weight'=>$weight, 'height'=>$height),
                          'green'=>array('weight'=>$weight, 'height'=>$height),
                          'blue'=>array('weight'=>$weight, 'height'=>$height));

回答by quantme

Your example is a little confuse rgbArray[1][1][red], it looks like you want this:

你的例子有点混淆rgbArray[1][1][red],看起来你想要这个:

$rgbArray = array(1 => array(1 => array('red' => 'value')));
echo $rgbArray[1][1]['red']; // prints 'value'

I recommend, as PMVsaid, to do next:

正如PMV所说,我建议接下来做:

$rgbArray = array('weight' => 1, 'height' => 1, 'rgb' => 'red' );

or

或者

$rgbArray = array();
$rgbArray['weight'] = 1; // int value
$rgbArray['height'] = 1; // int value
$rgbArray['rgb'] = 'red'; // string value

If it's not what you want please be more specific in order to be helped.

如果这不是您想要的,请更具体以便获得帮助。

回答by Dmitrij Holkin

If yours array

如果你的数组

$rgbArray = array('red'=>array('weight'=>$weight, 'height'=>$height),
                  'green'=>array('weight'=>$weight, 'height'=>$height),
                  'blue'=>array('weight'=>$weight, 'height'=>$height));

Then you can assign the value to rgbArray like

然后你可以将值分配给 rgbArray 像

$weight = $rgbArray['red']['weight']
$height = $rgbArray['red']['height']

If yours array

如果你的数组

$rgbArray = array('red'=>array($weight, $height),
                  'green'=>array($weight, $height),
                  'blue'=>array($weight, $height));

Then you can assign the value to rgbArray like

然后你可以将值分配给 rgbArray 像

$weight = $rgbArray['red'][0]
$height = $rgbArray['red'][1]