Krokodil Posted August 14, 2022 Share Posted August 14, 2022 (edited) Hello, In this array : https://pastebin.com/8nGbPx0P I want to check if the key "iso_639_1" is equal to "fr"and if yes, then I look for the value of the key "name" and if "iso_639_1" has no value "fr", then I want to go through the Array again looking for a key wtih valor "en", etc. But I don't know how to do it. If have try with if / else, but he stop when he find a good valor, he don't only check for FR valor, and if not exist, retry. This is my code : <?php function bandeAnnonce($videos) { foreach($videos->results as $key => $v) { if($v->iso_639_1 == 'fr') { if(preg_match('#bande|trailer#is', $v->name)) return (string) is_youtubeId($v->key); } else { if(preg_match('#bande|trailer#is', $v->name)) { echo $v->key; return (string) is_youtubeId($v->key); } } } return null; } Do you have an idea? Edited August 14, 2022 by Krokodil Quote Link to comment https://forums.phpfreaks.com/topic/315182-how-to-a-browse-an-array-many-times/ Share on other sites More sharing options...
Barand Posted August 14, 2022 Share Posted August 14, 2022 I can't imagine anyone attempting to edit that amount of pastebin data to make it suitable to test any solution. Post a json_encoded version of the array here so we have something useable. PS and post the code you have so far - it may give us a clue to what you are talking about. Quote Link to comment https://forums.phpfreaks.com/topic/315182-how-to-a-browse-an-array-many-times/#findComment-1599352 Share on other sites More sharing options...
Krokodil Posted August 14, 2022 Author Share Posted August 14, 2022 (edited) Sorry, i can't edit my post, but i have added my php code and here is the array code with 4 examples : stdClass Object ( [id] => 634649 [results] => Array ( [0] => stdClass Object ( [iso_639_1] => en [iso_3166_1] => US [name] => Peter Brings Villains To Happy's Condo [key] => Q8NbFwUGI38 [site] => YouTube [size] => 1080 [type] => Clip [official] => 1 [published_at] => 2022-08-09T00:00:06.000Z [id] => 62f1b9f3df857c007fb0d2e0 ) [1] => stdClass Object ( [iso_639_1] => en [iso_3166_1] => US [name] => The More Fun Stuff Version [key] => 6Bd2DZZi2eM [site] => YouTube [size] => 1080 [type] => Teaser [official] => 1 [published_at] => 2022-06-11T01:45:16.000Z [id] => 62a404ba85005d0052ca1b50 ) [2] => stdClass Object ( [iso_639_1] => en [iso_3166_1] => US [name] => The Condo Fight Scene [key] => vG7-hKnYcxQ [site] => YouTube [size] => 1080 [type] => Clip [official] => 1 [published_at] => 2022-05-16T21:00:21.000Z [id] => 628300e01495650066b593a5 ) [3] => stdClass Object ( [iso_639_1] => fr [iso_3166_1] => FR [name] => Spider-Man : No Way Home - Bande Annonce France [VF] [key] => o-qvJ2iUqvA [site] => YouTube [size] => 1080 [type] => Trailer [official] => 1 [published_at] => 2021-11-17T07:36:55.000Z [id] => 6196c4769603310062cf913a ) ) ) Edit : I have a solution, but I don't think it's really a good option, because doing 2 foreach for the same Array, it's not great function bandeAnnonce($videos) { foreach($videos->results as $key => $v) { if($v->iso_639_1 == 'fr') { if(preg_match('#bande|trailer#is', $v->name)) return (string) is_youtubeId($v->key); } } foreach($videos->results as $key => $v) { if(preg_match('#bande|trailer#is', $v->name)) { echo $v->key; return (string) is_youtubeId($v->key); } } return null; } Edited August 14, 2022 by Krokodil Quote Link to comment https://forums.phpfreaks.com/topic/315182-how-to-a-browse-an-array-many-times/#findComment-1599353 Share on other sites More sharing options...
kicken Posted August 14, 2022 Share Posted August 14, 2022 Are there only fr/en entries? If there's not an fr or en entry, do you just want whatever the first one is? I'd probably sort the array using usort according to your preferences, then just pick the first entry. When your posting an example array, it's best to use json_encode or var_export as these give versions of the data which we can simply copy/paste into a script to test with. The print_r format is nice for viewing, but not great for sharing. Quote Link to comment https://forums.phpfreaks.com/topic/315182-how-to-a-browse-an-array-many-times/#findComment-1599356 Share on other sites More sharing options...
Krokodil Posted August 14, 2022 Author Share Posted August 14, 2022 There are entries in all languages, but I filter only English and French. I don't necessarily want the first entry, because sometimes it's not a trailer but an extrait of the movie, so that's why I look in the video name with preg_match. The data I give, are the same data with which I use my function. Quote Link to comment https://forums.phpfreaks.com/topic/315182-how-to-a-browse-an-array-many-times/#findComment-1599357 Share on other sites More sharing options...
kicken Posted August 14, 2022 Share Posted August 14, 2022 Since you're only interested in fr/en entries, then this is what I'd do. $enResult = null; foreach ($videos->results as $item){ //If name does not contain bande or trailer, skip it. if (!preg_match('#bande|trailer#is', $item->name)){ continue; } //If the item is fr, return it immediately. if ($item->iso_639_1 === 'fr'){ return is_youtubeId($item->key); } //Otherwise, if the item is en, save it for later. else if ($item->iso_639_1 === 'en'){ $enResult = $item; } } //We only get to this point if no fr item was found. Check if we found an en item and if so return that. if ($enResult){ return is_youtubeId($enResult->key); } //Otherwise, found nothing. return null; You only go through the array once looking for the fr entry you want, if you find it then stop there. While going through the array, save your secondary en choice to a variable. If you get to the end of the array without finding the desired fr entry then you can return the saved en entry. Quote Link to comment https://forums.phpfreaks.com/topic/315182-how-to-a-browse-an-array-many-times/#findComment-1599365 Share on other sites More sharing options...
Barand Posted August 14, 2022 Share Posted August 14, 2022 Would this work? $results = array_filter($results, fn($v) => in_array($v->iso_639_1, ['en','fr']) && $v->type == 'Trailer' && $v->site == 'YouTube' ); rsort($results); // 'fr' first then 'en' results foreach ($results as $r) { echo "{$r->iso_639_1} – {$r->name}<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/315182-how-to-a-browse-an-array-many-times/#findComment-1599367 Share on other sites More sharing options...
benanamen Posted August 14, 2022 Share Posted August 14, 2022 (edited) Is this "Array" coming from a Database? If so, why are you not just querying the DB for the specific data you want? Edited August 14, 2022 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/315182-how-to-a-browse-an-array-many-times/#findComment-1599368 Share on other sites More sharing options...
Krokodil Posted August 14, 2022 Author Share Posted August 14, 2022 I will goo to sleep, i check code tomorrow; thanks for you're help. @benanamen : no it comes from an API and I don't store the information, it's a special project I just cache the data to avoid bothering the API Quote Link to comment https://forums.phpfreaks.com/topic/315182-how-to-a-browse-an-array-many-times/#findComment-1599370 Share on other sites More sharing options...
Krokodil Posted August 15, 2022 Author Share Posted August 15, 2022 @Barand sorry but don't work. @kicken worked perfectly as i want thanks you very much !!! Quote Link to comment https://forums.phpfreaks.com/topic/315182-how-to-a-browse-an-array-many-times/#findComment-1599387 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.