PHP:根据键的值修改关联数组

我有一个这样的数组


Array

(

    [id] => 3

    [type] => default

    [place] => 1

)

Array

(

    [id] => 3

    [type] => default

    [place] => 2

)

Array

(

    [id] => 3

    [type] => default

    [place] => 3

)

这个数组是从这个 php 创建的


for($count=1;$count <= 3;$count++){

$places_array = array(

    "id" => "3",

    "type" => "default",

    "place" => $count,

);

}

现在,如果 php mysql 数据找到该位置,我想更改此数组的结果。例如我有这个数组。


Array

(

    [id] => 7

    [type] => 1

    [place] => 2

    [description] => This is item place 2

    [image] => this_is_the_image.png

)

如您所见,第二个数组位于“位置 2”。现在我希望结果是这样的


Array

(

    [id] => 3

    [type] => default

    [place] => 1

)

Array

(

    [id] => 7

    [type] => 1

    [place] => 2

    [description] => This is item place 2

    [image] => this_is_the_image.png

)

Array

(

    [id] => 3

    [type] => default

    [place] => 3

)

如何做到这一点?我已经完成了 array_search 函数,但没有运气。任何人请帮助我


=======================编辑完整代码======================== ======== 这是代码,我正在显示数据库中的数据并在while循环函数中调用它


for($count=1;$count <= $max_places;$count++){

    $array[] = array(

        "id" => $res['id'],

        "type" => "default",

        "place" => $count

    );

    while($arr = $stmts->fetch()){

        $key = array_search($arr['place'], array_column($array, 'place'));

        if($key && array_key_exists($key, $array)) {

            $array[$key] = [

                "id" => $arr['id'],

                "type" => $arr['type'],

                "place" => $arr['place'],

                "url" => $arr['url'],

                "image" => $arr['image']

            ];

        }

    }

}

==========================交换代码======================= =======


while($arr = $stmts->fetch()){

        $array[] = [

                "id" => $arr['id'],

                "type" => $arr['type'],

                "place" => $arr['place'],

                "url" => $arr['url'],

                "image" => $arr['image']

        ];


摇曳的蔷薇
浏览 133回答 3
3回答

不负相思意

使用array_column()witharray_search()获取需要修改的数组键。请参阅以下代码片段:<?php// Here is trick; get the key of the array using array_column$key = array_search('2', array_column($array, 'place'));// If any key found modify the arrayif ($key && array_key_exists($key, $array)) {&nbsp; &nbsp; $array[$key] = [&nbsp; &nbsp; &nbsp; &nbsp; 'id' => 7,&nbsp; &nbsp; &nbsp; &nbsp; 'type' => 1,&nbsp; &nbsp; &nbsp; &nbsp; 'place' => 2,&nbsp; &nbsp; &nbsp; &nbsp; 'description' => 'This is item place 2',&nbsp; &nbsp; &nbsp; &nbsp; 'image' => 'this_is_the_image.png',&nbsp; &nbsp; ];}print_r($array);查看演示

呼啦一阵风

尝试 :for($count=1;$count <= 3;$count++){&nbsp; &nbsp; if(array_search($count,array_column("place",$array_from_db)) continue;&nbsp; &nbsp; $places_array = array(&nbsp; &nbsp; &nbsp; &nbsp; "id" => "3",&nbsp; &nbsp; &nbsp; &nbsp; "type" => "default",&nbsp; &nbsp; &nbsp; &nbsp; "place" => $count,&nbsp; &nbsp; &nbsp; &nbsp; );}这将跳过:Array(&nbsp; &nbsp; [id] => 3&nbsp; &nbsp; [type] => default&nbsp; &nbsp; [place] => 2)=========== 编辑 ==============while($arr = $stmts->fetch()){&nbsp; &nbsp; $array[$arr['place']] = [&nbsp; &nbsp; &nbsp; &nbsp; "id" => $arr['id'],&nbsp; &nbsp; &nbsp; &nbsp; "type" => $arr['type'],&nbsp; &nbsp; &nbsp; &nbsp; "place" => $arr['place'],&nbsp; &nbsp; &nbsp; &nbsp; "url" => $arr['url'],&nbsp; &nbsp; &nbsp; &nbsp; "image" => $arr['image']&nbsp; &nbsp; ];}for($count=1;$count <= $max_places;$count++){&nbsp; &nbsp; if(!isset($array[$count])){&nbsp; &nbsp; &nbsp; &nbsp; $array[$count] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id" => $res['id'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "type" => "default",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "place" => $count&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; }}&nbsp;&nbsp;ksort($array);

慕姐4208626

找到答案了,谢谢大家的帮助。这是诀窍首先我们需要像这样确定主数组for($count=1;$count <= $max_places;$count++){&nbsp; &nbsp; &nbsp; &nbsp; $array[$count] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id" => $res['id'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "type" => "default",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "place" => $count&nbsp; &nbsp; &nbsp; &nbsp; );}&nbsp;然后我们需要找到哪个地方不可用并显示匹配项。while($arr = $stmts->fetch()){&nbsp; &nbsp; $key = array_search($arr['place'], array_column($array, 'place'));&nbsp; &nbsp; $key+=1;&nbsp; &nbsp; if($key && array_key_exists($key, $array)) {&nbsp; &nbsp; &nbsp; &nbsp; $array[$arr['place']] = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id" => $arr['id'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "type" => $res['type'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "place" => $arr['place'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "url" => $arr['url'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "image" => $arr['image']&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; }}诀窍在于$key+=1;因为数组中的默认键是0。希望这可以帮助别人
打开App,查看更多内容
随时随地看视频慕课网APP