0

Given the following array structure:

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.
        )

)

What is the best way to access widget_content based on the value of widget_title?

For instance, I want to search for "Example" and return that first array, then store it to access the widget_content value.

4

2 に答える 2

1

One of many ways to do it:

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

Is this what you want?

<?
$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 に答える