How to modify key in php array

藏色散人
Release: 2023-03-03 16:18:02
Original
6298 people have browsed it

How to modify the key in the php array: 1. Modify the key through the "array_combine(array_keys($datas),$data)" method; 2. Use the foreach loop to process the key value, and the statement is "foreach ($data as $v=>$k){}".

How to modify key in php array

Recommended: "PHP Video Tutorial"

##php Processing array key value changes

$data =array(
             'classname' =>' 我们不一样',
             'id' =>    ' 6666',
          );
          //更改 id key值为cate_id
 
        $datas =array(
           'classname' =>$data['classname'],
            'cate_id'  =>$data['id'],
        );
       $arr3 =array_combine(array_keys($datas),$data);
        echo &#39;<pre class="brush:php;toolbar:false">&#39;;
        var_dump($arr3);
        echo &#39;
';
Copy after login

After printing, it is

array(2) {
  ["classname"]=>
  string(16) " 我们不一样"
  ["cate_id"]=>
  string(5) " 6666"
}
Copy after login

foreach loop processing

$data =array(
            &#39;classname&#39; =>&#39; 我们不一样&#39;,
            &#39;id&#39; =>    &#39; 6666&#39;,
        );
        //更改 id key值为cate_id
         foreach ($data as $v=>$k){
             $data[&#39;cate_id&#39;] =$data[&#39;id&#39;];   //key字段赋值
         }
          unset($data[&#39;id&#39;]);    //销毁原字段id key
          dump($data);die;
Copy after login

The above is the detailed content of How to modify key in php array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
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!