0

I have an array with the following structure :

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

Now I need to execute a function on every value of animal, say make the values uppercase. This is the expected output :

array(3) {
  [0]=>
  array(2) {
    ["animal"]=>
    string(3) "DOG"
    ["color"]=>
    string(5) "black"
  }
  [1]=>
  array(2) {
    ["animal"]=>
    string(3) "CAT"
    ["color"]=>
    string(5) "white"
  }
  [2]=>
  array(2) {
    ["animal"]=>
    string(5) "MOUSE"
    ["color"]=>
    string(4) "grey"
  }
}

When I do

for (int i=0; i<=$array.size(); i++) {
    $array["animal"] = array_map('strtoupper', $array["animal"]);
}

I get this error:

<b>Parse error</b>:  syntax error, unexpected 'i' (T_STRING), expecting ';' in <b>[...][...]</b> on line <b>15</b><br />
3
  • You're for loop is not valid PHP syntax: PHP for loop - You should check out some PHP-basics. I'm guessing this is a copy/paste from some other language? Commented Dec 20, 2016 at 11:02
  • argument at index 1 in array_map must be an array Commented Dec 20, 2016 at 11:02
  • and PHP doesnt use size, it uses count($array) Commented Dec 20, 2016 at 11:02

3 Answers 3

3

You can achieve this using following ways:

<?php
$array = array(
    array("animal"=>"dog","color"=>"black"),
     array("animal"=>"cat","color"=>"white"),
     array("animal"=>"mouse","color"=>"grey")
);

foreach ($array as $key => $value) {

    foreach ($value as $key1 => $value1) {

        if($key1 == 'animal'){
            $keys = ucfirst($value1);

            $array[$key][$key1]=$keys;
        }
    }
}
echo "<pre>";print_r($array);

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

Comments

0

You can also use array_walk Function of PHP

array_walk

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

$array_map = array_walk($array, 'walk_array');

function walk_array(&$item, $key){

$item['animal'] = strtoupper($item['animal']);
}
echo '<pre>';
print_r($array);

ScreenShot For Array Walk Function

Comments

0

Solution

You could use a for() loop to iterate through you animals. You'll have to count the number of animals you got first with the count() function cast to a variable. Then, you'll be able to make uppercase on your animal index with the strtoupper() function. Also note that you can use the new PHP syntax for arrays like so [] which is a quicker way to do it.

Source-code

<?php

$animals = [
    [
        'animal' => 'dog',
        'color' => 'black'
    ],

    [
        'animal' => 'cat',
        'color' => 'white'
    ],

    [
        'animal' => 'mouse',
        'color' => 'grey'
    ]
];

for ($i = 0; $i < count($animals); $i++)
{
    $animals[$i]['animal'] = strtoupper($animals[$i]['animal']);
}

echo '<pre>';
print_r($animals);
echo '</pre>';

Result

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

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

    [2] => Array
        (
            [animal] => MOUSE
            [color] => grey
        )

)

Demo.

PHP : for loop.

PHP : count().

PHP : strtoupper().

PHP : arrays.

Comments

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.