0

给定以下数组结构:

Array
(
    [0] => Array
        (
            [widget_title] => Example
            [widget_content] => A bunch of content, including <strong>HTML</strong>.
        )

    [1] => Array
        (
            [widget_title] => Example #2
            [widget_content] => Less content this time.
        )

)

widget_content根据 的值访问的最佳方式是widget_title什么?

例如,我想搜索“示例”并返回第一个数组,然后将其存储以访问该widget_content值。

4

2 回答 2

1

多种方法之一:

$filtered = array_filter($array, function($e){
    return $e['widget_title'] == 'Example';
});
于 2012-12-05T23:30:19.850 回答
0

这是你想要的吗?

<?
$arrays = array(
            array(
                'widget_title' => 'Example',
                'widget_content' => 'A bunch of content, including <strong>HTML</strong>.'
            ),
            array(
                'widget_title' => 'Example #2',
                'widget_content' => 'Less content this time.'
            )
        );

$widget_title = 'Example';

foreach ($arrays as $array) {
    switch($array['widget_title']) {
        case $widget_title: echo $array['widget_content']; break;
    }
}
?>
于 2012-12-05T23:39:32.420 回答