I have a CSV file to be imported to MYSQL database.
The problem is that each line has different number of fields.
and their arrangement is rather random.
However, they can be divided into categories (text,images….etc).
In current form it’s too irregular for direct import to the database, so I tired to re-format it using PHP.
simplified example:
$messy = array(
[0] => array("text1","text2" ),
[1] => array("img1.jpg" ),
[2] => array("text1","img1.jpg","img2.jpg"),
[3] => array("img1.jpg","text1","text2"),
...
[1000] => array("text2","img1.jpg","img2.jpg","text1")
);
i would like the new array to match the following the pattern
array(
[0] =>array(TEXT,TEXT,IMG,IMG),
[1] =>array(TEXT,TEXT,IMG,IMG),
in case of not sufficient data the remaining values=0;
[0] =>array("text1","text2",0,0),
[1] =>array(0,0,"img1.jpg",0),
i’ve tried to create new array and move “text” fileds to the begining like this:
$ordered=array();
$i=0;
foreach($messy as $row){
foreach($row as $item){
if (strlen(strstr($item,"text"))>0) {
if(($key = array_search($item, $row)) !== false){
unset($row[$key]);
}
array_unshift($row,$item);
}
}
$ordered[$i++]=$row;
}
However it doesn’t re-format, just reorder.
Completely untested. Assumes there are at most 2 TEXT types and 2 IMG types. Prone to failure.