5

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 this set of value.

Thank you in advance.

5 Answers 5

3

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));
Sign up to request clarification or add additional context in comments.

2 Comments

Please give me an example for use it. For example how can I assign the value to rgbArray[1][1][red]
Just quote 'red'. So you statement would be $rgbArray[1][1]['red'] = $value; It would read in English something like "The value of rgbArray with weight 1, height 1 and rgb red is value."
2

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]

Comments

2

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

Comments

1

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.

Comments

-1
$ThrdArray = array(
            array(
                    array("red","green","blue"),
                    array("cyan","pink","violet"),
                    array("brown","white","black"),
                )
            );
            echo "<br><br>";
            var_dump($ThrdArray);
            echo "<br><br>";
            for ($i=0;$i<count($ThrdArray);$i++){
               echo "<br><br><p><b> colors :-".$i+1 ."</p></b>";
               echo "<ul>";
            for ($j=0;$j<count($ThrdArray[$i]);$j++){
                echo "<li type='square'>". $j+1 ."<br>";
                for ($k=0;$k<count($ThrdArray[$i][$j]);$k++){
                    echo "<li type='disk'>".$ThrdArray[$i][$j][$k]."</li>";
                }
                echo "</li>";
            }
            echo "</ul>";
            }
            unset($i,$j,$k);

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
This a beginner friendly and you can understand from this pretty clearly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.