为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
摇曳的蔷薇 2022-10-14 10:53:23
我有一个这样的数组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']        ];
查看完整描述

3 回答

?
不负相思意

TA贡献1777条经验 获得超10个赞

使用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 array

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

    $array[$key] = [

        'id' => 7,

        'type' => 1,

        'place' => 2,

        'description' => 'This is item place 2',

        'image' => 'this_is_the_image.png',

    ];

}


print_r($array);

查看演示


查看完整回答
反对 回复 2022-10-14
?
呼啦一阵风

TA贡献1802条经验 获得超6个赞

尝试 :


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

    if(array_search($count,array_column("place",$array_from_db)) continue;

    $places_array = array(

        "id" => "3",

        "type" => "default",

        "place" => $count,

        );

}

这将跳过:


Array

(

    [id] => 3

    [type] => default

    [place] => 2

)

=========== 编辑 ==============


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

{

    $array[$arr['place']] = [

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

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

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

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

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

    ];

}

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

    if(!isset($array[$count])){

        $array[$count] = array(

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

            "type" => "default",

            "place" => $count

        );

    }

}  

ksort($array);


查看完整回答
反对 回复 2022-10-14
?
慕姐4208626

TA贡献1852条经验 获得超7个赞

找到答案了,谢谢大家的帮助。


这是诀窍


首先我们需要像这样确定主数组


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

        $array[$count] = array(

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

            "type" => "default",

            "place" => $count

        );

然后我们需要找到哪个地方不可用并显示匹配项。


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

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

    $key+=1;

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

        $array[$arr['place']] = [

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

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

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

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

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

        ];

    }

}

诀窍在于


$key+=1;

因为数组中的默认键是0。希望这可以帮助别人


查看完整回答
反对 回复 2022-10-14
  • 3 回答
  • 0 关注
  • 131 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号