如何获得这个数组值? (PHP)

时间:2011-08-02 05:04:22

标签: php arrays

很抱歉,我实际上正在尝试调试此代码justpaste.it/fh8,因为我无法让它工作。所以我怀疑它与数组

有关

我有这个数组$ compare_colours [' red']。当我var_dump时,它会给出这个值:

  

int(255)int(252)int(255)int(255)int(255)int(164)int(116)int(194)int(255)int(100)int(0)int(0 )int(1)

现在我如何手动获取第一个或第二个值?所以第一个值是255,第二个值是252。

我尝试使用$ compare_colours [' red'] [0],$ compare_colours 'red',但只获得空值。

感谢。

我从这段代码中获得了$ compare_colours数组

$compare_colors = imagecolorsforindex($compare_pic, $compare_rgb);

这是来自$ compare_colours

的print_r
Array ( [red] => 255 [green] => 255 [blue] => 255 [alpha] => 127 ) 
Array ( [red] => 252 [green] => 252 [blue] => 252 [alpha] => 126 ) 
Array ( [red] => 255 [green] => 255 [blue] => 255 [alpha] => 127 ) 
Array ( [red] => 255 [green] => 255 [blue] => 255 [alpha] => 127 ) 
Array ( [red] => 255 [green] => 255 [blue] => 255 [alpha] => 127 ) 
Array ( [red] => 164 [green] => 218 [blue] => 148 [alpha] => 44 ) 
Array ( [red] => 116 [green] => 202 [blue] => 115 [alpha] => 21 ) 
Array ( [red] => 194 [green] => 230 [blue] => 182 [alpha] => 64 ) 
Array ( [red] => 255 [green] => 255 [blue] => 255 [alpha] => 127 ) 
Array ( [red] => 100 [green] => 100 [blue] => 100 [alpha] => 50 ) 
Array ( [red] => 0 [green] => 0 [blue] => 0 [alpha] => 0 ) 
Array ( [red] => 0 [green] => 0 [blue] => 0 [alpha] => 0 )

当我使用var_dump($ compare_colors);

array(4) { ["red"]=> int(255) ["green"]=> int(255) ["blue"]=> int(255) ["alpha"]=> int(127) }
array(4) { ["red"]=> int(252) ["green"]=> int(252) ["blue"]=> int(252) ["alpha"]=> int(126) } 
array(4) { ["red"]=> int(255) ["green"]=> int(255) ["blue"]=> int(255) ["alpha"]=> int(127) } 
array(4) { ["red"]=> int(255) ["green"]=> int(255) ["blue"]=> int(255) ["alpha"]=> int(127) } 
array(4) { ["red"]=> int(255) ["green"]=> int(255) ["blue"]=> int(255) ["alpha"]=> int(127) } 
array(4) { ["red"]=> int(164) ["green"]=> int(218) ["blue"]=> int(148) ["alpha"]=> int(44) } 
array(4) { ["red"]=> int(116) ["green"]=> int(202) ["blue"]=> int(115) ["alpha"]=> int(21) } 
array(4) { ["red"]=> int(194) ["green"]=> int(230) ["blue"]=> int(182) ["alpha"]=> int(64) } 
array(4) { ["red"]=> int(255) ["green"]=> int(255) ["blue"]=> int(255) ["alpha"]=> int(127) } 
array(4) { ["red"]=> int(100) ["green"]=> int(100) ["blue"]=> int(100) ["alpha"]=> int(50) } 
array(4) { ["red"]=> int(0) ["green"]=> int(0) ["blue"]=> int(0) ["alpha"]=> int(0) } 
array(4) { ["red"]=> int(0) ["green"]=> int(0) ["blue"]=> int(0) ["alpha"]=> int(0) } 

3 个答案:

答案 0 :(得分:0)

如果$ compare_colours是颜色数组,那么你应该反过来这样做:

 echo $compare_colours[0]['red'].' '.$compare_colours[1]['red'];

答案 1 :(得分:0)

尝试通过以下代码获得任何输出。

echo $compare_colours[0]['red'];echo $compare_colors['red'];

答案 2 :(得分:-2)

你可以参加一个foreach循环:

$index = 0;
foreach ($compare_colours['red'] as $k => $v) {
  if ($index == 0) {
    $first_value = $v;
  }
  if ($index == 1) {
    $second_value = $v;
    break;
  }
  $index = $index + 1;
}

也许有一个更聪明的事情,但这很快,因为你只循环2次。