PHP - 从数组中获取特定数据

时间:2016-02-24 13:10:12

标签: php arrays

我有一个类似于以下

的数组
array:7 [▼
  0 => array:7 [▼
    "id" => "62"
    "name" => "creativeOption"
    "label" => "Other"
    "value" => "dfsdfsdfsdf"
    "someId" => "14"
  ]
  1 => array:7 [▼
    "id" => "60"
    "name" => "creativeOption"
    "label" => "checkboxSelection"
    "value" => "AnimSomething"
    "someId" => "14"
  ]
  2 => array:7 [▼
    "id" => "61"
    "name" => "creativeOption"
    "label" => "checkboxSelection"
    "value" => "Something"
    "someId" => "14"
  ]
  3 => array:7 [▼
    "id" => "59"
    "name" => "creativeNumber"
    "label" => "Something"
    "value" => ""
    "someId" => "14"
  ]
  4 => array:7 [▼
    "id" => "57"
    "name" => "creativeNumber"
    "label" => "Something"
    "value" => "Something 2 Info"
    "someId" => "14"
  ]
]

我有兴趣获取名称为creativeOption的值。目前,我有这个

foreach ($array as $data) {
    foreach($data as $key => $value) {
        if($value == 'creativeOption') {

        }
    }
}

这样我就可以找到所有的creativeOption,但是我怎样才能获得它们的价值呢?

4 个答案:

答案 0 :(得分:5)

稍微更改您的查询

foreach ($array as $data) {
    if($data['name'] == 'creativeOption') {
        echo $data['value'];
    }
}

答案 1 :(得分:2)

您可以尝试使用array_filter:

TDirectory.Delete(<YOUR DIR PATH>);

这有帮助吗?刚刚测试并更新了我的答案。玩得开心。

答案 2 :(得分:0)

foreach ($arrays as $array) {
    foreach ($array as $key => $value) {
        if($key=='name' && $value=='creativeOption'){
            echo $array['value'];
            echo '<br>';
        }
    }
}

这样试试它应该有效

答案 3 :(得分:-1)

试试这段代码。

foreach ($array as $data) {
        foreach($data as $key => $value) {
            if($value == 'creativeOption') {
                echo $key['value'];
            }
        }
    }