mercredi 17 juin 2020

PHP foreach loop with multiple array keys + If statement

I struggle with some PHP basics (multiple array keys inside foreach loop). So I have an array which print_r looks like that:

Array
(
    [0] => Array
        (
            [field] => Array
                (
                    [type] => set
                    [label] => Add game platform
                    [options] => Array
                        (
                            [fields] => Array
                                (
                                    [0] => Array
                                        (
                                            [name] => Choose publishing platform
                                            [type] => select
                                            [options] => Array
                                                (
                                                    [options] => Steam, Epic Games, GOG.COM, HumbleBundle, Origin, Playstation 4, Xbox One, Nintendo Switch
                                                )
                                        )
                                    [1] => Array
                                        (
                                            [name] => Product page URL
                                            [type] => text
                                        )
                                )
                        )
                )
            [value] => Array
                (
                    [Choose publishing platform] => Steam
                    [Product page URL] => https://steampowered.com/1
                )
        )
)

In general the array was made by a CMS, as repeater field (select field + text field). What I'm trying to achieve is to display all available elements of the array [platform + url] with a if statement (or other way which will work). So: - if in array will be a record selected as "Steam" I want to echo "Platform: Steam / URL: someurl) - if in array will be a record selected as "Epic Games" I want to echo "Platform: Epic Games/ URL: someurl) ...

I've started with something like this:

<?php                
                                $platforms = $object['factsheet_platforms'];

                                foreach ($platforms as $key => $value) {
                                  if ($key == 'Steam') {
                                    echo 'Platform: Steam';                                   
                                  }
                                  elseif ($key == 'Xbox One') {
                                    echo 'Platform: Xbox One';
                                  }
                                  ... // and so on
                                }            
                            ?>  

and it almost works. But I have no clue how to print the URL saved in the array.

Aucun commentaire:

Enregistrer un commentaire