Jump to content

How to a browse an array many times


Krokodil
 Share

Recommended Posts

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 by Krokodil
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Posted (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 by Krokodil
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.
 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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} &ndash; {$r->name}<br>";
 }

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.