$name= path1
$key= key1
public static function getpath($name, $key)
{
if (!self::is_valid($name, $key)) return false;
$path = '/var/lib/fdata/'.$name.'/'.$key;
if (!is_file($path)) return false;
return $path;
}
public static function get($name, $key)
{
$path = self::getpath($name, $key);
if ($path === false) return false;
return file_get_contents($path);
}
$configmap = unserialize(fdata::get('base', 'key'));
The questions is:
if variable $path = self::getpath($name, $key); then $path = ? and what mean self::
if variable $configmap = unserialize(fdata::get('base', 'key')); then $configmap = ? and what mean fdata::
1) self::getpath(...) should return a boolean value or a string. self:: refers to a current class.
2) Here's the man for unserialize
3)
fdata:: denotes the namespace fdata or perhaps a class.
Related
I want to read a variable from inside a weird function that I copied from this git .
This is the original function:
$values = array_map(function ($value) use ($connection) {
if ($value===null) return null;
// return mysqli_real_escape_string($connection,(string)$value);
return pg_escape_string($connection,(string)$value);
},array_values($input));
and I changed it into this in order to adapt it to my needs (file upload)
$values = array_map(function ($value) use ($connection) {
if ($value === null)
return null;
if (gettype($value) === "array"){
$tmpname=$value['tmp_name'];
$value=$value['name'];
}
return mysqli_real_escape_string($connection, (string) $value);
}, array_values($input));
The problem is that I can't read $tmpname from outside this function.
Can anyone help me?
So the answer as #rtfm said is to set a global var like this
$values = array_map(function ($value) use ($connection) {
if ($value === null)
return null;
if (gettype($value) === "array"){
global $tmpname;
$tmpname=$value['tmp_name'];
$value=$value['name'];
}
return mysqli_real_escape_string($connection, (string) $value);
}, array_values($input));
what is the best way to put parents value in the same array ( Concate ) as this code just return one value
public function divisionParent($name)
{
$path = array();
$path[] = $name;
$div = CsiCategory::where('name', $name)->first();
$parent_id = $div->parent_id;
if ($parent_id != 0) {
$name = CsiCategory::where('id', $parent_id)->first();
$this->divisionParent($name->name);
}
return $path;
}
Something like this maybe?
public function divisionParent($name, $path = [])
{
// Append to $path arr.
array_push($path, $name);
// Get division by name.
$div = CsiCategory::where('name', $name)->first();
// Check for existing parent division by id.
if (isset($div)) {
if ($div->parent_id != 0) {
$divParent = CsiCategory::where('id', $div->parent_id)->first();
}
}
return isset($divParent) ? $this->divisionParent($divParent->name, $path) : $path;
}
I got a problem.
I make a function to update my config.json file.
The problem is, my config.json is a multdimensional array. To get a value of a key i use this function:
public function read($key)
{
$read = explode('.', $key);
$config = $this->config;
foreach ($read as $key) {
if (array_key_exists($key, $config)) {
$config = $config[$key];
}
}
return $config;
}
I also made a function to update a key. But the problem is if i make update('database.host', 'new value'); it dont updates only that key but it overrides the whole array.
This is my update function
public function update($key, $value)
{
$read = explode('.', $key);
$config = $this->config;
foreach ($read as $key) {
if (array_key_exists($key, $config)) {
if ($key === end($read)) {
$config[$key] = $value;
}
$config = $config[$key];
}
}
print_r( $config );
}
my config.json looks like this:
{
"database": {
"host": "want to update with new value",
"user": "root",
"pass": "1234",
"name": "dev"
},
some more content...
}
I have a working function but thats not really good. I know that the max of the indexes only can be three, so I count the exploded $key and update the value:
public function update($key, $value)
{
$read = explode('.', $key);
$count = count($read);
if ($count === 1) {
$this->config[$read[0]] = $value;
} elseif ($count === 2) {
$this->config[$read[0]][$read[1]] = $value;
} elseif ($count === 3) {
$this->config[$read[0]][$read[1]][$read[3]] = $value;
}
print_r($this->config);
}
Just to know: the variable $this->config is my config.json parsed to an php array, so nothing wrong with this :)
After I had read your question better I now understand what you want, and your read function, though not very clear, works fine.
Your update can be improved though by using assign by reference & to loop over your indexes and assign the new value to the correct element of the array.
What the below code does is assign the complete config object to a temporary variable newconfig using call by reference, this means that whenever we change the newconfig variable we also change the this->config variable.
Using this "trick" multiple times we can in the end assign the new value to the newconfig variable and because of the call by reference assignments the correct element of the this->config object should be updated.
public function update($key, $value)
{
$read = explode('.', $key);
$count = count($read);
$newconfig = &$this->config; //assign a temp config variable to work with
foreach($read as $key){
//update the newconfig variable by reference to a part of the original object till we have the part of the config object we want to change.
$newconfig = &$newconfig[$key];
}
$newconfig = $value;
print_r($this->config);
}
You can try something like this:
public function update($path, $value)
{
array_replace_recursive(
$this->config,
$this->pathToArray("$path.$value")
);
var_dump($this->config);
}
protected function pathToArray($path)
{
$pos = strpos($path, '.');
if ($pos === false) {
return $path;
}
$key = substr($path, 0, $pos);
$path = substr($path, $pos + 1);
return array(
$key => $this->pathToArray($path),
);
}
Please note that you can improve it to accept all data types for value, not only scalar ones
Given a string in the format of "one/two" or "one/two/three", with / as delimiters, I need to get the value (as a reference) in a 2d array. In this specific case, I'm accessing the $_SESSION variable.
In the SessionAccessor::getData($str) function, I don't know what to put in there to make it parse the delimited string and return the array item. The idea is I won't know which array key I'm accessing. The function will be generic.
class SessionAccessor {
static &function getData($str) {
// $str = 'one/two/three'
return $_SESSION['one']['two']['three'];
}
}
/** Below is an example of how it will be expected to work **/
/**********************************************************/
// Get a reference to the array
$value =& SessionAccessor::getData('one/two/three');
// Set the value of $_SESSION['one']['two']['three'] to NULL
$value = NULL;
Here's the solution provided by #RocketHazmat here at StackOverflow:
class SessionAccessor {
static function &getVar($str) {
$arr =& $_SESSION;
foreach(explode('/',$str) as $path){
$arr =& $arr[$path];
}
return $arr;
}
}
From what you've described, it sounds like this is what you're after;
&function getData($str) {
$path = explode('/', $str);
$value = $_SESSION;
foreach ($path as $key) {
if (!isset($value[$key])) {
return null;
}
$value = $value[$key];
}
return $value;
}
EDIT
To also allow for the values to be set, it would be best to use a setData method. This should hopefully do what you hoped for;
class SessionAccessor {
static function getData($str) {
$path = explode('/', $str);
$value = $_SESSION;
foreach ($path as $key) {
if (!isset($value[$key])) {
return null;
}
$value = $value[$key];
}
return $value;
}
static function setData($str, $newValue) {
$path = explode('/', $str);
$last = array_pop($path);
$value = &$_SESSION;
foreach ($path as $key) {
if (!isset($value[$key])) {
$value[$key] = array();
}
$value = &$value[$key];
}
$value[$last] = $newValue;
}
}
I have a function called get_config() and an array called $config.
I call the function by using get_config('site.name') and am looking for a way to return the value of $config so for this example, the function should return $config['site']['name'].
I'm nearly pulling my hair out - trying not to use eval()! Any ideas?
EDIT: So far I have:
function get_config($item)
{
global $config;
$item_config = '';
$item_path = '';
foreach(explode('.', $item) as $item_part)
{
$item_path .= $item."][";
$item = $config.{rtrim($item_path, "][")};
}
return $item;
}
This should work:
function get_config($config, $string) {
$keys = explode('.', $string);
$current = $config;
foreach($keys as $key) {
if(!is_array($current) || !array_key_exists($key, $current)) {
throw new Exception('index ' . $string . ' was not found');
}
$current = $current[$key];
}
return $current;
}
you could try something like...
function get_config($item)
{
global $config;
return $config[{str_replace('.','][',$item)}];
}
Inside your get_config function, you can parse the string using explode function in php
function get_config($data){
$pieces = explode(".", $data);
return $config[$pieces[0]][$pieces[1]];
}