2

I find I have to do this a lot in PHP: take a value stored in a variable, and put this value in an array with a key that is the same as the variable name. This pattern comes up often with CodeIgniter, but this is not a CI specific question.

What I mean is I often have to do this:

$logs = //get logs from db
$this->load->view( 'showLogs', array('logs'=>$logs) ); //load a CI view

So my question is, is there a magic function / can we write a magic function that saves some keystrokes and improves clarity by simplifying this process?

What I want is this:

$arr = my_magic_function($logs)
// $arr = array('logs'=>$logs);

I would probably call this function "array_self", possibly shortened to "a_self" and to me, it would simply read better. What bugs me is having to type the name, in my example "logs", twice, as this can be a source of frustrating typos.

parker.sikand
  • 1,371
  • 2
  • 15
  • 32
  • If you think about it, the only tricky part of this function would be putting the var name into a string to be the key, in which case see: http://stackoverflow.com/questions/255312/how-to-get-a-variable-name-as-a-string-in-php – Digital Chris Mar 06 '14 at 18:23
  • I think this goes back to the question "In PHP, how can I get the variable name that is passed to a function call?" The best answer I've found is this one: http://stackoverflow.com/a/7782986/155248. You should be able to solve this problem using a more elegant solution that doesn't require accessing global vars or hacking your code. Maybe you can benefit form a DI container like Pimple? http://pimple.sensiolabs.org/ – Onema Mar 06 '14 at 19:17

2 Answers2

1

You can accomplish this using the built-in function compact().

Assuming you have a variable $logs you can create an array by calling:

compact("logs");

Example: https://eval.in/114710

$logs = "there was a blahbalh";

$ary = compact("logs");

var_dump($ary);

returns:

array(1) {
  ["logs"]=>
  string(20) "there was a blahbalh"
}
Digital Chris
  • 6,177
  • 1
  • 20
  • 29
0

Functions in php normally has no access to external variables. Hence you need to provide access somehow.

function magic_array_wrapped($name) {
    $a = array();
    $a[$name] = $GLOBALS[$name];
    return $a;
}

Usage: magic_array_wrapped('logs')

Maybe, it will work with this way, but I'm not sure:

function magic_array_wrapped($name) {
    global $$name;
    $a = array();
    $a[$name] = $$name;
    return $a;
}

PS: It's hard to tell, whether is it a problem in your architecture, but having such need is a sign, that you need to revise architecture.

kirilloid
  • 14,011
  • 6
  • 38
  • 52
  • ya i saw some other thing about hacking on the $GLOBALS array but people said it's not reliable, especially when dealing with scoped variables. Fair point about possible being able architect around this in the first place, but regardless, I feel like this is something ever PHP programmer has to do at some point (put a single variable into an array for compatibility with some function) and it would be nice to have a cleaner way to do so. – parker.sikand Mar 06 '14 at 18:39