Jump to content

[SOLVED] Using Explode, on an array?


nafetski

Recommended Posts

Hey everybody, time for a stupid question :P

 

Here is the scenario...

Our website has a spanish and english version, the data for both languages are pulled using an arrays of arrays.

I have one file that contains all of the arrays, here is an example of one

 

$products = array{
"dirtbike"=>array("dirtbike_eng","dirtbike_sp"),
}

 

A session variable is set (0 for English, 1 for Spanish) and the appropiate translation is pulled out of the array based on that.

 

It works great, easy to update and change things on our site...but now we want to take the task of translating and updating the page out of the hands of our web designer (me) and let operations do it.  They want a simple text file that they can edit and just upload to the web server for the changes to take effect...here is an example of what I have given them (translation.txt)

 

ID ------ English ----- Spanish

dirtbike | dirtbike_eng | dirtbike_sp

 

I can read the translation.txt file fine using $data=file(translation.txt);

Now it puts each line into an array.  What I want to do, is use explode on each line (using a ' | ' delimiter) and output each line into it's own array of arrays.

 

Here is what I have so far

 

<?php
$data=file(translation.txt);
$explodedata=explode(' | ', $data);

var_dump($explodedata);

?>

 

The $explodedata array has no values! ;(  I'm pretty sure I'm just using completely wrong logic.  Anybody mind pointing me in the right direction?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/75671-solved-using-explode-on-an-array/
Share on other sites

That is because $data is an array and explode will only work on strings.

 

You will need to loop through the $data array, eg:

$translations= file('translation.txt');

foreach($translations as $translation)
{
   list($id, $english, $spanish) = explode(' | ', $translation);

   $translate[$id][0] = $english;
   $translate[$id][1] = $spanish;
}

echo '<pre>', print_r($translate, true), '</pre>';

 

Archived

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

×
×
  • Create New...

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.