How to use 3 dimensional array in PHP
I'm doing some image processing in php and normally I never use array in php before.
I have to keep the value of rgb value of hold image in 3 dimensional array.
For example, 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 th开发者_Go百科is set of value.
Thank you in advance.
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.
$rgpArray[$index] = array('red'=>array('weight'=>$weight, 'height'=>$height),
'green'=>array('weight'=>$weight, 'height'=>$height),
'blue'=>array('weight'=>$weight, 'height'=>$height));
Your example is a little confuse rgbArray[1][1][red], it looks like you want this:
$rgbArray = array(1 => array(1 => array('red' => 'value')));
echo $rgbArray[1][1]['red']; // prints 'value'
I recommend, as PMV said, to do next:
$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.
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
$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
$weight = $rgbArray['red'][0]
$height = $rgbArray['red'][1]
Define three-dimensional array
$threeDimArray = array(array(
array("reza", "daud", "ome"),
array("shuvam", "himel", "izaz"),
array("sayanta", "hasib", "toaha")));
Printing three-dimensional array
for ($i=0; $i < count($threeDimArray); $i++) {
for ($j=0; $j < count($threeDimArray[$i]); $j++) {
for ($k=0; $k < count($threeDimArray[$i][$j]); $k++) {
echo $threeDimArray[$i][$j][$k];
echo " ";
}
echo "<br>";
}
}
精彩评论