Jump to content

write array contents into file...


mkosmosports

Recommended Posts

Hey,

 

Ive got the below array:

 

    [geninfo] => Array

        (

            [groups] => Array

                (

                    [13] => African Nations Cup XXV

                    [7] => Champions League

                    [1] => English Premiership

                )

      )

 

And Id like to write its contents to a file, but that content needs to be written in a certain format. Id like that format to be <option value="(array key)">(array value)</option>...

 

Ive tried some variations using the file_put_contents and implode functions but I havent been able to arrive to what I want to do...

 

Can someone help out?

 

Thanks for any ideas!

 

mkosmosports

 

Link to comment
Share on other sites

foreach ($array as $key => $val) {
    if (is_array($val)) {
        foreach ($val as $key2 => $val2) {
                $write .= '<option="'. $key2 . '">'.$val2.'</option>';
        }
     }
}

$fp = fopen('writeto.txt', '+w');
fwrite($fp, $write);
fclose($fp);

 

 

Given the example array that should work.

 

Link to comment
Share on other sites

Hey,

 

Thanks for your help on this one frost...

 

Im getting an error related to the write variable...

 

Notice: Undefined variable: write in C:\Web\Apache2\htdocs\index.html on line 122

 

Warning: fopen(writeto.txt) [function.fopen]: failed to open stream: No error in C:\Web\Apache2\htdocs\index.html on line 127

 

Warning: fwrite(): supplied argument is not a valid stream resource in C:\Web\Apache2\htdocs\index.html on line 128

 

Warning: fclose(): supplied argument is not a valid stream resource in C:\Web\Apache2\htdocs\index.html on line 129

Link to comment
Share on other sites

$write = ""; // declare variable to avoid notice which does not really mean anything
foreach ($array as $key => $val) {
    if (is_array($val)) {
        foreach ($val as $key2 => $val2) {
                $write .= '<option="'. $key2 . '">'.$val2.'</option>';
        }
     }
}

$fp = fopen('writeto.txt', '+w') or DIE("Could not open file to write to");
fwrite($fp, $write);
fclose($fp);

 

http://us2.php.net/fopen

 

Make sure you have write permissions for that directory. IE CHMOD 777

Link to comment
Share on other sites

Basically it concatenates or "appends" the data after it to the variable.

 

IE:

 

$cat = "How did the ";
$cat .= "cat get so fat?";

print $cat;
// will print "How did the cat get so fat?"

// the same as 

$cat = "How did the " . "cat get so fat?";

 

It can also be used like += for numbers IE:

 

$num = 5;
$num += 5;
print $num; // should print 10.

 

 

Link to comment
Share on other sites

Damn, looks like Ive got one last question for you. Im trying to insert a line break (\n) after every line thats inserted into this new file but its always printing it out as opposed to applying the line break..

 

$write .= '<option="'. $key . '">'.$val.'</option>'"\n"';

 

Thanks

Link to comment
Share on other sites

Man, I just cant seem to finish this off. Using the code below Im attempting to run the query results of 4 different queries into four separate files....

			
$allgroups = @mysql_query("SELECT id, name from sf_group ORDER BY name");
$allteams = @mysql_query("SELECT id, quick_name from sf_team WHERE linkable = 'y' ORDER BY quick_name");
$allplayers = @mysql_query("SELECT id, nick_name from sf_player ORDER BY nick_name");
$allarticles = @mysql_query("SELECT id, title from sf_articles");

$type_arr = array("allgroups" => array("groups","name"),"allteams" => array("teams","quick_name"),"allplayers" => array("players","nick_name"),"allarticles" => array("articles","title"));
$write = "";

foreach($type_arr as $key => $value)
{
while($row = mysql_fetch_array($$key,MYSQL_ASSOC))
{
$write .= '<option value="'. $row["id"] . '">'.$row["$value[1]"]."</option>\n";
}
$file = fopen("$value[0]"."_"."select.html", 'w+') or DIE("Could not open file to write to");
fwrite($file, $write);
fclose($file);
}

 

The problem Im having now is that the files that are being written contain more than theyre supposed to. I want them each to contain only the contents from the query currently being looped through but what happens is the first file is correct but the second one contains the query results of the first plus its results, and so on after that....

 

I want it to only write the contents of the query its currently looping through...

 

Any ideas?

 

Thanks!

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.