从PHP中的多维数组替换内部键

时间:2016-07-13 08:57:22

标签: php arrays

拥有以下数组:

array(4) {
  [0]=>
  array(2) {
    [0]=>
    string(3) "233"
    [1]=>
    string(37) "some data"
  }
  [1]=>
  array(2) {
    [0]=>
    string(3) "233"
    [1]=>
    string(68) "some other data"
  }
  [2]=>
  array(2) {
    [0]=>
    string(3) "144"
    [1]=>
    string(38) "some other data"
  }
  [3]=>
  array(2) {
    [0]=>
    string(3) "233"
    [1]=>
    string(42) "some other data"
  }
 }

我想用一些随机HEX颜色替换值233144(内部数组中的键0)。例如,具有相同键(233)的那些必须具有相同的HEX颜色(FFF000,例如在上面所需的解决方案中)。

这是我用来生成随机HEX颜色的函数:

function randHEXcolor() {
    return sprintf('%06X', mt_rand(0, 0xFFFFFF));
}

我想要的输出应该是:

 array(4) {
  [0]=>
  array(2) {
    [0]=>
    string(6) "FFF000"
    [1]=>
    string(37) "some data"
  }
  [1]=>
  array(2) {
    [0]=>
    string(6) "FFF000"
    [1]=>
    string(68) "some other data"
  }
  [2]=>
  array(2) {
    [0]=>
    string(6) "111333"
    [1]=>
    string(38) "some other data"
  }
  [3]=>
  array(2) {
    [0]=>
    string(6) "FFF000"
    [1]=>
    string(42) "some other data"
  }
 }

我怎么能达到这个目的?

提前致谢。

5 个答案:

答案 0 :(得分:0)

foreach ($array as &$item) {
    if (!isset($temp[$item[0]]) {
        $temp[$item[0]] = randHEXcolor();
    }
    $item[0] = $temp[$item[0]];
}

答案 1 :(得分:0)

如果您希望将所有值转换为相同的随机颜色,则必须保存这些颜色:

$colors_translation = array();

foreach ($array as &$item) {
  $color = $item[ 0 ];
  $translate = $colors_translation[ $color ];

  if (empty($translate)) {
    $colors_translations[ $color ] = $translate = randHEXcolor();
  }

  $item[ 0 ] = $translate;
}

答案 2 :(得分:0)

使用in_arrayisset函数的解决方案:

$keys = [];
foreach ($arr as &$v) {  // $arr is your initial array
    if (in_array($v[0], ['233', '144'])) {
        if (!isset($keys[$v[0]])) $keys[$v[0]] = sprintf('%06X', mt_rand(0, 0xFFFFFF));
        $v[0] = $keys[$v[0]];
    }
}

print_r($arr);

输出:

Array
(
    [0] => Array
        (
            [0] => 65A4BB
            [1] => some data
        )

    [1] => Array
        (
            [0] => 65A4BB
            [1] => some data
        )

    [2] => Array
        (
            [0] => DDB588
            [1] => some data
        )

    [3] => Array
        (
            [0] => 65A4BB
            [1] => some data
        )
)

答案 3 :(得分:0)

此代码将在遍历数组时创建颜色映射。如果您想要预定义的颜色翻译,请预先填充$colorMap

<?php

$array = array(
    0 => array(
        0 => "233",
        1 => "some data"
    ),
    1 => array(
        0 => "233",
        1 => "some data"
    ),
    2 => array(
        0 => "144",
        1 => "some data"
    ),
    3 => array(
        0 => "233",
        1 => "some data"
    ),
);

$colorMap = array();

foreach ($array as &$inner) {
    if (!array_key_exists($inner[0],$colorMap)) {
        $newColor = randHEXcolor();
        $colorMap[$inner[0]] = $newColor;
        $inner[0] = $newColor;
    } else {
        $inner[0] = $colorMap[$inner[0]];
    }
}

function randHEXcolor() {
    return sprintf('%06X', mt_rand(0, 0xFFFFFF));
}

print_r($array);
print_r($colorMap);
Array
(
    [0] => Array
        (
            [0] => F1519A
            [1] => some data
        )

    [1] => Array
        (
            [0] => F1519A
            [1] => some data
        )

    [2] => Array
        (
            [0] => 2F7D00
            [1] => some data
        )

    [3] => Array
        (
            [0] => F1519A
            [1] => some data
        )

)

Array
(
    [233] => F1519A
    [144] => 2F7D00
)

答案 4 :(得分:-1)

尝试:

<?php
$array = array(
    0 => array(
        0 => "233",
        1 => "some data"
    ),
    1 => array(
        0 => "233",
        1 => "some data"
    ),
    2 => array(
        0 => "144",
        1 => "some data"
    ),
    3 => array(
        0 => "233",
        1 => "some data"
    ),
);
function randHEXcolor() {
    return sprintf('%06X', mt_rand(0, 0xFFFFFF));
}
$firstHex = randHEXcolor();
$secondHex = randHEXcolor();

foreach($array as $arrayIndex => &$arrayValue){
    if($arrayValue[0] == "144"){
        $arrayValue[0] = $firstHex;
    }
    if($arrayValue[0] == "233"){
        $arrayValue[0] = $secondHex;
    }
}

输出:

array(4) {
  [0]=>
  array(2) {
    [0]=>
    string(6) "AB8248"
    [1]=>
    string(9) "some data"
  }
  [1]=>
  array(2) {
    [0]=>
    string(6) "AB8248"
    [1]=>
    string(9) "some data"
  }
  [2]=>
  array(2) {
    [0]=>
    string(6) "22AF8B"
    [1]=>
    string(9) "some data"
  }
  [3]=>
  &array(2) {
    [0]=>
    string(6) "AB8248"
    [1]=>
    string(9) "some data"
  }
}