0

I have an array with the following structure:

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
                )

        )

)

I now need to execute a function on every value in the attribute array. So for example capitalize SNUGGLES and BUDDY.

This is my approach:

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

But I get this error:

strtoupper() expects parameter 1 to be string, array given in ...

2
  • 2
    You can't be testing with the same data as you've posted in your question. Since the there isn't any key called dropdown in the array you've posted, this $key1 == 'dropdown' would never evaluate as true and strtoupper() would never be executed. Commented Dec 22, 2016 at 13:53
  • @MagnusEriksson: My bad, just a copy-paste error. Commented Jan 9, 2017 at 15:06

4 Answers 4

1

You are using the wrong variable in your inner loop:

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

Judging by the code, you would want to use $value1 there:

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

Assuming that dropdown => attributes...

Sign up to request clarification or add additional context in comments.

Comments

1

Simple way to do this is..

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's:

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

Comments

0

//Code : To capitalize each value. You can use below :

$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);
        }
    }
}

Output :

   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
                )

        )

)

Comments

0

You can use array_map

You can do it like this:

Let the inputs be (same as yours):

$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"
    ]
  ]
]

Then you can manipulate through your array in a more simplest & shortest way like this:

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);

This will give you the output as according to your inputs:

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"
    ]
  ]
]

Your provided inputs have an array key named attributes which contains a array having 2 values of same key-value pair nicknames. In that case this will take the last one.

Hope this helps!

5 Comments

This is very nice, but modification is needed. Just 'attributtes' sub-array should be modified...
@sinisake - Yup, just attributes is needed to be modified. Kindly upvote! :D
"I now need to execute a function on every value in the attribute array. So for example capitalize SNUGGLES and BUDDY." - so, not every element, just one part of array. :)
I didn't get you, can you please post a new question for your problem as questions are not meant to be asked here!
I didn't posted question, just tried to explain what is wrong: OP doesn't need ALL VALUES of array uppercased, just some of them. :) So, maybe array_map isn't suitable for this.

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.