看板 PHP 關於我們 聯絡資訊
請問各位我有一個object如下 如果我想要找在info裡 "只" 取得UGG XL的資料再把資料後有含空白或特殊字元刪掉 除了我自己用foreach外要怎麼寫會比較好? 謝謝 code: foreach ($obj->info as $key) { if ($key->name === "shoes") echo substr( $key->value, strpos($key->value, UGG)); } $obj ======== object(stdClass)#258 (2) { ["store"]=> string(1) "0" ["info"]=> array(4) { [0]=> object(stdClass)#259 (2) { ["name"]=> string(11) "shirt" ["value"]=> string(16) "XL" } [1]=> object(stdClass)#260 (2) { ["name"]=> string(15) "pant" ["value"]=> string(10) "L" } [2]=> object(stdClass)#261 (2) { ["name"]=> string(15) "glove" ["value"]=> string(4) "brand" } [3]=> object(stdClass)#262 (2) { ["name"]=> string(14) "shoes" ["value"]=> string(57) "female UGG XL" } } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 71.183.242.84
jhunkyoiori:UGG XL 都固定在尾巴六個字元嗎? 01/19 16:47
jacobcan118:不是不一定會在最後面 01/20 06:26
MOONRAKER:這樣寫沒什麼不好,不然就用array_walk()。 01/20 10:47
jacobcan118:感謝 01/22 13:59
jacobcan118:如只要XL值 除substr(..,strpos($key->value) + 4, 2) 01/23 11:44
jacobcan118:還有其他方法不需要去算字元嗎? 01/23 11:45
MOONRAKER:就preg_match嘍。 01/23 16:35
jacobcan118:pre_match要怎麼從字串中取值?他不是只能找是否含字串 01/25 12:33
danny8376:regex可以用()去框出要挑出來的結果... 01/25 13:04
danny8376:如果只有判斷的功能的話 你覺得REGEX倒底好用在哪? 01/25 13:05
chrisQQ:其實,只有判斷功能也還是很好用的 XD 01/26 09:46
jacobcan118:感謝小弟新手只是想知道怎樣可以將判斷和取值一次搞定 01/26 14:45