Jump to content

Archived

This topic is now archived and is closed to further replies.

Juniorflip

Load a variable in an array

Recommended Posts

I need to load a variable into an array here is my code so far

[code]
$ourFileName2 = $ourFileName;

Function create_xml() {
$files = array('header.xml',$ourFileName2,'footer.xml');

foreach($files as $file)
{
$contents[] = file_get_contents($file);
}

$fp = fopen('new_file.xml','w+');
fwrite($fp,join("\n",$contents));
fclose($fp);

}
create_xml();[/code]

Share this post


Link to post
Share on other sites
create your contents variable outside of your loop:
[code]
<?php
$contents = array();
foreach ($files as $file) {
  $contents[] = file_get_contents($file);
}
?>
[/code]

Share this post


Link to post
Share on other sites
You need to pass the variable to the function, so:

[table]
[tr][td][b]Was:[/b][/td][td][b]Should be:[/b][/td][/tr]
[tr][td]function create_xml() [/td][td]function create_xml($outFileName2)[/td][/tr]
[tr][td]create_xml();[/td][td]create_xml($outFileName2);[/td][/tr]
[/table]

Orio.

Share this post


Link to post
Share on other sites
[quote author=Orio link=topic=111937.msg453963#msg453963 date=1161201325]
You need to pass the variable to the function, so:

[table]
[tr][td][b]Was:[/b][/td][td][b]Should be:[/b][/td][/tr]
[tr][td]function create_xml() [/td][td]function create_xml($outFileName2)[/td][/tr]
[tr][td]create_xml();[/td][td]create_xml($outFileName2);[/td][/tr]
[/table]

Orio.
[/quote]

why? he's statically declaring the file within the function itself with this line:
[code]
<?php
$fp = fopen('new_file.xml','w+');
?>
[/code]

i can't understand what you're recommending here (or at least how it would help). i believe his issue is simply a problem with variable scope. he's trying to access a variable from outside the loop that was created within. if he declares the variable before he gets into the loop and simply modifies it in there, he should be ok as long as all the variables actually contain what he thinks they do ;)

Share this post


Link to post
Share on other sites

×

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.