dpvm7231 2012-11-14 11:10
浏览 26
已采纳

在其他变量名称中使用变量 - PHP

See this:

        $q = 'blah';
        for($k = 0; $k < count($results_array); $k++){
            $results_array_ . $k = explode(',', $results_array[$k]);
            foreach($results_array_ . $k as $key => $value){
                if (stripos($value, $q) === false) {
                unset($results_array_ . $k[$key]);
                break;
                }
            }
        }

On line 3 I'm simply using "$results_array_ . $k" and it's working just fine, but on line 6 I'm getting PHP parse errors on "unset($results_array_ . $k[$key])", why is this happening?

I appreciate anykind of help


Why I'm doing it:

I have an array named results_array:

var_dump($results_array):
0 => php,mysql,jquery,ruby,html,css,lamp
1 => mouse,keyboard,laptop,pad
2 => table,sofa,caption

and I have a $q which stands for query, I want to search in the $results_array and remove the items which has nothing to do with the query, so if I set $q=a then results array should be this:

0 => lamp
1 => keyboard,laptop,pad
3 => table,sofa,caption

now, I want to put the above results in each index of the results_array, at the end results_array should be:

0 => lamp
1 => keyboard
2 => laptop
3 => pad
4 => table
5 => sofa
6 => caption
  • 写回答

1条回答 默认 最新

  • duankangpazhuo0347 2012-11-14 11:14
    关注

    Answer to original question

    unset expects its argument to be a direct reference to a value, e.g. $var or $array['key']. If you want to dynamically create the argument based on other values, you 'll have to use variable variable syntax:

    unset(${$results_array_ . $k[$key]});
    

    This will get rid of the warning, but it still won't make the code work because it's fundamentally flawed. Line 3 which you mention reads:

    $results_array_ . $k = explode(',', $results_array[$k]);
    

    What this does is explode an array into $k and then concatenate $results_array_ with $k and... throw away the result. You could just as easily have written

    $k = explode(',', $results_array[$k]);
    

    and it would work the same (except possibly not giving an E_NOTICE that $_results_array_ does not exist).

    So, it seems that you have a misunderstanding of how some PHP fundamentals work. It would be best if you asked another question that explains what you are trying to do, in order to determine what would be a good way of doing it.

    Answer to current question

    Let's take the steps one at a time:

    1. Take the array of strings and turn each string into an array with explode, making an array of arrays. You can do this with array_map or a simple foreach.
    2. "Flatten" the array of arrays into one big array. You can do this with array_merge or array_merge_recursive (the details will be a bit different, the idea is the same).
    3. Search the flattened array and filter out uninteresting elements with array_filter.
    4. If necessary, reindex the filtered array so that it has consecutive numeric keys with array_values.

    Here's code that does this, albeit a little differently (I am doing steps 1 and 2 at the same time in the first line using array_reduce):

    $array = (...);
    $array = array_reduce($array, function(&$result, $item) {
        return array_merge($result, explode(',', $item));
    }, array());
    $array = array_filter($array, function($item) use ($string) { 
        return strpos($item, $string) !== false;
    });
    $result = array_values($array);
    

    A version that does the same without using fancy functions:

    // Step 1
    foreach($array as &$row) {
        $row = explode(',', $row);
    }
    unset($row);
    
    // Step 2
    $array = call_user_func_array('array_merge_recursive', $array);
    
    // Step 3
    foreach ($array as $k => $v) {
        if(strpos($v, 'a') === false) unset($array[$k]);
    }
    
    // Step 4
    $array = array_values($array);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题