迭代多个数组并执行函数

时间:2016-12-22 13:48:46

标签: php arrays

我有一个具有以下结构的数组:

Array
(
    [0] => Array
        (
            [animal] => dog
            [color] => black
        )

    [1] => Array
        (
            [animal] => cat
            [color] => white
        )

    [2] => Array
        (
            [animal] => mouse
            [color] => grey
            [attributes] => Array
                (
                    [nickname] => snuggles
                    [nickname] => buddy
                )

        )

)

我现在需要对属性数组中的每个值执行一个函数。例如,大写SNUGGLESBUDDY

这是我的方法:

$array = array(
    array("animal"=>"dog","color"=>"black"),
     array("animal"=>"cat","color"=>"white"),
     array("animal"=>"mouse","color"=>"grey", "attributes" => array("nicknames" => "snuggles", "nicknames" => "buddy"))
);

foreach ( $array as $key => $value ) {
    foreach ( $value as $key1 => $value1 ) {
        if ($key1 == 'attributes') {
            foreach ( $value as $key2 => $value2 ) {
                $value2 =  strtoupper($value2);
                $array [$key] [$key1] [$key2]= $key2;
            }
        }
    }
}

但是我收到了这个错误:

  

strtoupper()期望参数1为字符串,数组在......

中给出

4 个答案:

答案 0 :(得分:1)

您在内循环中使用了错误的变量:

foreach ( $value as $key2 => $value2 ) {
          ^^^^^^ here
    ...
}

根据代码判断,您希望在那里使用$value1

foreach ( $value1 as $key2 => $value2 ) {
          ^^^^^^^ here
    ...
}

假设dropdown => attributes ...

答案 1 :(得分:1)

这样做的简单方法是......

foreach ( $array as $key => $value ) {
    /* check whether the index is available or not */
    if( isset( $value['attributes'] ) ) {
         foreach ( $value['attributes'] as $namekey => $names ) {
             /* here capitalise the value and save in same index */
             $array[$key]['attributes'][$namekey] = strtoupper($names);
         }
    }
}
print_r($array);

OUTPUT的:

Array ( [0] => Array ( [animal] => dog [color] => black )
        [1] => Array ( [animal] => cat [color] => white )
        [2] => Array ( [animal] => mouse [color] => grey [attributes] => Array ( [nicknames] => BUDDY ) ) )

答案 2 :(得分:0)

您可以使用array_map

你可以这样做:

让输入(与你的相同):

$array = array:3 [
  0 => array:2 [
    "animal" => "dog"
    "color" => "black"
  ]
  1 => array:2 [
    "animal" => "cat"
    "color" => "white"
  ]
  2 => array:3 [
    "animal" => "mouse"
    "color" => "grey"
    "attributes" => array:1 [
      "nicknames" => "snuggles"
      "nicknames" => "buddy"
    ]
  ]
]

然后你可以用更简单的方式操纵你的阵列。像这样最短的方式:

function toUpper($val) {
    if(is_array($val)) {
        return array_map('toUpper', $val);
    }
    return strtoupper($val);
}

$final_arr = array_map('toUpper', $array);
print_r($final_arr);

这将根据您的输入为您提供输出:

array:3 [
  0 => array:2 [
    "animal" => "DOG"
    "color" => "BLACK"
  ]
  1 => array:2 [
    "animal" => "CAT"
    "color" => "WHITE"
  ]
  2 => array:3 [
    "animal" => "MOUSE"
    "color" => "GREY"
    "attributes" => array:1 [
      "nicknames" => "BUDDY"
    ]
  ]
]
  

您提供的输入有一个名为attributes的数组键,其中包含一个具有2个相同键值对nicknames值的数组。在这种情况下,这将采用最后一个。

希望这有帮助!

答案 3 :(得分:0)

//代码:大写每个值。你可以在下面使用:

$array = array(
    array("animal"=>"dog","color"=>"black"),
     array("animal"=>"cat","color"=>"white"),
     array("animal"=>"mouse","color"=>"grey", "attributes" => array("nickname1" => "snuggles", "nickname2" => "buddy"))
);

foreach ( $array as $index => $value ) 
{
    foreach ( $value as $key1 => $value1 )
    {
        if(is_array($value1))
        {
            foreach ( $value1 as $key2 => $value2 )
            {
                if(is_array($value2))
                {
                    foreach ( $value2 as $key3 => $value3 )
                    {
                        $array[$index][$key1][$key2][$key3] = strtoupper($value3);
                    }
                }
                else
                {
                    $array[$index][$key1][$key2] = strtoupper($value2);
                }
            }
        }
        else
        {
            $array[$index][$key1] = strtoupper($value1);
        }
    }
}

输出:

   Array
(
    [0] => Array
        (
            [animal] => DOG
            [color] => BLACK
        )

    [1] => Array
        (
            [animal] => CAT
            [color] => WHITE
        )

    [2] => Array
        (
            [animal] => MOUSE
            [color] => GREY
            [attributes] => Array
                (
                    [nickname1] => SNUGGLES
                    [nickname2] => BUDDY
                )

        )

)