php - About changing the specified value of each small array in a two-digit array without using a loop
ringa_lee
ringa_lee 2017-05-27 17:43:55
0
6
799

For example, I have a two-dimensional array:

$a = [
    '0' => [
        'a' => '11',
        'b' => '22',
        'c' => '33'
    ],
    '1' => [
        'a' => '44',
        'b' => '55',
        'c' => '66'
    ],
    ...
];

With such an array, I want to change the values ​​of all 'b's to '99' without using a loop. How can I achieve this?

After thinking for a long time, I had no idea.

ringa_lee
ringa_lee

ringa_lee

reply all(6)
PHPzhong

No, and it makes no sense.
No matter what, you have to loop in disguise to achieve traversal.

phpcn_u1582
$a=array_map($a,function($val){
    $val['b']=99;
    return $val;
})
淡淡烟草味

For another method, even if you don’t need a loop, then that method must also use a loop

SoChange the soup but not the medicine

PHPzhong

The order upstairs is messed up. It should be array_map(function,$arr);
In fact, what you said upstairs is correct. Built-in functions need to traverse the entire array. How should you use built-in functions to solve your problem?

$a=array_map(function($val){
    $val['b']='99';
    return $val;
},$a);
伊谢尔伦

Boring, not grasping the big picture, getting hung up on minutiae. So I’m bored too, haha

    $arr = [
        '0' => [
            'a' => '11',
            'b' => '22',
            'c' => '33'
        ],
        '1' => [
            'a' => '44',
            'b' => '55',
            'c' => '66'
        ]
    ];
    
    $arr   = json_encode($arr);
    
    $match = preg_replace('/"b":"(.+?)"/', '"b":"99"', $arr);
    
    var_dump(json_decode($match, true));
Peter_Zhu

Convert to string + regular match and replace?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!