How to Find a Multidimensional Array of Find('all') in CakePHP

Asked 2 years ago, Updated 2 years ago, 87 views

Nice to meet you!

In the following array of the items table associated with the Favorites table (Item hasMany Favorite),
I would like to search the user_id of Favorite to see if there is a 10 in the user_id, but I don't know what to do.

What code should I write in this case?
If you know anything, please take good care of me.

<?phpecho$item['Favorite'][0]['user_id'];?>

displays 12.

$dataForView=array(
    'items' = > array(
        (int) 0 = > array(
            'Item' = > array(
                maximum depth reached
            ),
            'Purchase' = > array(
                maximum depth reached
            ),
            'Favorite' = > array( 
               user_id=>12
            )                                       
        ),
        (int) 1 = > array(
            'Item' = > array(
                maximum depth reached
            ),
            'Purchase' = > array(
                maximum depth reached
            ),
            'Favorite' = > array(
                user_id=>23
            )
        ),
           //(int)2 to continue

php cakephp

2022-09-30 20:59

1 Answers

For Cake2 and later, it is convenient to use the Hash utility class.

$found=Hash::extract($dataForView, 'items.{n}.Favorite [user_id=10]');

This extracts all favorite favorites whose user_id matches 10.
If you can't find it, the empty array will return, so you can make a decision.


2022-09-30 20:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.