从数组中筛选结果,而不是显示php中每个索引的键和结果

时间:2018-11-15 09:53:42

标签: php arrays

我有一个数据数组,在其中我得到的结果为var_dump($get_data);

array(45) {
    ["sitelock"] => array(4) {
        [181] => array(2) {
            ["plan_name"] => string(10) "Enterprise" 
            ["plan_status"] => string(6) "Active"
        } 
        [180] => array(2) {
            ["plan_name"] => string(7) "Premium" 
            ["plan_status"] => string(6) "Active"
        } 
        [179] => array(2) {
            ["plan_name"] => string(12) "Professional" 
            ["plan_status"] => string(6) "Active"
        } 
        [178] => array(2) {
            ["plan_name"] => string(5) "Basic" 
            ["plan_status"] => string(6) "Active"
        }
    } 
    ["codeguard"] => array(4) {
        [230] => array(2) {
            ["plan_name"] => string(12) "Professional" 
            ["plan_status"] => string(6) "Active"
        } 
        [229] => array(2) {
            ["plan_name"] => string(5) "Basic" 
            ["plan_status"] => string(6) "Active"
        } 
        [232] => array(2) {
            ["plan_name"] => string(10) "Enterprise" 
            ["plan_status"] => string(6) "Active"
        } 
        [231] => array(2) {
            ["plan_name"] => string(7) "Premium" 
            ["plan_status"] => string(6) "Active"
        }
    }
}

步骤1 =我想从数组中过滤sitelock,而不是将其键值181,180,179,178显示为列表中的pid,而["plan_name"]值分别显示为Enterprise,Premium,专业的

以下是预期结果示例:

pid  Plan Name
181  Enterprise
180  Premium
179  Professional
181  Enterprise
181  Basic

到目前为止,我正在尝试类似的事情

$get_data = json_decode($data,true);
//var_dump( $get_data);
$productkey = "sitelock";
foreach($get_data as $key => $value) {
    if($key == $productkey) {
        // table code goes here
    }
}

1 个答案:

答案 0 :(得分:1)

您需要遍历sitelock的键$get_data,并循环创建包含所需自定义数据的新数组。

$arr = [];
foreach($get_data['sitelock'] as $key => $value){
    $arr[$key] = $value['plan_name'];
}

demo中查看结果