1

I am a bit new to PHP, and I am having some trouble converting a csv file into a multidimensional array, where each dimension has a name. I need to two distinct named array so that I can use them in a preg_replace statement like the below:

$expand_abbrev=preg_replace($contracted_form , $expanded_form , $sentence); 

the file content are as follows:

contracted_form,expanded_form

'/a./','in dates ante'

'/abbrev./','abbreviation of'

'/Abbrev./','abbreviations'

'/Abd./','Aberdeen'

'/Aberd./','Aberdeen'

'/Aberdeensh./','Aberdeenshire'

This is so far what I have come up with, but it does not achieve the desired output.

$abbrev_list=file_get_contents('files/abbreviations.text');
$test=str_getcsv($abbrev_list, ",");
$expand_abbrev=preg_replace($contracted_form, $expanded_form, $sentence);

Can anyone help me out with this please ? I have been trying so many times but so far no success.

---- Please allow me to make a clarification, because it seems I might have mislead you. I would like to process a csv file which has two two values per line contracted_form and expanded_form. These two values per line are separated by a comma.

I am not sure how best to approach it, I was thinking perhaps splitting the each line into two arrays like for example each contracted_form is stored in the contracted_from array and each expanded_form is stored in the expanded_form array.

So that preg_replace can replace any instance of contracted_from, encountered in a sentence with its corresponding expanded_form. So for example the following sentence:

Hi sir, I live in a flat in Abd.

So preg_replace(Adb. , Aberdeen, Hi sir, I live in a flat in Abd.) would result in the below.

Hi sir, I live in a flat in Aberdeen.

2
  • 1
    maybe this will help stackoverflow.com/a/28118243/4341572 Commented Feb 11, 2016 at 15:21
  • If your CSV file contains the values quoted with apostrophes (') as you put them in the question then you need to tell str_getcsv() about it. By default is uses quotes (") as $enclosure. Commented Feb 11, 2016 at 15:49

2 Answers 2

0

So from what I understood you want to put each line of the csv into a multi dimensional array, if I understood you wrong please correct me.

Also bare in mind the algorithm below does not take into csv headers into account.

// The Multidimensional Array
$array = array();
$handle = fopen("file.csv", "r");
// If csv if empty, this will not run
while((($data = fgetcsv($handle, null, ',')) !== false)) {
    // The "magic" happen here.
    temp = array();
    $temp["contrated_form"] = $data[0];
    $temp["expanded_form"]  = $data[1];

    array_push($array, $temp);
}

For example if your csv file had the following:

data1,data2
data3,data4
data5,data6

The result of the above algorithm would be:

Array ( 
   [0] => Array ( 
       [contrated_form] => data1
       [expanded_form] => data2 
   ) 
  [1] => Array ( 
       [contrated_form] => data3
       [expanded_form] => data4 
   ) 
  [2] => Array ( 
       [contrated_form] => data5
       [expanded_form] => data6 
   ) 
)
Sign up to request clarification or add additional context in comments.

6 Comments

Sorry I guess you aright, I did not supply an example of what I would like to achieve. Here it comes, I would the out put to be something on these lines: array([contracted_form] => Abd., [expanded_form] =< Aberdeen) So that when preg_replace finds the abbreviation Abd., in a string, it will replace it with Aberdeen. Do you think there is away to achieve it ?
@KennethBorg post it on your question
@KennethBorg that is not a multidimensional array.
Sorry for misleading you. Do you know how can I get it to work then please ?
@KennethBorg can you post the expected result in your answer so its easier for us to read. Please
|
0

Tips:
str_getcsv does not know that the first row of your csv file contains your column names.
str_getcsv does not need "," in the statement str_getcsv($abbrev_list, ",") because , is default separator.
Statement $expand_abbrev=... has no idea what you're arguments mean.
You must write a foreach to create array $contracted_form using $test.
You must write a foreach to create array $expanded_form using $test.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.