Jump to content

2D array confusion


gregm

Recommended Posts

I'm successfully gotten on top of arrays but now I need to use a 2D array.

 

I have the following variables which will contain strings with values retrieved from a sql database.

 

$Server

$Site

$Status

 

I want to create an array, so I will have something like this:

 

myrecord[0] = "Name0", "Site0", "Status0"

myrecord[1] = "Name1", "Site1", "Status1"

myrecord[2] = "Name2", "Site2", "Status2"

 

Can I do something like this:

 



$myrecord = array();
$No_Of_records = (gets this value from sql)

for ($i = 1; $i < $No_Of_records; $i++) {
    $Server = (gets a value from sql)
    $Site =  (gets a value from sql)
    $Status =  (gets a value from sql)
    $myrecord[$i] = array($Server, $Site, $Status)
}


 

Link to comment
Share on other sites

Or even

$myArray = array();

$res = mysql_query("SELECT server,site,status FROM mytable");
while ($myArray[] = mysql_fetch_row($res)) { }

 

Although I don't really advise that - I think having a loop with nothing in it is just odd.

 

But, anyway, do you even need to do this? You already have the results from the query and all this would do is take those same results and put into an array. There are some scenarios where this would make sense, but most time you can just process the results directly from the query.

Link to comment
Share on other sites

Yes- i tried it. I don't get any errors but I don't think its populating the array. Either that or I'm not printing the results correctly.

 

I've got this to print the first record in the array - but it doesnt print anything.

for ($i = 0; $i < count($myrecord) ; $i++) {
        echo $myrecord[0];
}

 

I've also tried

echo $myrecord[0,0];

and

echo $myrecord[0,0,0];

 

Either my code above to fill the array is incorrect, or my code to print out the array - or both.

 

 

 

 

Link to comment
Share on other sites

$myrecord = array();
$No_Of_records = (gets this value from sql)

for ($i = 1; $i < $No_Of_records; $i++) {
    $Server = (gets a value from sql)
    $Site =  (gets a value from sql)
    $Status =  (gets a value from sql)
    $myrecord[$i] = array($Server, $Site, $Status)
}

Link to comment
Share on other sites

@Barand - that line prints out:

 

[1] => Array
        (
            [0] => Server1
            [1] => Status1
            [2] => Site1
        )

    [2] => Array
        (
            [0] => Server2
            [1] => Status2
            [2] => Site2
        )

    [3] => Array
        (
             [0] => Server3
            [1] => Status3
            [2] => Site3
        )

How in blue blazes do I print these values out. For example to print the first array:

echo "Server" .  $myrecord[0][0] . "Status:" . $myrecord[0][1] . "Site:" . $myrecord[0][2];

 

That doesnt work - am I close? :)

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.