I have the 'path' to an array value stored in a variable, and I am trying to set the value. What I am trying to do is this:
$array['Breaks'][1]['In'] = "XXX";
However, I have ['Breaks'][1]['In'] stored in a variable. So I am essentially trying to do something like this:
$path = "['Breaks'][1]['In']";
$array.$path = "XXX";
This doesn't work though, and I'm not exactly sure how to go about making this work correctly.
Any suggestions?
As long as the path string is not modifiable by users or parsed through previously you could just do:
eval("\$array".$path." = 'Value';");
Try doing this,
//This solution works if you are sure the length of $path_arr is going to be 3
$path = "['Breaks'][1]['In']";
$path_arr = explode(']', str_replace(array("['", "'", "["), '', $path));
$array[$path_arr[0]][$path_arr[1]][$path_arr[2]] = "XXX";
var_dump($array);
Demo
Try extracting each component of the $path variable into its own variable, or into an array ($pathArray below). So, if you had $pathArray[0] set to 'Breaks', $pathArray[1] set to 1, etc., you could do something like:
$array[$pathArray[0]][$[pathArray[1]][$[pathArray[2]] = "XXX";
Although this seems like an unusual way to go about things, and it might be worth re-thinking your approach entirely.
Related
I have an include with a single array in it that holds 3 instructions; a "y/n" switch and a start and end date. The include meetingParams.php looks like this:
<?php
$regArray = array("n","2018-03-03","2018-03-07");
?>
I want to update those array values from time to time using a web based form. Where I get stuck is finding the correct syntax to do that. Right now I have the following:
$registration = $_POST['registration'];
$startMeeting = $_POST['startMeeting'];
$endMeeting = $_POST['endMeeting'];
$replace = array($registration, $startMeeting, $endMeeting);
$search = file_get_contents('includes/meetingParams.php');
$parsed = preg_replace('^$regArray.*$', $replace, $search);
file_put_contents("includes/meetingParams.php", $parsed);
When I run this code, the file meetingParams.php get's replaced with an empty file. What am I missing?
This should work fine:
$content = '<?php
$regArray = array("'.$registration.'","'.$startMeeting.'","'.$endMeeting.'");
?>';
file_put_contents("includes/meetingParams.php", $content);
Try this.
include_once "includes/meetingParams.php";
$registration = $_POST['registration'];
$startMeeting = $_POST['startMeeting'];
$endMeeting = $_POST['endMeeting'];
$regArray = array($registration, $startMeeting, $endMeeting);
Explanation
There is no need to use file_get_contents since you are using a PHP file you can simply include it.
What that means is that you are placing that file inside your script. Then there is no need to use RegEx to replace the array, just reassign its value.
I am writing some code to create fields automatically, which will save me a load of time. I have got most of my code working, but I have came across one error with the code, which is preventing me from achieving my final goal.
The code is as follows:
while ($i <= $numFields) {
$type = "\$field{$i}_Data['type']";
$name = "\$field{$i}_Data['name']";
$placeholder = "\$field{$i}_Data['placeholder']";
$value = "\$field{$i}_Data['value']";
echo '<input type="'.$type.'" name="'.$name.'" placeholder="'.$placeholder.'" value="'.$value.'">';
$i++;
}
The $numFields variable is defined at the top of my script, and I have worked out that it is something to do with how I am setting the variables $type, $name etc.
The end result is to create inputs depending on properties set in variables at the top of the script, The only issue I am having is with the settings of the variables, as said above.
If any extra code/information is needed, feel free to ask.
Thank you.
NOTE - There is no physical PHP error, it's purely an error with this:
"\$field{$i}_Data['value']";
There are a few ways we could write this one out, but they are all extensions of variable expansion and/or variable-variables.
Basically, we just need to put the variable name in a string and then use that string as the variable (much like you're currently doing with $i inside the string):
$type = ${"field{$i}_Data"}['type'];
$name = ${"field{$i}_Data"}['name'];
// ...
However, if you don't mind an extra variable, this can be written more cleanly by saving it like so:
$data = ${"field{$i}_Data"};
$type = $data['type'];
$name = $data['name'];
// ...
I have this code for scraping team names from a table
$url = 'http://fantasy.premierleague.com/my-leagues/303/standings/';
$html = #file_get_html($url);
//Cut out the table
$FullTable = $html->find('table[class=ismStandingsTable]',0);
//get the text from the 3rd cell in the row
$teamname = $FullTable->find('td',2)->innertext;
echo $teamname;
This much works.. and gives this output....
Why Always Me?
But when I add these lines..
$teamdetails = $teamname->find('a')->href;
echo $teamdetails;
I get completely blank output.
Any idea why? I am trying to get the /entry/110291/event-history/33/ as one variable, and the Why Always Me? as another.
Instead do this:
$tdhtml = DOMDocument::loadHTML($teamdetails);
$link = $tdhtml->getElementsByTagName('a');
$url = $link->item(0)->attributes->getNamedItem('href')->nodeValue;
$teamdetails = $teamname->find('a')->href;
^^^^^^^^^---- never defined in your code
I also fail to see how your "works" code could possibly work. You don't define $teamname in there either, so all you'd never get is the output of a null/undefined variable, which is...no output all.
Marc B is right, I get that you don't have to initialize a variable, but he is saying you are trying to access a property of said variable:
$teamdetails = $teamname->find('a')->href;
^^^^^^^^^---- never defined in your code
This is essentially:
$teamname = null;
$teamname->find('a')->href;
The problem in your example is that $teamname is a string and you're treating it like a simple_html_dom_node
Depending on the current page, I would like to change the css of the chosen menu module and make others different, etc. All that while building dynamically the page before loading.
My problem is that I have a serie of variables going by this structure :
$css_(NAME_OF_MODULE)
To know what variable must be set , I have another variable I received in parameters of this functions, called
$chosen_menu
Say $chosen_Menu = "C1", I would like to add content to $css_C1. Thus, what I want is to create a variable name out of 2 variables, named $css_C1
I have tried :
${$css_.$chosen_menu} = "value";
But it doesnt seem to work. Any clue ?
That probably won't just work. PHP supports full indirection though, so something like this will work.
$varName = $css."_".$chosen_menu;
$$varName = "value";
If not, it will probably be attempting to interpret $css_ as a variable name in your second code sample, so just change that to $css."_".$chosen_menu.
$nr = 1;
${'name' . $nr} = 20 ;
var_dump($name1);
Check out http://php.net/manual/en/language.variables.php
You should be able to use:
$menu = $css . '_' .$chosen_menu;
$$menu = 'some value';
or
${$menu} = 'some value';
I'm not sure if I get you right, but how about this:
${$css."_".$chosen_menu} = "value";
I would like to know if there is a way to bind PHP function inside a regexp.
Example:
$path_str = '/basket.php?nocache={rand(0,10000)}';
$pattern = ? // something i have no idea
$replacement = ? // something i have no idea
$path = preg_replace($pattern, $replacement, $path_str);
Then :
echo "'$path'";
would produce something like
'/basket.php?nocache=123'
A expression not limited to the 'rand' function would be even more appreciated.
Thanks
You could do the following. Strip out the stuff in between the {} and then run an eval on it and set it to a variable. Then use the new variable. Ex:
$str = "/basket.php?nocache={rand(0,10000)}";
$thing = "rand(0,10000)";
eval("\$test = $thing;");
echo $test;
$thing would be what's in the {} which a simple substr can give you. $test the becomes the value of executing $thing. When you echo test, you get a random number.
Don't, whatever you do, store PHP logic in a string. You'll end up having to use eval(), and if your server doesn't shoot you for it, your colleagues will.
Anywhoo, down to business.
Your case is rather simple, where you need to append a value to the end of a string. Something like this would be sufficient
$stored = '/basket.php?nocache=';
$path = $stored . rand(0,10000);
If, however, you need to place a value somewhere in the middle of a string, or possibly in a variable location, you could have a look at sprintf()
$stored = '/basket.php?nocache=%d&foo=bar';
$path = sprintf($stored, rand(0,10000));
I would not try to store functions in a database. Rather store some kind of field that represents the type of function to use for each particular case.
Then inside your crontab you can do something like:
switch ($function)
{
case 'rand':
$path_str = '/basket.php?nocache='. rand(0,10000);
}
e.t.c