九色国产,午夜在线视频,新黄色网址,九九色综合,天天做夜夜做久久做狠狠,天天躁夜夜躁狠狠躁2021a,久久不卡一区二区三区

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
PHP-Codeigniter:如何從指定索引轉(zhuǎn)換數(shù)組值?

我有這樣的數(shù)組

array(2) { [0]=> object(stdClass)#20       (4) {            ["id"]=> string(1) "1"            ["name"]=> string(6) "robert"            ["height"]=> string(3) "165"            ["weight"]=> string(2) "81" } [1]=> object(stdClass)#21       (4) {            ["id"]=> string(1) "2"            ["name"]=> string(4) "mike"            ["height"]=> string(3) "175"            ["weight"]=> string(2) "69" } }

因此,我想更改數(shù)組值.

例如,我想更改[[height]]和[[weight]]的所有值.我將身高和體重按如下數(shù)字分類:

高度

1 = 150 ………. 170

2 = 171 ………. 190

重量

1 = 50 ……….. 70

2 = 71 ……….. 80

array(2) { [0]=> object(stdClass)#20       (4) {            ["id"]=> string(1) "1"            ["name"]=> string(6) "robert"            ["height"]=> string(1) "1"            ["weight"]=> string(1) "2" } [1]=> object(stdClass)#21       (4) {            ["id"]=> string(1) "2"            ["name"]=> string(4) "mike"            ["height"]=> string(1) "2"            ["weight"]=> string(1) "1" } }

我的數(shù)組是動態(tài)的,因此值可以隨時更改.當(dāng)然,[“ name”]不會改變,因為我沒有給出分類.您能幫我解決這個問題嗎?

解決方法:

給定您的示例數(shù)組為:

//example people array$people = [      //robert      (object)[            "id" =>"1",            "name"=> "robert",            "height" => "165",             "weight" => "79",            ],      //mike      (object)[        "id" => "2",        "name"=> "mike",        "height"=> "175",         "weight" =>"69",        ]  ];

您可以在稍作比較之后遍歷數(shù)組并修改每個對象.請注意,我不會檢查所有可能的范圍以保持簡潔(應(yīng)該再檢查幾下)

//walk the array array_walk($people, function($person){  //test height and assign category  if($person->height <= 190 && $person->height > 170)    $person->height = "2";  else    $person->height = "1";  //test weight and assign category  if($person->weight <= 80 && $person->weight > 70)    $person->weight = "2";  else    $person->weight = "1";});

可以產(chǎn)生期望的結(jié)果,您可以實時檢查"1", "name"=> "robert", "height" => "165", "weight" => "81", ], //mike (object)[ "id" => "2", "name"=> "mike", "height"=> "175", "weight" =>"69", ] ]; //walk the array array_walk($people, function($person){ //test height and assign category if($person->height <= 190="" &&="" $person-="">height > 170) $person->height = "2"; else $person->height = "1"; //test weight and assign category if($person->weight <= 80="" &&="" $person-="">weight > 70) $person->weight = "2"; else $person->weight = "1";}); print_r($people);" rel="nofollow">here(運行CTRL ENTER)

來源:https://www.icode9.com/content-1-529451.html
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
PHP的SPL擴展庫(一)數(shù)據(jù)結(jié)構(gòu)
php中stdClass的用法詳解
nil Nil NULL NSNull 之間的區(qū)別
泛型 反射 類型 new String[0]
python數(shù)據(jù)分析基礎(chǔ)——numpy和matplotlib
PHP stdClass 應(yīng)用
更多類似文章 >>
生活服務(wù)
熱點新聞
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服