I have a language file that is structured like this
$_LANG['en']['homepage']['hello'] = "hello";
$_LANG['en']['homepage']['world'] = "world";
... MANY MANY VARIABLES
$_LANG['es']['homepage']['hello'] = "Hola";
$_LANG['es']['homepage']['world'] = "Mundo";
... MANY MANY VARIABLES
how can I loop through that file using PHP, get only the symbols count inside each quotes - Example:
"hello" has 5 characters and "world" has 5 too which sum is 10. I need to loop through all these variables and count only the string characters length and sum them. Any ideas how to do that? Instantly in my head appears fopen, foreach loop, explode and stuff like that.
CodePudding user response:
Iterating a multidimensional array and e.g. counting string lengths is easy enough with array_walk_recursive(), as long as you remember that it modifies the source array. The callback function will only operate on scalar values, ie. the final leaves of the array. As follows:
<?php
$_LANG['en']['homepage']['hello'] = "hello";
$_LANG['en']['homepage']['world'] = "world";
$_LANG['es']['homepage']['hello'] = "Hola";
$_LANG['es']['homepage']['world'] = "Mundo";
// Copy the array so we don't overwrite:
$sums = $_LANG;
// Walk recursively over the $sums array, with $word passed as reference:
array_walk_recursive($sums, function(&$word) {
$word = strlen($word);
});
This results in an array with the string lengths for its values:
array(2) {
["en"] · array(1) {
["homepage"] · array(2) {
["hello"] · int(5)
["world"] · int(5)
}
}
["es"] · array(1) {
["homepage"] · array(2) {
["hello"] · int(4)
["world"] · int(5)
}
}
}
I'm not entirely certain this is the output format you're looking for, but then again I don't know what your use case is. It gets you the matching string lengths alright.
CodePudding user response:
Since you do not state on what grouping, resp. array level, you want to count and sum the characters of the strings:
$_LANG['en']['homepage']['hello'] = "hello";
$_LANG['en']['homepage']['world'] = "world";
$_LANG['en']['website']['other'] = "ljkasdlkj";
$_LANG['es']['homepage']['hello'] = "Hola";
$_LANG['es']['homepage']['world'] = "Mundo";
$_LANG['de']['website']['something'] = "Abc";
$_LANG['de']['website']['other'] = "Defghijklm";
// –––––
$resultLevel3 = [];
foreach ($_LANG as $key1 => $value1) {
foreach ($value1 as $key2 => $value2) {
foreach ($value2 as $key3 => $value3) {
$resultLevel3[$key1 . '–' . $key2 . '–' . $key3] = strlen($value3);
}
}
}
print_r($resultLevel3);
// –––––
$resultLevel2 = [];
foreach ($_LANG as $key1 => $value1) {
foreach ($value1 as $key2 => $value2) {
$resultLevel2[$key1 . '–' . $key2] = strlen(implode('', $value2));
}
}
print_r($resultLevel2);
// –––––
$resultLevel1 = [];
foreach ($_LANG as $key1 => $value1) {
$sum = 0;
foreach ($value1 as $key2 => $value2) {
$sum = strlen(implode('', $value2));
}
$resultLevel1[$key1] = $sum;
}
print_r($resultLevel1);
This will print:
Array
(
[en–homepage–hello] => 5
[en–homepage–world] => 5
[en–website–other] => 9
[es–homepage–hello] => 4
[es–homepage–world] => 5
[de–website–something] => 3
[de–website–other] => 10
)
Array
(
[en–homepage] => 10
[en–website] => 9
[es–homepage] => 9
[de–website] => 13
)
Array
(
[en] => 19
[es] => 9
[de] => 13
)
CodePudding user response:
This is what you need, using fopen, fgetcsv and foreach:
$filesPath = 'https://path to my file/test.txt';
// this array we will fill with what we read from file
$linesArray = [];
// if we can open file and while we read each line we fill our array
// NOTE THE DELIMITER IN fgetcsv() !!! You can change it!
// this is based on the example strings you gave us
if (($handle = fopen($filesPath, 'r')) !== false) {
while (($data = fgetcsv($handle, 1000, "=")) !== false) {
$linesArray[] = $data;
}
fclose($handle);
}
// We loop through our array and add length of each string to
// totalCount variable
$totalCount = 0;
foreach ($linesArray as $array) {
$t = strtok($array[1], ';');
echo "<pre>";
echo "<b>".__FILE__."</b><br/>";
var_dump($t);
var_dump(strlen($t));
echo "</pre>";
$totalCount = strlen($t);
}
echo "<pre>";
echo "<b>".__FILE__."</b><br/>";
var_dump($totalCount);
echo "</pre>";
