当前位置:Gxlcms > PHP教程 > php嵌套数组递归搜寻返回数组key

php嵌套数组递归搜寻返回数组key

时间:2021-07-01 10:21:17 帮助过:33人阅读

php嵌套数组递归搜索返回数组key
var rewardTypes={"experience":{"\u7ecf\u9a8c\u503c":{"1":"\u660e\u661f\u6587\u827a\u996d","2":"\u6587\u827a","3":"\u963f\u91cc\u5df4\u5df4\u7f51"}},"money":{"\u865a\u62df\u8d27\u5e01":{"little_red_heart":"\u5c0f\u7ea2\u5fc3","golden_heart":"\u91d1\u8272\u4e4b\u5fc3","diamond_heart":"\u94bb\u77f3\u5fc3"}}};

使用1:

getShowText("3",rewardTypes,true);

返回:

"经验值"

使用2:

getShowText("3",rewardTypes);

返回:

"阿里巴巴网"

//递归搜索key对应文本,isParent=true则返回所在父节点对应文本名,false则返回匹配到的节点文本function getShowText(keyid,data,isParent){    var title="";     for(var key in data){        if(keyid==key){            if(typeof(data[key])=="object"){                for(var item in data[key]){                    title=item;                    break;                }                break;            }else{                title=data[key];            }            break;        }else if(typeof(data[key])=="object"){             title=getShowText(keyid,data[key],isParent);            if(title!=""){                if(isParent){                    for(var item in data[key]){                        title=item;                    }                }                break;            }        }    }    return title;}

人气教程排行