我有一個結構像這樣的語言檔案
$_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
如何使用 PHP 回圈遍歷該檔案,僅獲取每個引號內的符號計數 - 示例:“hello”有 5 個字符,“world”也有 5 個,總和為 10。我需要遍歷所有這些變數并只計數字串字符的長度并將它們相加。任何想法如何做到這一點?立即在我的腦海中出現fopen,foreach回圈explode和類似的東西。
uj5u.com熱心網友回復:
使用 迭代多維陣列和計算字串長度很容易array_walk_recursive(),只要您記住它會修改源陣列。回呼函式只會對標量值進行操作,即。陣列的最后一個葉子。如下:
<?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);
});
這會產生一個陣列,其中包含其值的字串長度:
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)
}
}
}
我不完全確定這是您正在尋找的輸出格式,但是我不知道您的用例是什么。它可以為您提供匹配的字串長度。
uj5u.com熱心網友回復:
由于您沒有說明什么分組,因此。陣列級別,您要對字串的字符進行計數和求和:
$_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);
這將列印:
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
)
uj5u.com熱心網友回復:
這就是你需要的,使用fopen、fgetcsv和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>";
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/421043.html
標籤:
