|
-
March 26th, 2007, 09:15 PM
#1
Array Problem
alright im having bit of trouble here...
i have 2 files english.lang and grab-template.php
I want to load english.lang and create an array out of it in get-template.php so I can then do a replace.
english.lang
Code:
'UPLOAD' => 'Upload Files',
'MANAGE' => 'Manage Files',
'SHARE' => 'Share Files'
grab-template.php
Code:
$words_arr = array(file_get_contents('language/english.lang'));
print_r($words_arr);
whats happening is that it grabs the information successfully but then it thinks the file_get_contents is one entire string which it is making and then sends it to the array so there is one thing in the array
$words_arr[0]
and when you echo that it displays
Code:
Array ( [0] => 'UPLOAD' => 'Upload Files', 'MANAGE' => 'Manage Files', 'SHARE' => 'Share Files' )
when I would actually want
Code:
Array ( [UPLOAD] => Upload Files [MANAGE] => MANAGE [SHARE] => Share Files)
Last edited by Forsakenblade; March 26th, 2007 at 09:18 PM.
-
March 27th, 2007, 03:43 AM
#2
Re: Array Problem
If I understand you correctly, what you need to do is to divide up the file into lines and then convert the values to array keys and array values. The way I would do this is to use:
PHP Code:
$temp_array=array();
$arrayOfLines=array();
$newArray=array();
//Get back whole file-->
$words_arr = file_get_contents('language/english.lang');
//Explode the whole file into lines based on the newline '\r\n' separator.
$arrayOfLines=explode('\r\n',$words_arr);
//Take each line and explode again based on the '=>' seperator, then compile newArray.
foreach($arrayOfLines as $aol => $a)
{
$temp_array=explode(' => ',$a);
//Pos 0 becomes the key, pos 1 the value-->
$newArray[$temp_array[0]]=$temp_array[1];
//Destroy array each time to prevent duplicate values.
unset($temp_array);
}
//Print out the array-->
print_r($newArray);
Hope that works. If there are duplicate keys then they will be overwritten with this approach. I didn't test it so there may be some errors, post back if you get stuck.
Last edited by Nibinaear; March 27th, 2007 at 03:49 AM.
Reason: Added print_r
-
March 27th, 2007, 05:56 PM
#3
Re: Array Problem
with what you gave me it outputs
Code:
Array ( [ 'UPLOAD'] => 'Upload Files' )
it adds a space in the array for some reason
like above it has [ 'UPLOAD']
any ideas?
Last edited by Forsakenblade; March 27th, 2007 at 06:30 PM.
-
March 27th, 2007, 08:17 PM
#4
Re: Array Problem
Forsakenblade, post the language file so we know how PHP should read it.
All consequences are eternal in some way.
-
March 27th, 2007, 08:48 PM
#5
Re: Array Problem
i did but here it is again
Code:
'UPLOAD' => 'Upload Files',
'MANAGE' => 'Manage Files',
'SHARE' => 'Share Files'
that is the ENTIRE file, obviously I will add more definitions later but for now thats what I'm working with.
-
March 28th, 2007, 03:39 AM
#6
Re: Array Problem
Sorry, that one didn't work very well, here is a better solution. File divides a file up into an array of lines:
PHP Code:
<?
$temp_array=array();
$arrayOfLines=array();
$newArray=array();
//Get back whole file-->
$arrayOfLines = file('language/english.lang');
//Take each line and explode again based on the '=>' seperator, then compile newArray.
foreach($arrayOfLines as $aol => $a)
{
//echo $a."<p />";
$temp_array=explode('=>',$a);
//Pos 0 becomes the key, pos 1 the value-->
$newArray[$temp_array[0]]=$temp_array[1];
//Destroy array each time to prevent duplicate values.
unset($temp_array);
}
//Print out the array-->
print_r($newArray);
?>
-
March 29th, 2007, 10:35 PM
#7
Re: Array Problem
thanks for the help but im still facing the spacing trouble
Code:
Array ( ['UPLOAD' ] => 'Upload Files'; ['MANAGE' ] => 'Manage Files'; ['SHARE' ] => 'Share Files'; )
is what it outputs
Edit:
I think its because in the lang file i have
Code:
'UPLOAD' => 'Upload Files',
'MANAGE' => 'Manage Files',
'SHARE' => 'Share Files'
if i have it like
Code:
'UPLOAD'=>'Upload Files',
'MANAGE'=>'Manage Files',
'SHARE'=>'Share Files'
any way to make it work with both so the space doesnt really affect it?
Last edited by Forsakenblade; March 29th, 2007 at 10:41 PM.
-
March 30th, 2007, 08:27 AM
#8
Re: Array Problem
Here is a little variation of Nibinaear's code. I haven't tested it, but it looks right.
PHP Code:
<?php
$content = @file_get_contents('language/english.lang');
if($content){
$content = str_replace("\r", "", str_replace("\n", "", $content));
$arr_lines = explode(",", $content);
$arr_parts = array();
foreach($arr_lines as $line){
$arr_temp = explode(" => ", $line);
$index = trim(str_replace("'", "", $arr_temp[0]));
$value = trim(str_replace("'", "", $arr_temp[1]));
$arr_parts[$index] = $value;
}
}
print_r($arr_parts);
?>
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
April 1st, 2007, 08:21 PM
#9
Re: Array Problem
thanks, seems to be working properly.
-
April 1st, 2007, 08:36 PM
#10
Re: Array Problem
You're welcome. I'm glad I could help. Good luck with the rest!
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|