search

Home  >  Q&A  >  body text

php - About changing the specified value of each small array in a two-digit array without using a loop

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_leeringa_lee2728 days ago834

reply all(6)I'll reply

  • PHPz

    PHPz2017-05-27 17:45:55

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

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-27 17:45:55

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

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-27 17:45:55

    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

    reply
    0
  • PHPz

    PHPz2017-05-27 17:45:55

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

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-27 17:45:55

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

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-27 17:45:55

    Convert to string + regular match and replace?

    reply
    0
  • Cancelreply