Re-arrange txt files within ZIP File in php

I am working in php. I want to read from zip file having some text files inside in this sequence like:

1 : BPBrick
2 : BPCust
3 : BPProd
6 : BPStock <---- Notice these lines
5 : BPTran <----
4 : BPValue

But i want to sort it in my own sequence, below is the desired sequence:

1 : BPBrick
2 : BPCust
3 : BPProd
6 : BPTran <------ These lines reversed
5 : BPStock <-----
4 : BPValue

How it can be possible? I just want to sort it in my own sequence and then read from zip, so that i will insert data in tables in desired sequence.

Do you know what the files are, and in what order they need to be processed? Or, does your code know that, so is it hard-coded and always the same? If so, can’t you just access each of them individually in the order you want them? I thought (but it’s a long time since I’ve tried it) that the order is based on the order the files are added to the zip file when it is created - yours appear to be in alphabetical order, suggesting perhaps that they were selected from a file manager / explorer style window and added en masse.

If the situation allows it, dealing with the files individually at least means that a badly-ordered zip file won’t cause problems next time around. Or if order is important, name the tables so that they preserve the order you need.

That’s my understanding too. So the first time a file needs updating the order will change.

From http://php.net/manual/en/array.sorting.php

The order of the sort: alphabetical, low to high (ascending), high to low (descending), numerical, natural, random, or user defined

It may be different with different environments, but I tried glob() with GLOB_NOSORT and the order was by name ascending seemingly ignoring filesize, filemtime, fileatime and filectime

I tried usort() with a callback, but it started getting messy fast.

I think the easiest way is to use a series of native array functions eg.

$target_filename = "BPStock";
$target_key = array_search($target_filename, $file_arr);
$array_first_piece = array_slice($file_arr, 0, $target_key, true); 
$array_last_piece = array_slice($file_arr, $target_key, null, true);
$target_value = array_shift($array_last_piece); 
$after_target_value = array_shift($array_last_piece); 
array_push($array_first_piece, $after_target_value); 
array_push($array_first_piece, $target_value); 
$custom_sorted_array = array_merge($array_first_piece, $array_last_piece);

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.