Jump to content

2D array display error: undefined offset


0rangeFish
Go to solution Solved by Ch0cu3r,

Recommended Posts

I'm getting an error: Undefined offset: 1 in… I'm trying to display a 2D array after filling it. It displays fine if I echo it while it's being made, but after it is not working.

 

I'm making a pyramid solitaire game and am trying to set up the pyramid of cards. There are 7 arrays, each with a set amount of cards in them. The first array only gets one cards while the last array gets seven. This makes it like a pyramid. All the indexes that don't get a card are set to 0. So it looks like this where the 1's are cards:

 

1 0 0 0 0 0 0 0

​1 1 0 0 0 0 0 0

​1 1 1 0 0 0 0 0

1 1 1 1 0 0 0 0

1 1 1 1 1 0 0 0

1 1 1 1 1 1 0 0

1 1 1 1 1 1 1 0

1 1 1 1 1 1 1 1

 

The display2Darray function is what is causing the error. Specifically this line:

if($array[$i][$j] == 0) 

Heres my main code:

<?php
    include_once "pyramidCard.php";
    include_once "pyramidDeck.php";
    
    $theDeck = new Deck();
    $pyramid = array(array(), array(), array(), array(), array(), array(), array());
    $size = 7;

    $theDeck->shuffleCards();
    
    makePyramid($pyramid, $size, $theDeck);
    display2Darray($pyramid, $size);
    
    
    //************************
    //      FRUNCTIONS       *
    //************************
    
    function makePyramid($array, $size, $deck)
    {
        $row = 0;
        for($i = 0; $i < $size; $i++)
        {
            for($j = 0; $j < $size; $j++)
            {
                if($j > $row)
                {
                    $array[$i][$j] = 0;
                    //echo ". <br>";
                }                    
                else
                {
                    $array[$i][$j] = $deck->dealCards();
                    //echo "this card is ".$array[$i][$j]->getFace()." of ".$array[$i][$j]->getSuit()."<br>";
                }
            }
            $row++;
        }
    }
    
    
    function display2Darray($array, $size)
    {
       for($i = 0; $i < $size; $i++)
    {
        for($j = 0; $j < $size; $j++)
        {
            if($array[$i][$j] == 0)
                echo "  ";
            else
                echo $array[$i][$j]->getFace()." of ".$array[$i][$j]->getSuit();
        }
        echo "<br>";
    } 
    }
?>

 

 

 

Link to comment
Share on other sites

  • Solution

Im guessing the makePyramid() function is supposed to add the deck of cards to the $pyramid array? And display2Darray() function outputs the pyramid?

 

Passing the variable to a function only sends its value to the function. It wont change the contents of the variable. Your display2Darray() function will be trying to get contents from an empty array. This is will be the cause of the undefined offset

 

In order for you code to work as intended you can either pass $pyramid by reference

function makePyramid(&$array, $size, $deck)

Or the alternative is for the makePyramid() function to return the new array structure ($array) once it has dealt the cards.

function makePyramid($array, $size, $deck)
{
    ...

    return $array;
}


$pyramid = makePyramid($pyramid, $size, $deck);

Please read the functioins documentation here - http://php.net/manual/en/language.functions.php

Edited by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

×
×
  • 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.