如何在php中删除多维数组的索引

我有这个多维数组。


Array

(

    [0] => Array

       (

          [firstName] => Hadi

          [lastName] => Pratama

          [age] => 22

       )


    [1] => Array

       (

          [firstName] => Jefri

          [lastName] => Ronaldo

          [age] => 21

       )


    [2] => Array

       (

          [firstName] => Rizky

          [lastName] => Aulia

          [age] => 20

       )

)

我想像这样将它们放入索引数组中。


Array = ({"firstName":"Hadi","lastName":"Pratama","age":"22"},{"firstName":"Jefri","lastName":"Ronaldo","age":"21"},

{"firstName":"Rizky","lastName":"Aulia","age":"20"});

请帮帮我。


动漫人物
浏览 231回答 3
3回答

函数式编程

不太清楚你的问题是什么(请通过编辑你的帖子来澄清)。您的输出看起来不像一个 PHP 数组。更像 JSON。当您说删除索引时,这可能是因为有非连续的 0 计数索引吗?见下文:<?php$people =[&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; 'name'=>'fred',&nbsp; &nbsp; &nbsp; &nbsp; 'family'=>'flintstones'&nbsp; &nbsp; ],&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; 'name'=>'barney',&nbsp; &nbsp; &nbsp; &nbsp; 'family' => 'rubble'&nbsp; &nbsp; ],&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; 'name'=> 'wilma',&nbsp; &nbsp; &nbsp; &nbsp; 'family' => 'flintstones'&nbsp; &nbsp; ]];var_dump(json_encode($people));unset($people[1]);var_dump(json_encode($people));var_dump(json_encode(array_values($people)));输出:string(116) "[{"name":"fred","family":"flintstones"},{"name":"barney","family":"rubble"},{"name":"wilma","family":"flintstones"}]"string(88) "{"0":{"name":"fred","family":"flintstones"},"2":{"name":"wilma","family":"flintstones"}}"string(80) "[{"name":"fred","family":"flintstones"},{"name":"wilma","family":"flintstones"}]"您可以在编码为 JSON 之前使用 array_values 重新索引数组以“删除”这些索引,并转换为对象列表。

慕后森

json_encode()&nbsp;:返回值的 JSON 表示echo&nbsp;json_encode($a);工作示例:-&nbsp;https://3v4l.org/EgOtE

手掌心

问题您的数组已经具有正确的尺寸,只需运行json_encode($array);它,您就会得到您正在寻找的结果。如果你想使用关联数组,你必须更有创意:$result = [];foreach($array as $index) {&nbsp; &nbsp;$result[$index['firstName']] = $index;}var_dump($result);结果:Array(&nbsp; &nbsp; [Hadi] => Array&nbsp; &nbsp; &nbsp; &nbsp;(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [firstName] => Hadi&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [lastName] => Pratama&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [age] => 22&nbsp; &nbsp; &nbsp; &nbsp;)&nbsp; &nbsp; [Jefri] => Array&nbsp; &nbsp; &nbsp; &nbsp;(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [firstName] => Jefri&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [lastName] => Ronaldo&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [age] => 21&nbsp; &nbsp; &nbsp; &nbsp;)&nbsp; &nbsp; [Rizky] => Array&nbsp; &nbsp; &nbsp; &nbsp;(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [firstName] => Rizky&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [lastName] => Aulia&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [age] => 20&nbsp; &nbsp; &nbsp; &nbsp;))
打开App,查看更多内容
随时随地看视频慕课网APP