第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何獲取 PHP 數(shù)組中特定 Key 的出現(xiàn)次數(shù)?

如何獲取 PHP 數(shù)組中特定 Key 的出現(xiàn)次數(shù)?

PHP
揚(yáng)帆大魚 2023-08-06 15:44:23
我有一個(gè)如下所示的示例對(duì)象數(shù)組,它是一個(gè) json 解碼數(shù)組。該對(duì)象數(shù)組中的索引 value1,value2,value3 將來可以增長(zhǎng)到 value'n' 。我需要獲取此對(duì)象數(shù)組中索引出現(xiàn)次數(shù)的計(jì)數(shù)。我怎樣才能實(shí)現(xiàn)這一點(diǎn)?我正在使用 PHP 5.6     stdClass Object(                      [general_rates] => Array                 (                    [0] => stdClass Object                          (                               [id] => 1                               [item] => Item name                               [cost1] =>                                [cost2] =>                                [cost3] => 60                            )                    [1] => stdClass Object                           (                               [id] => 2                               [item] =>                                [cost1] => N/A                               [cost2] => N/A                               [cost3] => 60                                   )                         )                        [value1] => EUROPE                        [value2] => AMERICA                        [value3] => FRANCE                 )
查看完整描述

2 回答

?
慕的地10843

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊

您可以使用以下函數(shù)對(duì)數(shù)組元素進(jìn)行擬合array_filter:


$jsonStr = '{

    "general_rates" : [

        { "id" : 1, "item" : "Item name", "cost1": "", "cost2": "", "cost3": "" },

        { "id" : 2, "item" : "", "cost1": "N/A", "cost2": "N/A", "cost3": 60 }

    ],

    "value1" : "EUROPE",

    "value2" : "AMERICA",

    "value3" : "FRANCE"

}';


$decoded = json_decode($jsonStr, true);


// Filter the array elements which key start with 'value'

$filtered = array_filter($decoded, function ($key) {

    return strpos($key, 'value') === 0;

}, ARRAY_FILTER_USE_KEY);


var_dump(count($filtered));


查看完整回答
反對(duì) 回復(fù) 2023-08-06
?
一只甜甜圈

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊

使用preg_grep一個(gè)小的正則表達(dá)式“從值開始”:


$ar = ['value1', 'value2', 'no_vlue_here', 'and_here'];

$filtered = preg_grep('/^value/', $ar);

echo count($filtered);

小提琴



查看完整回答
反對(duì) 回復(fù) 2023-08-06
  • 2 回答
  • 0 關(guān)注
  • 174 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)