PHP从带有Key值的数组中获取值

时间:2023-02-09 21:37:16

following problem: I want to build a mysql based language system.

下面的问题:我想构建一个基于mysql的语言系统。

To decrease loading time and increase performance I thought it would be the best, to first fetch all Language phrases into an array (which happens with mysqli fetch array). Then, if a language term is required, calling a function with the array as global variable.

为了减少加载时间并提高性能,我认为最好是首先将所有语言短语提取到一个数组中(mysqli fetch array就是这样的)。然后,如果需要一个语言术语,则调用具有数组作为全局变量的函数。

My function for the array:

我的数组函数:

$sql       = $db->query('SELECT * FROM lang_phrases WHERE phraseLanguageId = 1');
$phrases   = array();

while($row = $sql->fetch_array()) {
    $phrases[] = $row;
}

Now I've got this array:

现在我有了这个数组:

array(2) { 
    [0]=> array(8) { 
        [0]=> string(1) "1" 
        ["phraseId"]=> string(1) "1"
        [1]=> string(1) "1" 
        ["phraseLanguageId"]=> string(1) "1" 
        [2]=> string(4) "HOME" 
        ["phraseKey"]=> string(4) "HOME" 
        [3]=> string(10) "Homepage" 
        ["phraseContent"]=> string(10) "Homepage" 
    } 
    [1]=> array(8) { 
        [0]=> string(1) "2" 
        ["phraseId"]=> string(1) "2" 
        [1]=> string(1) "1" 
        ["phraseLanguageId"]=> string(1) "1" 
        [2]=> string(4) "BACK" 
        ["phraseKey"]=> string(4) "BACK" 
        [3]=> string(6) "Back" 
        ["phraseContent"]=> string(6) "Back" 
    } 
}

And thats my function:

这是我的功能:

function l($key) {
    global $phrases;

    return PLACEHOLDER;
}

I wonder, how can I now search the array for "phraseKey" $key from the function and get the value? Do you have any hints? Or shall I simply select each phrase with each one query (performance problems with 100 quers per load?!)?

我想知道,现在如何从函数中搜索“phraseKey”$key的数组并获得值?你有什么提示吗?还是我只需为每个查询选择每个短语(每负载100个quers的性能问题?!)?

Cheers and Thanks!

欢呼和谢谢!

2 个解决方案

#1


2  

May I suggest to retrive the phrases with

我可以建议你把这些短语重新用一下吗

while($row = $sql->fetch_array()) {
    $phrases[$row['phraseKey']] = $row;
}

Then in your function l($key) you can search them with

然后在函数l($key)中可以搜索它们

$t = isset($phrases[$key]) ? $phrases[$key] : null

#2


3  

That's a bad structure. Why not key your array with the phrase ID and the language? e.g.

这是一个糟糕的结构。为什么不在数组中键入短语ID和语言呢?如。

$translations = array(
    'HOME' => array(
       'english' => 'Home',
       'french' => 'Maison',
       'german' => 'Haus"
    etc...

This eliminates the need for ANY searching. You just look up directly by language/phrase ID.

这样就不需要进行任何搜索。你只需通过语言/短语ID直接查找。

#1


2  

May I suggest to retrive the phrases with

我可以建议你把这些短语重新用一下吗

while($row = $sql->fetch_array()) {
    $phrases[$row['phraseKey']] = $row;
}

Then in your function l($key) you can search them with

然后在函数l($key)中可以搜索它们

$t = isset($phrases[$key]) ? $phrases[$key] : null

#2


3  

That's a bad structure. Why not key your array with the phrase ID and the language? e.g.

这是一个糟糕的结构。为什么不在数组中键入短语ID和语言呢?如。

$translations = array(
    'HOME' => array(
       'english' => 'Home',
       'french' => 'Maison',
       'german' => 'Haus"
    etc...

This eliminates the need for ANY searching. You just look up directly by language/phrase ID.

这样就不需要进行任何搜索。你只需通过语言/短语ID直接查找。