behz4d
behz4d

Reputation: 1849

using variables in other variables name - 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

Upvotes: 0

Views: 69

Answers (1)

Jon
Jon

Reputation: 437336

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);

Upvotes: 2

Related Questions