I have data contained in an array which is like so,
$file['info']['files'] = array(
[0] => array(
'length' => (int),
'path' => array (
[0] => 'file.txt',
),
),
[1] => array(
'length' => (int),
'path' => array (
[0] => 'directory one',
[1] => 'file2.txt',
),
),
[2] => array(
'length' => (int),
'path' => array (
[0] => 'directory one',
[1] => 'directory two',
[2] => 'file3.txt',
),
),
);
The $file['info']['files'] array can contain any number of elements. The path array contained in each $file['info']['files'] array is where I am having trouble.
It contains information about a file structure. If just 1 element exists then it is a file. If more than one element exists then each element starting from the top is a parent folder of the next element and the last element is the file in the last folder. Taking the example above would be a file structure of
FILE file1.txt
FOLDER directory one
FILE file2.txt
FOLDER directory two
FILE {file3.txt}
I would like to extract this data into my own array structure which is to be as follows,
$sortedFiles = array(
'file1.txt' => (int),
'directory one' => array(
'file2.txt' => (int),
'directory two' => array(
'file3.txt' => (int),
),
),
);
I have this code so far,
foreach($file['info']['files'] as $file) {
// LENGTH AND PATH ARE SET
if(isset($file['length'], $file['path'])) {
// GET COUNT OF FILE PATH ARRAY
$count = count($file['path']);
// SINGLE FILE
if($count == 1) {
$sortedFiles[$file['path'][0]] = $file['length'];
// FILES IN DIRECTORY
} else {
// BUILD ARRAY STRUCTURE FOR DIRECTORIES
}
}
}
I am having trouble when it comes to adding directories to the array. I could do it manually and only go so many directories down each time checking if the array for the directory exists and if not create it, and if it does exist then add to it. I tried this with the code below but it only goes one directory deep (the code went where it says // BUILD ARRAY STRUCTURE above).
// FOLDER NOT SET
if(!isset($files[$file['path'][0]])) {
$sortedFiles[$file['path'][0]] = array($file['path'][1] => $file['length'],);
// FOLDER SET
} else {
$sortedFiles[$file['path'][0]][$file['path'][1]] = $file['length'];
}
How can I dynamically create arrays for each directory that exists and add the information that is needed bearing in mind that the directory structure could be any amount of levels deep?
Thanks for taking the time to read my rather long question and I appreciate any help that anyone gives to me.
You will need to call your function recursively, as in the example below:
function get_contents_dir( $dir )
{
$names = array();
if ( is_dir($dir) && is_readable($dir) )
{
foreach ( scandir($dir) as $file )
{
if ( is_dir($dir."/".$file) && is_readable($dir."/".$file) )
{
$names[] = get_contents_dir($dir."/".$file);
}
if ( is_file($dir."/".$file) && is_readable($dir."/".$file) )
{
$names[] = $dir."/".$file;
}
}
}
return $names;
}
This function first opens the set $dir folder and scans the list of files, adding each found file to the array which is, after scanning the folder, returned as the return value of the function.
The twist comes in when an entry of the scandir() result (list of files and folders in the folder) is actually a folder. If that happens, the function is called from it's internals, recursively (see the line $names[] = get_contents_dir($dir."/".$file); calling the function from within the function) and the subfolder will be indexed too. Rinse and repeat, until all subfolders are indexed.
If you call the function and let it execute, an array will be returned. Each key of the array will be an entry. If it was a file, the value linked to the key is the name of the file, if it was a folder, the value will be another array nested into the previous one.
Here is an example dump taken of the returned array:
array (
0 => './libmysqlclient.so.16.0.0',
1 => './libmysqlclient_r.so.16.0.0',
2 =>
array (
0 => './libs/libboost_thread-mt.a',
1 => './libs/libboost_thread-mt.so.1.38.0',
2 => './libs/libmysql.dll',
3 => './libs/libmysqlclient16_5.1.41-3ubuntu12_i386.deb',
),
3 =>
array (
0 => './radio_sneaker/cl_auto.lua',
1 => './radio_sneaker/sh_auto.lua',
2 => './radio_sneaker/sh_coms.lua',
3 => './radio_sneaker/sh_info.lua',
4 => './radio_sneaker/sv_auto.lua',
5 => './radio_sneaker/sv_hooks.lua',
),
4 => './sv_auto.lua',
)
Compare this output against the tree command ran on the same folder:
| libmysqlclient.so.16.0.0
| libmysqlclient_r.so.16.0.0
| sv_auto.lua
|
+---libs
| libboost_thread-mt.a
| libboost_thread-mt.so.1.38.0
| libmysql.dll
| libmysqlclient16_5.1.41-3ubuntu12_i386.deb
|
\---radio_sneaker
cl_auto.lua
sh_auto.lua
sh_coms.lua
sh_info.lua
sv_auto.lua
sv_hooks.lua
Why don't you use a join function in php to merge the path to check whether it then exists? If it doesn't then go level by level checking if the folder exists and if it doesn't then create in and move further. My point is that creating such a dynamic structure is, first of all, difficult and easy to mess up. Why not to go the easy way?
Imagine that you have an array representing the directory stucture which is initially empty and will be filled in piece by piece. You need to keep track of a "current" item in this array and iterate over the directory names. At each iteration you would create a sub-array under the current item if it does not already exist and then set the current to be this sub-array.
This can be done either with recursion or with iteration, and since it's PHP the "current" marker would need to be a reference.
With this overview in mind, have a look at this question and the answers there. The input there is in the form of a string, but that's just an implode away from your current situation.
I'll let you wrestle this into your code, but heres the important lessons.
turn the flat array into a nested structure
$a = array(
'dir1', 'dir2', 'file.txt'
);
$structure = array(array_pop($a));
foreach (array_reverse($a) as $dir) {
$structure = array($dir => $structure);
}
print_r($structure);
merge one structure into another
$result = array_merge_recursive($result, $structure);
Just iterate over all structures for the merging.
Related
I am writing a page that pulls images and image data out of a multidimensional array. I need to be able to click a button that calls a function to sort out the images by tags(IE tag_GlassDoor & tag_GlassWall) - basically to show only images that do or do not have that particular element (in this case im using 0 and 1 for yes and no), such as a glass door. I can currently make that array display the data, but I cant figure out how to sort the data by one of the array keys, or even really the syntax to pull a single value out at will.
$arrImages[] =
[
'img_sm'=>'image1.jpg',
'tag_GlassDoor'=>0,
'tag_GlassWall'=>1,
];
$arrImages[] =
[
'img_sm'=>'image2.jpg',
'tag_GlassDoor'=>1,
'tag_GlassWall'=>1,
];
Filtering is the answer, it can be used to filter one dimensional Arrays and multidimensional arrays.
the general implementation would be something like this:
$arr = array(
array(
'image' => "data",
'hasObject' => 1
),
array(
'image' => "data",
'hasObject' => 0
),
);
$finteredArray = array_filter($arr, function ($r) {
return (bool) $r['hasObject'];
});
print_r($finteredArray);
// it outputs:
// Array ( [0] => Array ( [image] => data [hasObject] => 1 ) )
I have 2 arrays. The first array contains the correct word variants and the second array contains incorrect word variants. I want to write them by combining into one as an array, passing in the wrong version of the words to the key and to the value of the correct version of the word. Then write them to a file and use it. I saved the contents of the array in a file, but with a new record to the same file, all the data is cleared and only new entries are re-written and the old entries are deleted automatically. I want that before writing new data to an array, the array is checked for duplication and if such is not written in the file with the array, then let it be written to the file without clearing the previous data of the file. In general, that will select a file or database for storing more than a billion words. Does the database have the best speed or file?
Example first array:
$uncorrect = array
(
0 => "мувосокори",
1 => "мунаггас",
2 => "мангит",
3 => "мангития",
4 => "мунфачир",
5 => "мунфачира",
6 => "манфиатпарасти",
7 => "манфиатчу",
8 => "манфиатчуи",
9 => "манфиатхох",
10 => "манфи",
.....................
);
Example second array:
$correct = array
(
0 => "мувосокорӣ",
1 => "мунағғас",
2 => "манғит",
3 => "манғития",
4 => "мунфаҷир",
5 => "мунфаҷира",
6 => "манфиатпарастӣ",
7 => "манфиатҷӯ",
8 => "манфиатҷӯӣ",
9 => "манфиатхоҳ",
10 => "манфӣ",
.....................
);
I combined two arrays with this code:
$dict = array_combine($uncorrect, $correct);
Example result my array with data here:
$dict = array (
"мувосокори" => "мувосокорӣ",
"мунаггас" => "мунағғас",
"мангит" => "манғит",
"мангития" => "манғития",
"мунфачир" => "мунфаҷир",
"мунфачира" => "мунфаҷира",
"манфиатпарасти" => "манфиатпарастӣ",
"манфиатчу" => "манфиатҷӯ",
"манфиатчуи" => "манфиатҷӯӣ",
"манфиатхох" => "манфиатхоҳ",
"манфи" => "манфӣ",
"минкор" => "минқор",
.....................................
);
I am writing to a file with this code:
file_put_contents("data.json", json_encode($dict));
I will get the array with this code:
$array = json_decode(file_get_contents("data.json"));
You would be better off using a database for this task.
To solve your issue if you decide to keep with the file storage, the reason you are losing old entries is because you forgot to load them before adding new values.
// more or less something like below
$array = json_decode(file_get_contents("data.json"));
$dict = array_combine($incorrect, $correct);
$newArray = array_merge($array, $dict);
file_put_contents("data.json", json_encode($newArray));
This will not be efficient for billions or rows, especially if it's something that gets loaded every time a user loads a page.
Any time you want to add new items just load the file first, then merge the new items in before saving it again. file_put_contents is going to overwrite whatever was there, so you need to get that data before running that function. Something like this:
$array = json_decode(file_get_contents("data.json"));
$newArray = array(
array( "rightWord" => "wrongWord")
);
$finalArray = array_merge($newArray, $array);
file_put_contents("data.json", json_encode($finalArray));
Simple one, but still can't figure out.
I have two arrays of slightly different structures.
The first array contains members (as first level indexes, e.g. 4, 2) and their document ids (as second level indexes, e.g. 2, 3) and department tags for those documents:
array (
4 =>
array (
2 => 'support',
),
2 =>
array (
3 => 'billing',
),
)
The second array's first level index doesn't have any meaning, so could be get rid of. However, the second level index contains member ids (e.g. 4, 2) and department tags those members opened access to (the current user):
array (
0 =>
array (
4 =>
array (
'support' => 'support',
'billing' => 'billing',
),
),
1 =>
array (
2 =>
array (
'support' => 'support',
),
),
)
So I am trying to compile a list of documents that should be displayed to the current user.
For example, since member #4 has given access to support and billing the current user should be able to see document #2 (tagged as support) from that member.
And because member #2 has given access to only support tagged documents, the current user should not be able to see document #3 (tagged as billing).
So the above example should give only:
array(2)
How do I generate the final array of documents in PHP comparing two arrays?
It's possible to do what you want with loops and searches, but I'd consider this data structure unmaintainable and aim for changing it in the first place. Well, sometimes you can't, so here's how I'd do it:
$documents_data =[
4 => [2 => 'support'],
2 => [3 => 'billing']
];
$access_data = [
[4 => ['support' => 'support', 'billing' => 'billing']],
[2 => ['support' => 'support']]
];
// You need current user's data so having his id
// extract his access rights from second array
$user_id = 4;
function userData($user_id, $access_table) {
$access = [];
foreach ($access_table as $user_acc) {
if (key($user_acc) !== $user_id) { continue; }
$access = reset($user_acc);
break;
}
return [
'id' => $user_id,
'access' => $access
];
}
$user = userData($user_id, $access_data);
// Filter out documents (if any) not matching user's access rights
function userDocuments($user, $docs) {
if (empty($docs[$user['id']])) { return []; }
return array_filter(
$docs[$user['id']],
function ($doc_type) use ($user) {
return isset($user['access'][$doc_type]);
}
);
}
$allowed_docs = userDocuments($user, $documents_data);
I am trying to push 2 variables into an array but I want that the key is the same.
The code below is a search function through a folder full of files.
In the foreach, I'm doing a check whether the name or a part of the name matches the search term. If there are results, I put the filename and the filepath in the array.
protected function search()
{
$keyword = $this->strKeyword;
$foundResults = array();
$dir_iterator = new RecursiveDirectoryIterator(TL_ROOT."/tl_files/");
$iterator = new RecursiveIteratorIterator($dir_iterator,
RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $splFile) {
if ($splFile->getBaseName() == $keyword) {
array_push($foundResults, $splFile->getBaseName(), $splFile->getPathName());
}
elseif(stripos($splFile->getBaseName(), $keyword) >= 3){
array_push($foundResults, $splFile->getBaseName(), $splFile->getPathName());
}
}
return $foundResults;
}
When I run the code it gives back the following:
[0] => FileName Output 1
[1] => FilePath Output 1
[2] => FileName Output 2
[3] => FilePath Output 2
As you can see , he set a new key for File name and File Path
But what I want is this:
[0] => Example
(
[fileName] => logo.png
[pathName] => /tes/blalabaa/ddddd/logo.png
)
I hope it's a bit clear, and someone can help me.
Greetz
I suppose you need something like this:
$foundResults[] = array(
'fileName' => $splFile->getBaseName(),
'pathName' => $splFile->getPathName());
Instead of the values you can push an array containing the key-value-pairs:
array_push($foundResults,
array(
'fileName' => $splFile->getBaseName(),
'filePath' => $splFile->getPathName()
)
);
I am trying to build a hierarchical array in PHP, from relational database contents that are stored using a closure table. For a given result, I will have the full path to the LEAF node, the below would look like my result set.
1~root~the root node
1~root~the root node>>>2~category1~First category
1~root~the root node>>>3~category2~Second category
1~root~the root node>>>2~category1~First category>>>4~subCatOfCategory1~SubCategory of Cat 1
Anyway, those are my database results. So I want to traverse them and build a hierarchical structure in PHP so I can convert it to JSON and render a tree in DOJO
So as I walk through each row, I am building a "path" to the leaf because I only need to add an element to tree when the element is a "leaf"... Along that thinking I decided that I would tokenize each result, using ">>>" as the delimiter, giving me the nodes that are in that row. Then I loop through those nodes, tokenizing each one by "~" which gives me the attributes of each node.
So, I have a for loop to process each ROW and it basically determines that if the node being processed is NOT a leaf, add it's ID to an array that is to track the path to get to the eventual leaf that will be processed. THEN, when I finally do arrive at the LEAF, I can call a function to insert a node, using the PATH that I've compiled along the way.
Hopefully that all makes sense.. so I've included the code below.. Consider the second result from above. When I have processed that entire result and am about to call the function insertNodeInTreeV2(), the arrays look as below...
$fullTree is an array with 1 element, indexed at [1]
That element contains an array with four elements: ID(1), NAME(root), Description(the root node), CHILDREN(empty array)
$pathEntries is an array with only one element, (1). That is to mean that the PATH to the LEAF node being inserted is by node [1], which is the root node.
$nodeToInsert is an array with four elements: ID(2), NAME(category1), Description(First Category), CHILDREN(empty array)
$treeRootPattern is a STRING that contains the Variable name I'm using to store the whole array/tree, which in this case is "fullTree".
private function insertNodeInTreeV2( array &$fullTree, array $pathEntries, array $nodeToInsert, $treeRootPattern )
{
$compiledPath = null;
foreach ( $pathEntries as $path ) {
$compiledPath .= $treeRootPattern . '[' . $path . '][\'CHILDREN\']';
}
// as this point $compiledPath = "fullTree[1]['CHILDREN']"
$treeVar = $$compiledPath;
}
So when I make the assignment, $treeVar = $$compiledPath;, I THINK I am setting the variable $treeVar to be equal to $fullTree[1]['CHILDREN'] (which I have verified in my debugger is a valid array index). Even if I paste the contents of $compiledPath into a new Expression in Eclipse debugger, it shows me an empty array, which makes sense because that is what's located in $fullTree[1]['CHILDREN']
But instead, the runtime is telling me the following error...
troller.php line 85 - Undefined variable: fullTree[1]['CHILDREN']
Any help on this would be greatly appreciated... And if you have a better way for me to get from the result set I described to the hierarchical array I'm trying to build, I'd be eager to adopt a better method.
UPDATED TO ADD THE CODE THAT CALLS THE ABOVE FUNCTION -- THE FOR LOOP PROCESSES ROWS OF DATABASE RESULTS, AS DESCRIBED ABOVE
foreach ( $ontologyEntries as $entry ) {
// iterating over rows of '1~~root~~The root node>>>2~~category1~~The first category
$nodes = explode( '>>>', $entry['path'] );
$numNodes = count( $nodes ) - 1 ;
$pathToNewNode = null; // this is the path, based on ID, to get to this *new* node
for ( $level = 0; $level <= $numNodes; $level++ ) {
// Parse the node out of the database search result
$thisNode = array(
'ID' => strtok($nodes[$level], '~~'), /* 1 */
'NAME' => strtok( '~~'), /* Root */
'DESCRIPTION' => strtok( '~~'), /* This is the root node */
'CHILDREN' => array()
);
if ( $level < $numNodes ) { // Not a leaf, add it to the pathToThisNodeArray
$pathToNewNode[] = $thisNode['ID'];
}
else {
// processing a leaf, add it to the array
$this->insertNodeInTreeV2( $$treeRootPattern, $pathToNewNode, $thisNode, $treeRootPattern );
}
}
}
See my comments below your question for an explanation.
$paths = array(
"1~root~the root node",
"1~root~the root node>>>2~category1~First category",
"1~root~the root node>>>3~category2~Second category",
"1~root~the root node>>>2~category1~First category>>>4~subCatOfCategory1~SubCategory of Cat 1"
);
$tree = array();
foreach ($paths as $path)
{
$currentNode = &$tree;
$parts = explode(">>>", $path);
foreach ($parts as $part)
{
$node = explode("~", $part);
// create all nodes along this path
if (!isset($currentNode[$node[0]]))
{
$currentNode[$node[0]] = array(
"ID" => $node[0],
"NAME" => $node[1],
"DESCRIPTION" => $node[2],
"CHILDREN" => array(),
);
}
$currentNode = &$currentNode[$node[0]]["CHILDREN"];
}
}
var_dump($tree);
Outputs:
array
1 =>
array
'ID' => string '1' (length=1)
'NAME' => string 'root' (length=4)
'DESCRIPTION' => string 'the root node' (length=13)
'CHILDREN' =>
array
2 =>
array
'ID' => string '2' (length=1)
'NAME' => string 'category1' (length=9)
'DESCRIPTION' => string 'First category' (length=14)
'CHILDREN' =>
array
4 =>
array
'ID' => string '4' (length=1)
'NAME' => string 'subCatOfCategory1' (length=17)
'DESCRIPTION' => string 'SubCategory of Cat 1' (length=20)
'CHILDREN' => &
array
empty
3 =>
array
'ID' => string '3' (length=1)
'NAME' => string 'category2' (length=9)
'DESCRIPTION' => string 'Second category' (length=15)
'CHILDREN' =>
array
empty
The loop will create all nodes that are included in a path, so you won't need to insert 1~root~the root node, if you also insert 1~root~the root node>>>2~category1~First category.
You can change this by only creating nodes if the node is the path's last node. The length of a path is count($parts) and you can count which level you are in inside the inner foreach-loop.
I hope this is what you wanted.