简体   繁体   中英

how can i access such a php array

i was trying to access this php array with no luck, i want to access the [icon] => icon.png

Array ( [total] => 2 
  [total_grouped] => 2 
  [notifys] => Array ( [0] => Array ( [notifytype_id] => 12 
           [grouped] => 930 
           [icon] => icon.png 
           [n_url] => wall_action.php?id=930 
           [desc] => 690706096 
           [text] => Array ( [0] => Sarah O'conner ) [total] => 1 )))
$arr['notifys'][0]['icon']

ETA : I'm not sure what your comment means, the following code:

$arr = array('total'=>2, 'total_grouped' => 2, 'notifys' => array(array(
'notifytype_id' => 12, 'icon' => 'icon.png')));
echo '<pre>';
print_r($arr);
echo '</pre>';
var_dump($arr['notifys'][0]['icon']);

outputs:

Array
(
    [total] => 2
    [total_grouped] => 2
    [notifys] => Array
        (
            [0] => Array
                (
                    [notifytype_id] => 12
                    [icon] => icon.png
                )

        )

)

string(8) "icon.png" 

Generally, code never outputs nothing. You should be developing with all errors and notifications on.

$arr['notifys'][0]['icon']
rg = Array ( [total] => 2 [total_grouped] => 2 [notifys] => Array ( [0] => Array ( [notifytype_id] => 12 [grouped] => 930 [icon] => icon.png [n_url] => wall_action.php?id=930 [desc] => 690706096 [text] => Array ( [0] => Sarah O'conner ) [total] => 1 )));
icon = rg["notifsys"][0]["icon"];

Everybody is posting right answer. Its just you have giving a wrong deceleration of array.

Try var_dump / print_r of array and then you can easily understand nodes.

$arr = array(total => 2, 
    total_grouped => 2, 
    notifys => array( 0 => array(notifytype_id => 12, 
        grouped => 930,
        icon => 'icon.png', 
        n_url => 'wall_action.php?id=930', 
        desc => 690706096,
        text =>array(0 => 'Sarah Oconner' ),
        total => 1, 
        ),
    ),
);

echo $arr['notifys']['0']['icon'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM