Evgenij Reznik
Evgenij Reznik

Reputation: 18594

Execute function on every value of a key

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 />

Upvotes: 0

Views: 101

Answers (3)

Amin NAIRI
Amin NAIRI

Reputation: 2504

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.

Upvotes: 0

Manish
Manish

Reputation: 247

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

Upvotes: 0

Sachin I
Sachin I

Reputation: 1508

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

?>

Upvotes: 3

Related Questions