Q:

Inserting data from *.txt file into assoc array

I have a text file 'test.txt' having  fruitnames and their scientific names in separate lines.
I want to create an array as  
$FRUIT_NAME ( fruit1=>"apple, 
              SNfruit1=>"Pyrus malus",  
              fruit2=>"mango", 
              SNfruit2=>"Mangifera indica"  ...  )

But, I am getting blank values in SNfruit1, SNfruit2.. 
Here is the 'test.txt'
SNapple="Pyrus malus" 
SNmango="Mangifera indica" 
SNbanana="Musa paradisicum" 
SNorange="Citrus aurantium" 
SNDefault="No SN" 
fruit1="apple|$SNapple" 
fruit2="mango|$SNmango" 
fruit3="banana|$SNbanana" 
fruit4="orange|$SNorange" 
veg1="potato" 
veg2="cauliflower"

Here is the code file 'test.php':
$lines=explode("\n", file_get_contents('test.txt'));

$FRUIT_NAME=[] ;
$result=[];
for($i=0; $i<count($lines); $i++)
{   if (strpos($lines[$i], '=') !== false) 
    {  
        $link = explode('=', $lines[$i], 2); 
        if (strpos(substr($lines[$i], 0, 2), 'SN') !== false)
           $result[str_replace('"',"",$link[0])] = $link[1];
        else
        {   $FName=$link[1];
           if (strpos($link[1], '|') !== false)
              {  
                $LINK = explode('|$', $link[1]); 
                $FName=$LINK[0];
                $SN=str_replace('"',"",$LINK[1]);
              }
           else $SN="SNDefault" ;

           $FRUIT_NAME[$link[0]] = $FName ;
           //$FRUIT_NAME["SN".$link[0]] = $SN ;
           $FRUIT_NAME['SN'.$link[0]] = $result[$SN] ;
        }
    }
}

foreach ($result as $key => $value) echo "$key::$value".'<br>';

echo '============================================';

foreach ($FRUIT_NAME as $key => $value) echo "$key::$value".'<br>';  

A:

Re-write your middle if-else statement:
if (strpos(substr($lines[$i], 0, 2), 'SN') !== false)
    $result[str_replace('"',"",$link[0])] = $link[1];
else

with 
if (strpos(substr($lines[$i], 0, 2), 'SN') !== false){ 
   $result[$link[0]] = str_replace('"',"",$link[1]);
}

and 
$FName=$link[1];
if (strpos($link[1], '|') !== false) {  
    $LINK = explode('|$', $link[1]); 
    $FName=$LINK[0];
    $SN=str_replace('"',"",$LINK[1]);
} else $SN="SNDefault" ;

$FRUIT_NAME[$link[0]] = $FName ; 
$FRUIT_NAME['SN'.$link[0]] = $result[$SN] ;

with 
$link[1] = str_replace('"','',$link[1]);

if (strpos($link[1], '|$') !== false) {  
     $sublink = explode('|$', $link[1]); 
     $FRUIT_NAME[$link[0]] = $sublink[0]; 
     $FRUIT_NAME["SN".$link[0]] = $result[trim($sublink[1])];
} else { 
    $FRUIT_NAME[$link[0]] = $link[1] ; 
} 

then outputs will be:
SNapple::Pyrus malus
SNmango::Mangifera indica
SNbanana::Musa paradisicum
SNorange::Citrus aurantium
SNDefault::No SN
============================================
fruit1::apple
SNfruit1::Pyrus malus
fruit2::mango
SNfruit2::Mangifera indica
fruit3::banana
SNfruit3::Musa paradisicum
fruit4::orange
SNfruit4::Citrus aurantium
veg1::potato
veg2::cauliflower

The problem was in true and false parts. 
In true you did str_replace of wrong element, that's why you've received values like "apple instead of clean apple.
In false part you didn't trim, that's why indexes were incorrect and so your $FRUIT_NAME was built not so correctly.
Note: echo '============================================'.'<br>';

