1

Is it possible in php to convert this kind of data

    ABANO TERME A001
ABBADIA CERRETO A004
ABBADIA LARIANA A005
ABBADIA SAN SALVATORE   A006
ABBASANTA   A007
ABBATEGGIO  A008
ABBIATEGRASSO   A010

to an associated array where in the first column are keys and in the second column are values?

4
  • 3
    Yes. Where does the data come from and what do you have so far? Commented Apr 6, 2017 at 8:51
  • Is it string or what kind of data it is? Commented Apr 6, 2017 at 8:54
  • Do you mean $myArr["ABETONE"] = "A012";? Commented Apr 6, 2017 at 8:56
  • @Gaurav i have data in a .txt file in the format i have shown above Commented Apr 6, 2017 at 8:57

3 Answers 3

2
$handle = fopen(path\to\file\filename, "r");
$result_arr = array();
while(($filesop = fgets($handle)) !== false)
{
    $line_arr = explode(" ", $filesop);
    $value = end($line_arr);
    unset($line_arr[array_search($value, $line_arr)]);
    $key = implode(" ", $line_arr);
    $result_arr[trim($key] = trim($value);
}
print_r($result_arr);

Store in the text file like this (separated by commas):

ABANO TERME,A001
ABBADIA CERRETO,A004
ABBADIA LARIANA,A005
ABBADIA SAN SALVATORE,A006
ABBASANTA,A007
ABBATEGGIO,A008
ABBIATEGRASSO,A010

Solution to read file:

$handle = fopen(path\to\file\filename, "r");
$result_arr = array();
while(($filesop = fgets($handle)) !== false)
{
    $line_arr = explode(",", $filesop);
    $value = end($line_arr);
    unset($line_arr[array_search($value, $line_arr)]);
    $key = implode(",", $line_arr);
    $result_arr[trim($key] = trim($value);
}
print_r($result_arr);
Sign up to request clarification or add additional context in comments.

9 Comments

i updated my question because my data may be as i have shown now but if i explode with space they are misinterpreted @JustCarty
@Gaurav it throws an error: Undefined offset: 0. Sorry but i am a beginner in php
@Gaurav Array ( [ABANO] => TERME A001 [ABBADIA] => LARIANA A005 [ABBADIA SAN] => SALVATORE A006 [] => ZUNGRI M204 [ACI] => SANT'ANTONIO A029 [ACQUANEGRA] this is the output still misinterpreted :(
@belka Is it possible to change each row so they are delimited by a "," and then change the explode and implode to use a ","?
@JustCarty but in belka question text file don't have any comma. So we have to wait.
|
2
    $txt = file_get_contents('foo.txt');
    $arr=array_values(array_filter(explode(' ',$txt)));
    $new=[];
    for($i=0; $i<count($arr)-1;$i++){

    $new[$arr[$i]]=$arr[++$i];
    }

    print_r($new);

Comments

1

One solution for this particular data would be:

<?php

$data = file_get_contents('data.txt')

//use regular expressions to parse whole file at once
preg_match_all("/([^\s]+\s?[^\s]+?)\s+(.\d+)$/m", $data, $m);

//and combine results from result array ($m)
//first entry - $m[0] contains whole lines thats why I dont use it here
//second entry - $m[1] - data from first brackets is an array key
//third entry - $m[2] - is a value (both are separated by some number of spaces - \s+)
$array = array_combine($m[1], $m[2]);

print_r($array);

?>

output:

Array
(
    [ABETONE] => A012
    [ABRIOLA] => A013
    [ACATE] => A014
    [ACCADIA] => A015
)

of course, as colleagues above pointed out, this depends on where data came from, and if this particular set is representative.

1 Comment

I have cases when keys are two words like: ACQUAVIVA COLLECROCE A050 ACQUAVIVA D'ISERNIA A051 in this case the keys and values are misinterpreted. Can this be fixed?

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.