pairing 2D grid

Hi everyone! I need some assistance with matching a two-dimensional array in my code snippet below:

$games = array("Game1" => "Team 2 vs. Team 3", "Game2" => "Team 1 vs. Win 1","Game3" => "Loser Game 1 vs. Loser Game 2","Game4" => "Win Game 2 vs. Win Game 3", "Game5" => "Win Game 4 vs. Loser Game 4 (If Loser 4 is First Loss)");

    $time= array("06:00 " => "06:30","06:30 " => " 07:00","07:00" => "07:30","07:30" =>"08:00","08:00" => "08:30");

    foreach($games as $games => $versus)
    {
       foreach($time as $start_time => $end_time)
       {
          echo $games . ": ". $versus ." ". $start_time. " to " . $end_time . "<br>";
       }
    }
    

Currently, the output looks like this:

Output mentioned above...
    

But I would like it to be displayed as follows instead:

Game1: Team 2 vs. Team 3 06:00 to 06:30
    Game2: Team 1 vs. Win 1 06:30 to 07:00
    Game3: Loser Game 1 vs. Loser Game 2 07:00 to 07:30
    Game4: Win Game 2 vs. Win Game 3 = 07:30 to 08:00
    Game5: Win Game 4 vs. Loser Game 4 (If Loser 4 is First Loss) = 08:00 to 08:30
    

If anyone can help me achieve this, I would greatly appreciate it. Thank you in advance!

Answer №1

If you are able to manipulate the arrays, I would suggest considering a different structure that allows for easier matching of games and times.

However, if the arrays do not have numeric keys, you will need to retrieve the key key() and value current() from the $time array and move the array pointer forward with each loop iteration:

foreach($games as $game => $versus)
{
    echo $game . ": " . $versus ." ". key($time) ." to ". current($time) ."<br>";
    next($time);
}

An alternative approach could be using each():

foreach($games as $game => $versus)
{
    list($start, $end) = each($time);
    echo $game . ": " . $versus ." ". $start ." to ". $end ."<br>";
}

Answer №2

If you're looking to streamline the organization of games and their start/end times, consider consolidating them into a single multi-dimensional array. The following example is tailored for 2 games, but it can easily accommodate more:

$games_list = array(
    "Game1" => 
            Array(
                "versus" => "Team 2 vs. Team 3",
                "start_time" => "06:00",
                "end_time" => "06:30"
            ),
     "Game2" => 
            Array(
                "versus" => "Team 1 vs. Win 1",
                "start_time" => "06:30",
                "end_time" => "07:00"
            )
);

foreach ($games_list as $game_name => $attributes)
{
    echo "<br>";
    echo $game_name . ": versus : " . $attributes['versus'] . ": start_time: ";
    echo $attributes['start_time'] . ": end_time: " . $attributes['end_time'];
}

Output:

Game1: versus : Team 2 vs. Team 3: start_time: 06:00: end_time: 06:30

Game2: versus : Team 1 vs. Win 1: start_time: 06:30: end_time: 07:00

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the functionality behind a free hosting website?

Is anyone familiar with websites like Hostinghood, where users can create a subdomain and upload HTML, CSS, etc.? I'm curious about how they operate and how I can create a similar site. This is my first question here, so please respond instead of disl ...

PdoServiceProvider not found in Dokku-deployed Silex deployment

After encountering random crashes with socket errors while using the abandoned herrera-io/silex-pdo PDO provider in my Silex project, I switched to csanquer/pdo-service-provider. Everything worked perfectly on my localhost server, but upon deployment to a ...

Having trouble creating a Node.js equivalent to PHP's hash checking functionality

These PHP lines are effective, but I am unable to replicate them in Node. $secret_key = hash('sha256', XXXX, true); $hash = hash_hmac('sha256', YYYY, $secret_key); The documentation states that hash() returns raw binary data, but it a ...

Creating a dynamic dropdown menu using JQuery that does not automatically submit the form when a value is

Upon selecting a background color from the dropdown menu, I am generating a dynamic dropdown for text colors. The Text Color dropdown is populated correctly based on the selection of Background Color. Although the functionality works as intended, I encoun ...

Having trouble accessing the website content with PHP5 domdocument

<?php class parsedictionary { public function _process() { $webpage="http://www.oppapers.com/essays/Computerized-World/160871?read_essay"; $doc=new DOMDocument(); $doc->loadHTML($webpage); e ...

Altering information with the use of history.pushState throughout time

I've created a template that I want to load using the jQuery .load() function. However, during testing, I discovered that it's not loading at all. Here is the code I'm trying to use for loading: function open() { history.p ...

Troubleshooting Socket Errors in PHP for Apple Push Notification Feedback Service

For my project, I referred to this question on implementing APNS Feedback Service. Below is the code snippet I used to request feedback from servers; function send_feedback_request() { //connecting to APNS feedback servers //ensuring correct dev/p ...

51% of memory being consumed by /ds_extras_node_page_view in Drupal 7

I've been dealing with a Drupal 7 website that experiences downtime a few times per month for about 4 to 5 minutes. According to the New Relic report, /ds_extras_node_page_view consumes 52% to 92% of the memory during loading, which is causing the sit ...

Zend: Designing models for a items database

In the process of developing two distinct models, namely products and product_manufacturers, I aim to streamline the retrieval and modification of manufacturers and products through the admin interface. The blueprint for these tables is outlined below (sub ...

PHP fails to retrieve data from a JavaScript Ajax function using the FormData object (specifically, when

I'm facing an issue where my file is not being detected when I send a FormData object using AJAX and PHP code. Both the `$_FILES` array and the `$_POST` array appear to be empty. However, the browser does send the file with the AJAX request. <inpu ...

Can administration users view the same content and functionalities as other user types?

I am currently working on a Laravel web application that caters to two distinct types of users: customers admins Depending on their user type, they have access to different features and functionalities. Customers When customers log in, they are greete ...

Understanding the optimal scenarios for utilizing static functions within your codebase

One of my colleagues argues that static is beneficial when performing tasks that do not require any extensions, but we faced a problem when we needed to add a new feature on top of his code. We ended up having to extensively modify the original code becaus ...

In PHP, one way to update a variable outside a function is by creating a function that accepts inputs and conditions the output before updating the variable. This allows for dynamic

My latest project involves creating a semi-automatic attendance register. When an individual submits their name, a table is updated to reflect their status. Below is the form code I have developed: <form action="Attendance.php" method="post"> ...

Displaying Magento's Shopping Cart Price Rules Directly on Product Pages

Currently seeking a solution to implement 'Shopping Cart Price Rules' on the product page itself, before adding the item to the cart. The reason for not opting for 'Catalog Price Rules' is due to its limitations in functionality. Specif ...

Retrieve content from Wordpress blog including image preview

Can someone help me find a PHP script that can import posts from my WordPress blog and display them on another website, complete with a thumbnail of each blog post? Thank you in advance for your assistance! ...

"Pear website has been relocated to a new hosting provider, causing certain pages to experience technical

I recently transferred my website from an old shared hosting plan to a new VPS hosting plan. The site is coded in PHP. After successfully moving the site, I encountered an error when trying to access a specific page: Fatal error: Call to undefined me ...

Tips for effectively internationalizing white-labeled PHP code

In my current code base, I have the ability to customize it for various customers. This customization is typically done by loading a class with customer-specific constants, all having the same class name. To avoid any collisions, only one set of constants ...

Tips for guiding users to a different page based on whether a linkid is associated with a specific Cat

I created this script, but I am facing an issue with the redirect. Can someone please assist me as soon as possible? <?php include('config.php'); $number = intval($_POST['catid']); $query = mysql_query("SELECT * FROM sound ...

Managing JSONP calls in ZEND is crucial for handling cross-domain requests

After conducting a search and not finding an answer, I have decided to ask the question myself. Can the Zend Framework handle JSONP calls? I came across this page: http://framework.zend.com/wiki/display/ZFPROP/Zend_Json_Server+-+Lode+Blomme However, I a ...

Issue with menu display on Internet Explorer

Having some trouble with IE. My menu displays fine in Firefox and Chrome, but IE is causing issues... It seems that IE isn't rendering the <li> tag properly, so my dropdown menu isn't functioning in IE. Can anyone assist me? Here's t ...