💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
``` <?php /** * 需求:将 $arr 中age大于第一个大于30的人增加一项一条信息,得到 hongwei2的信息 * $arr = [ ['id' => 1, 'username' => 'hongwei', 'age' => 36], ['id' => 2, 'username' => 'hongwei2', 'age' => 38], ['id' => 3, 'username' => 'hongwei3', 'age' => 20], ['id' => 4, 'username' => 'hongwei4', 'age' => 37] ]; */ $arr = [ ['id' => 1, 'username' => 'hongwei', 'age' => 36], ['id' => 3, 'username' => 'hongwei3', 'age' => 20], ['id' => 4, 'username' => 'hongwei4', 'age' => 37] ]; //step1:计算 数组 $arr 中 键值age 中大于 30 的第1个值处于第几位 $result = ''; //位置 foreach ($arr as $k => $v) { if ($v['age'] > 30) { $result = $k + 1; break; } } //step2:在数组中第1位再增加一个数组 $arr2 = [$arr[$result]]; $arr2[0]['id'] = 2; $arr2[0]['username'] = 'hongwei2'; $arr2[0]['age'] = 38; $newArr = array_merge($arr, $arr2); //step3:对 $arr 根据其中一个键值age进行排序,形成新的 $arr $age = []; foreach ($newArr as $v) { $age[] = $v["age"]; } array_multisort($age, SORT_ASC, $newArr); print_r($newArr); ```