mardi 14 juillet 2015

Why the array_rand() is not returning me the expected array output?

I've an array titled $all_events as follows :

Array
(
    [status] => 1
    [data] => Array
        (
            [0] => Array
                (
                    [group_name] => 
                    [event_id] => 201
                    [view_id] => 0
                    [is_featured] => 0
                    [is_sponsor] => 0
                )

            [1] => Array
                (
                    [group_name] => 
                    [event_id] => 235
                    [view_id] => 0
                    [is_featured] => 0
                    [is_sponsor] => 0
                )

            [2] => Array
                (
                    [group_name] => 
                    [event_id] => 236
                    [view_id] => 0
                    [is_featured] => 0
                    [is_sponsor] => 0                   
                )

        )

    [msg] => Success
)

In resultant array I want any two elements from the inner array['data'] so for it I written following code :

$new_arr = array_rand($all_events['data'], 2);
print_r($new_arr); die;

I got following weird output :

Array
(
    [0] => 0
    [1] => 2
)

The expected result should be as follows(if first and last elements are selected randomly):

Array
            (
                [0] => Array
                    (
                        [group_name] => 
                        [event_id] => 201
                        [view_id] => 0
                        [is_featured] => 0
                        [is_sponsor] => 0
                    )        

                [2] => Array
                    (
                        [group_name] => 
                        [event_id] => 236
                        [view_id] => 0
                        [is_featured] => 0
                        [is_sponsor] => 0                   
                    )
)

Why so?

Thanks.




Aucun commentaire:

Enregistrer un commentaire