I use the following function to verify if a search word is in the name of files of my folder.
$files2=list_files("documents/minelli");
Class Commentaire_filter{
static function test_print($item, $key, $value)
{
if (preg_match("#".$value."#", $item))
{
$array = Array($key=>$item);
print_r($array);
?>
<?php echo $key.' '. $item; ?><br />
<?php
}
}
}
array_walk_recursive($files2, 'Commentaire_filter::test_print',$motrecherche );
I obtain a list of files.
I would like add a link to permit users to download the file.
When i'm using the array_walk_recursive function, i can only get the name of file and the key. How can i get the name of parent arrays to made the link ?
Here an extract of my $files:
array (size=5)
'Administratifs' =>
array (size=5)
0 => string 'campagne-sanmarina.jpg' (length=22)
1 => string 'COSMO Echantillons ETE 2009.xls' (length=31)
2 => string 'COSMOPARIS Echantillons MARO Hiver 2011 311-411.xls' (length=51)
3 => string 'cosmoparis-boutique.jpg' (length=23)
4 => string 'minelli-20-ans.png' (length=18)
'Commerce' =>
array (size=4)
0 => string 'a-gagner-cosmoparis.jpg' (length=23)
1 => string 'CONTROLE 2009.pdf' (length=17)
2 => string 'cosmoparis-boutique.jpg' (length=23)
3 => string 'soldes-cosmoparis.jpg' (length=21)
'Gestion' =>
array (size=1)
'PROCEDURES' =>
array (size=5)
0 => string 'cosmoparis-boutique.jpg' (length=23)
1 => string 'flux-ecommerce-smc.pdf' (length=22)
2 => string 'Minelli-Lyon.jpg' (length=16)
3 => string 'sanmarina-magasin-interieur.jpg' (length=31)
4 => string 'visuel_chaussures_minelli_printemps_ete_2009.jpg' (length=48)
'Magasins' =>
array (size=2)
0 => string 'COSMO Echantillons ETE 2009.xls' (length=31)
1 => string 'san-marina-saint-etienne.jpg' (length=28)
'Ressources Humaines' =>
array (size=7)
'ACTUALITES PAYE' =>
array (size=3)
0 => string 'COSMOPARIS Echantillons MARO Hiver 2011 311-411.xls' (length=51)
1 => string 'cosmoparis-boutique25.jpg' (length=23)
2 => string 'minelli-tours.jpg' (length=17)
...
For 'cosmoparis-boutique25.jpg', i would like to get the parent array name ('Ressources Humaines'=>'ACTUALITES PAYE'). How can i get this information to build a link like 'myfolder/Ressources Humaines/ACTUALITES PAYE/cosmoparis-boutique25.jpg ?
Thank you for your help!
You will have to ditch array_walk_recursive and roll your own recursive walk. This will allow you to maintain or pass custom state information whenever you recurse.
For example:
function my_walk_recursive(array $array, $path = null) {
foreach ($array as $k => $v) {
if (!is_array($v)) {
// leaf node (file) -- print link
$fullpath = $path.$v;
// now do whatever you want with $fullpath, e.g.:
echo "Link to $fullpath\n";
}
else {
// directory node -- recurse
my_walk_recursive($v, $path.'/'.$k);
}
}
}
Related
I have an array $indexedarray
printr($indexedarray) gives something like this
array (size=3)
0 => string 'Homes' (length=5)
1 => string 'Apartments' (length=10)
2 => string 'Villas' (length=6)
I want to change this arrays index also same as value, like
array (size=3)
'Homes' => string 'Homes' (length=5)
'Apartments' => string 'Apartments' (length=10)
'Villas' => string 'Villas' (length=6)
is it posssible??
You can use array_combine:
$indexedarray= ['Homes', 'Apartments', 'Villas'];
print_r(array_combine($indexedarray, $indexedarray));
Gives:
Array
(
[Homes] => Homes
[Apartments] => Apartments
[Villas] => Villas
)
But be aware that your duplicate values will be dropped. Keys will be unique!
Try This :
$myArray = [
0 => 'Homes',
1 => 'Apartments',
2 => 'Villas' ];
$newArray = [];
foreach($myArray as $key => $value){
$newArray[$value] = $value;
}
var_dump($newArray);
here is $_POST['members'] and I want to explode it with |
[members] => Array
(
[0] => test.com|test Melissa
[1] => eboo#abcd.com.au|Buckley test
[2] => testtest#test.com.au|test Ashley
[3] => testset.com.au|Forno test
[4] => get.com.au|test Nathan
[5] =>set.com.au|Brown test
)
I am trying with follows php code
$get=explode('|',$_POST['members']);
echo '<pre>';
print_r($get);
try something like
foreach($_POST['members'] as $str){
$get[] = explode('|',$str);
}
print_r($get);
as $_POST['members'] is array, you need to use explode in foreach by accessing all array elements:
foreach($_POST['members'] as $members)
{
$get=explode('|',$members);
echo '<pre>'; print_r($get);
}
Loop Through the Data Array, store the result of your Explode in a new Array and perhaps you got what you want like so:
<?php
$arr = [
0 => "test.com|test Melissa",
1 => "eboo#abcd.com.au|Buckley test",
2 => "testtest#test.com.au|test Ashley",
3 => "testset.com.au|Forno test",
4 => "get.com.au|test Nathan",
5 => "set.com.au|Brown test"
];
$arrSubData = array();
foreach($arr as $pipeDividedString){
$arrSubData[] = explode('|', $pipeDividedString);
}
var_dump($arrSubData);
DUMPS
array (size=6)
0 =>
array (size=2)
0 => string 'test.com' (length=8)
1 => string 'test Melissa' (length=12)
1 =>
array (size=2)
0 => string 'eboo#abcd.com.au' (length=16)
1 => string 'Buckley test' (length=12)
2 =>
array (size=2)
0 => string 'testtest#test.com.au' (length=20)
1 => string 'test Ashley' (length=11)
3 =>
array (size=2)
0 => string 'testset.com.au' (length=14)
1 => string 'Forno test' (length=10)
4 =>
array (size=2)
0 => string 'get.com.au' (length=10)
1 => string 'test Nathan' (length=11)
5 =>
array (size=2)
0 => string 'set.com.au' (length=10)
1 => string 'Brown test' (length=10)
$_POST['members'] is an array and explode(); works on strings. You'll have to loop through the array and explode each value. Something like this :
$arr = array(
'test.com|test Melissa',
'eboo#abcd.com.au|Buckley test',
'testtest#test.com.au|test Ashley',
'testset.com.au|Forno test',
'get.com.au|test Nathan',
'set.com.au|Brown test'
);
$get = array();
foreach ($_POST['members'] as $member) {
$get[] = explode('|',$member);
}
echo '<pre>'; print_r($get);
In order to explode the values you need to loop trough $_POST['members'], for that you can use foreach().
The following, is an example that uses explode() and list() :
<?php
$members = !empty($_POST['members']) ? $_POST['members'] : die("post members is empty");
foreach($members as $member)
{
list($siteEmail, $name) = explode("|", $member);
echo "<pre> $siteEmail $name </pre>";
}
http://ideone.com/qdDmSe
You can use like this
foreach($_POST['members'] as $val){
$get = explode('|',$val);
}
echo "<pre>";print_r($get);
I am reading data from a .csv (which originally comes from an external database) file with fgetcsv. The first line of the .csv file contains the column names, the following rows contain the data.
What I got so far is an array with the column names, and a second array with the rows of of the .csv file (each row as an array).
How can I convert the second array to an associative array with keys being the column names from the first array?
// this is for Mac OS X
ini_set("auto_detect_line_endings", true);
// the name of the file to read
$fileName = 'WebArtikel_V1-modified.csv';
// open file (get handle to file)
$file = fopen($fileName, 'r');
// the array to store the csv's data
$rawData = array();
// the first line of the csv
$header = array();
$i = 0;
while (($line = fgetcsv($file, 4096, ";")) !== FALSE) {
//$line is an array of the csv elements
//print_r($line);
if($i == 0){
$header[] = $line;
} else {
$rawData[] = $line;
}
$i++;
}
fclose($file);
Here is the var_dump($header):
array (size=1)
0 =>
array (size=50)
0 => string 'KHK_EAN' (length=7)
1 => string 'StyleNumber' (length=11)
2 => string 'StyleName' (length=9)
3 => string 'Setname' (length=7)
4 => string 'ColorNumber' (length=11)
5 => string 'ColorName' (length=9)
6 => string 'StyleSize' (length=9)
7 => string 'KHK_Artikel' (length=11)
8 => string 'PriceNew' (length=8)
9 => string 'PriceSale' (length=9)
Note: there is a toplevel array with 1 element which contains another array with the column names.
The $rawData array:
array (size=3604)
0 =>
array (size=50)
0 => string '4055765008989' (length=13)
1 => string '201001001' (length=9)
2 => string ' ' (length=1)
3 => string 'ACCESS 3' (length=8)
4 => string '2942' (length=4)
5 => string 'Blau/Marine' (length=11)
6 => string '1' (length=1)
7 => string '201001001-2942-1' (length=16)
8 => string '199,9' (length=5)
9 => string '199,9' (length=5)
1 =>
array (size=50)
0 => string '4055765008996' (length=13)
1 => string '201001001' (length=9)
2 => string ' ' (length=1)
3 => string 'ACCESS 3' (length=8)
4 => string '3924' (length=4)
5 => string 'Beige/Braun' (length=11)
6 => string '1' (length=1)
7 => string '201001001-3924-1' (length=16)
8 => string '199,9' (length=5)
9 => string '199,9' (length=5)
2 =>
array (size=50)
0 => string '4055765009047' (length=13)
1 => string '201002001' (length=9)
2 => string ' ' (length=1)
3 => string 'ACCESS 3' (length=8)
4 => string '2942' (length=4)
5 => string 'Blau/Marine' (length=11)
6 => string '1' (length=1)
7 => string '201002001-2942-1' (length=16)
8 => string '179,9' (length=5)
9 => string '179,9' (length=5)
Note: same as above, nested arrays, so I can't use array_combine().
Any suggestions?
Is this the same question as here? Copying it below for convenience.
how do i parse a csv file to grab the column names first then the rows that relate to it?
For reading it all at once you can use:
$csv = array_map("str_getcsv", file("file1.csv",FILE_SKIP_EMPTY_LINES));
$keys = array_shift($csv);
To turn all the rows into a nice associative array you could then apply:
foreach ($csv as $i=>$row) {
$csv[$i] = array_combine($keys, $row);
}
I want to do :
Check if array contains array.
If parent array contains child array then interchange the key and value of child array.
Update all keys of child array to '1'. i.e. value of child array after interchange key and value
Delete child array and merge its element to parent array.
Example:
Generated array:
array
'first_name' => string 'sushil' (length=6)
'last_name' => string 'asfasfaf' (length=8)
'gen' => string 'Male' (length=4)
'language' => string 'PHP' (length=3)
'biodata' => string 'sfsafsaf hdffd ' (length=15)
'hobbies' =>
array
0 => string 'gaming' (length=6)
1 => string 'football' (length=8)
2 => string 'cricket' (length=7)
'academic_qualification' => string 'Bachelor' (length=8)
I want to modify as above step:
//finds if child array exists. If exists interchange key and value and update value to '1'.
array
'gaming' => int 1
'football' => int 1
'cricket' => int 1
And finally unset original child array and merge modified child array's element to parent array.
My expected array form:
array
'first_name' => string 'sushil' (length=6)
'last_name' => string 'asfasfaf' (length=8)
'gen' => string 'Male' (length=4)
'language' => string 'PHP' (length=3)
'biodata' => string 'sfsafsaf hdffd ' (length=15)
'academic_qualification' => string 'Bachelor' (length=8)
'gaming' => int 1
'football' => int 1
'cricket' => int 1
I tried like following but its not working:
$submited_data = $_POST;
var_dump($submited_data);
foreach($submited_data as $value){
if(is_array($value)) { //checks if array contains array.
$a = array_flip($value); //then interchange
var_dump($a);
foreach($a as $key=>$b){
$a[$key] = 1; //update all value to '1'.
}
array_push($submited_data,$a); // here is the problem I cannot proceed to furthur step. Please help me. How to merge modified child array to parent array.
var_dump($a);
}
}
Thank You.
Ive just rewritten your code a bit. Please tell me if anything goes wrong!
<?php
$submited_data = $_POST;
foreach($submited_data as $key => $data)
{
if(is_array($data))
{
foreach($data as $sub_data)
{
$submited_data[$sub_data] = 1;
}
unset($submitted_data[$key]);
}
}
suppose $array is the array with child array,
$mergedArray = [];
array_walk_recursive($array, function($a,$b) use (&$mergedArray) { $mergedArray[$b] = $a; });
echo '<pre>';
print_r($mergedArray);
echo '</pre>';
Right now i'm trying to validate some postdata with filter_var(). I want to get the filter related to each input from my database. So if the input should be filtered by EMAIL, the variable would contain FILTER_VALIDATE_EMAIL. This would then be passed like so:
foreach($this->postdata as $key => $input){
if(!((empty($requirements[$key][1])) || $requirements == 'allowed')){
if(filter_var($input, $requirements[$key][1]) === false){
$errors = true;
}
}
}
The $postdata looks like this:
array (size=4)
'personer_navn' =>
array (size=1)
0 => int 0
'personer_alder' =>
array (size=1)
0 => int 1
'personer_kon' =>
array (size=2)
0 => int 2
1 => int 3
'personer_by' =>
array (size=1)
0 => int 4
And the $requirements looks like this:
array (size=4)
'personer_navn' =>
array (size=4)
0 => string 'string' (length=6)
1 => string 'FILTER_VALIDATE_EMAIL' (length=21)
2 => string '' (length=0)
3 => string '' (length=0)
'personer_alder' =>
array (size=4)
0 => string 'int' (length=3)
1 => string 'FILTER_VALIDATE_EMAIL' (length=21)
2 => string '' (length=0)
3 => string '' (length=0)
'personer_kon' =>
array (size=4)
0 => string 'allowed' (length=7)
1 => string 'allowed' (length=7)
2 => string 'allowed' (length=7)
3 => string 'allowed' (length=7)
'personer_by' =>
array (size=4)
0 => string 'string' (length=6)
1 => string 'FILTER_VALIDATE_EMAIL' (length=21)
2 => string '' (length=0)
3 => string '' (length=0)
Again the problem seems to ba passing $requirements[$key][1] to the filter_var() function.
Any help is appreciated.
FILTER_VALIDATE_EMAIL is not to be used as a string. Try using it without the quotes. Examples can be found in the documentation
A simple change that will fix the bug
foreach($this->postdata as $key => $input){
if(!((empty($requirements[$key][1])) || $requirements == 'allowed')){
if(filter_var($input, constant( $requirements[$key][1]) ) === false){
$errors = true;
}
}
}
The constant function returns the (integer) value of the filter that is string.
read P.P-s answer too.