I have a bunch of arrays that I'd like to var_dump. These arrays are named based off of a $_GET from a form, so each are different but have a pre defined name attached to the start so I might have array_bob, array_mary, array_sam where bob mary and sam is the $_GET value.
I thought using preg_match would be my best bet but I just don't know how to go about doing it
I thought something like this but it obviously doesn' work
if (isset($array_(preg_match("/[A-Z]|[0-9]/i",$array_))))
{
var_dump($array_(preg_match("/[A-Z]|[0-9]/i",$array_)))
}
Basically, what I need is a wild card at the end of array_* to dump mary, bob and sue.
Can someone please point me in the right direction?
All the arrays are in the GLOBAL object, right? So you can do this:
$arrayName = "array_" . $_GET['name'];
var_dump($GLOBALS[$arrayName]);
Although, a better way to do this would be to have all of the array_bob, array_mary, etc. arrays as indices in one array, so you don't have to deal with the GLOBAL object. Something like this:
$allArrays = array("mary" => array_mary, "bob" => array_bob);
$name = $_GET['name'];
var_dump($allArrays[$name]);
are you looking for this ??
$arrName = $_GET['name'];
print_r(${'array_' . $arrName});
or this ?
$nameArray = array('bob', 'mary', 'sam');
foreach ($nameArray as $arrName) {
if (isset(${'array_' . $arrName})) {
print_r(${'array_' . $arrName});
}
}
Related
This is driving me crazy and definitely fits into some sort of stupid question category, but for some reason my minds gone completely blank. I KNOW there is a simple way to do this, a default PHP function even, but I can't find it.
If anyone can help, there's some easy points for you.
I have an array like this:
array(
'oauth_consumer_key' => "mykey",
'oauth_signature' => "mysignature",
'oauth_signature_method' => "HMAC-SHA1",
'oauth_timestamp' => 1452103343
);
I want to turn it into this:
echo someFunction($data);
// returns
// 'oauth_consumer_key="mykey", oauth_signature="mysignature", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1452103343"'
Can anyone point me in the right direction?
There are multiple functions you could try.
array_map() is one of them. You can use this function with a combination of something like implode() to get what you want but it is a little complicated since your mind has gone blank haha.
print_r() will print out your array with keys and values. Simple to use.
var_dump() same behavior as print_r() except it will give you key and value types as well.
Or you could use json_encode() which will return you as string as you say you want.
Try the built-in PHP function print_r. It will pretty-print the array for you.
Or, if you want an in-memory variable, you can do something like:
function array_to_string($array) {
$acc = "";
foreach($array as $key => $value) {
$acc = $acc.$key.'='.$value.', ';
}
return substr($acc, 0 -1);
}
Yes its very simple you can use like that
function yourrequirement($yourArr){
foreach($yourArr as $key => $value){
echo " ' " .$key. " = ".'"'.$value.'",'."'";
}
}
//Call your function
yourrequirement($yourArr);
How can I include an string in an array?
emailconfig.php
$globalemail 'info#site.com'=>'site'";
I want to make a new array like this:
sendemail.php
include "emailconfig.php"
$fulllist=array('info#forum.com'=>'forum', '$globalemail');
// the Array MUST must appear above, ideally it would look like this
// $fulllist=array('info#forum.com'=>'forum', 'info#site.com'=>'site');
It brings PHP error because of the =>
One way is: in your emailconfig.php, you should have 2 variables, $globalemailkey and $globalemailvalue.
$globalemailkey = 'info#site.com';
$globalemailvalue = 'site';
$fulllist = array('info#forum.com'=>'forum', $globalemailkey => $globalemailvalue);
Or, store an array in emailconfig.php, and use array_merge.
$globalemail = array('info#site.com' => 'site');
$fulllist = array('info#forum.com'=>'forum');
$fulllist = array_merge($fulllist, $globalemail);
$fulllist=array('info#forum.com'=>'forum');
$globalemail = "info#site.com'=>'site'";
$parts = explode('=>', $globalemail);
$fulllist[trim($parts[0], "'")] = trim($parts[1], "'");
http://ideone.com/mmvu9
You could But You Shouldn't use eval to do something like eval("array($yourstring)");. But you shouldn't. really. please.
You can do all sorts of things like preg-match or explode, but couldn't you easier find the source of those pieces of information, and work from there?
I'm currently using the following format to save a value from an HTML form $item_name=$_POST['item_name'];
This saves the value, but how to I also save the name attribute in a variable?
Thanks in advance!
Assuming you want to store each element of $_POST variable as a key-value pair, then you can try:
$var = array();
foreach($_POST as $key => $val) {
$var[$key] = $val;
}
I'm saving a lot of values and want to avoid typing each one out.
Please, make your mind first.
Global variables are intended to be typed by hand.
If you want some automated processing - just keep them in a form of array.
Looks like rdt.exe's answer is what you're looking for.
maye you noticed you have to use the name to access the $_POST-array and get the value. if you want to store the name in a variable, too, just do:
$item_name_name = 'item_name';
$item_name_value = $_POST[$item_name_name];
you could also use some kind of loop to dynamically create variables with the according names like this:
foreach( $_POST as $name => $value ){
$$name = $value;
}
both ways are some kind of unnecessary and useless in my opinion, but you havn't stated what exactly you're trying to achive - so maybe this helps.
An alternative approach:
$keysarray = array_keys ( $_POST);
print_r( $keysarray);
This will give you all the keys in array
The function you are looking for is called extract.
This will create variables for all the $key=>$val pairs in the array.
$_EXAMPLE = array('bird' => 'chicken', 'dog' => 'greyhound');
export($_EXAMPLE);
echo $bird; # prints "chicken"
echo $dog; # prints "greyhound"
Watch out though - this is a huge security risk. So are the solutions described in some of the other answers.
The problem with doing something like this is that a user can tamper with the POST data, and set parameters other than the ones she is supposed to set. If they set variables that are actually variable names in your application, those variables can be overwritten.
$is_admin = false;
$_EXAMPLE = array('bird' => 'chicken', 'dog' => 'greyhound', 'is_admin' => 'true');
export($_EXAMPLE);
if ($is_admin) { # this will now evaluate to true.
# do sensitive stuff...
}
Is there a better way to search through $_SESSION variables (or any array) for a particular string than:
foreach($_SESSION as $k => $v){
if(strstr($k, 'p_')){
Thanks.
edit: My keys will look similar to:
p_123
p_456
i_123
...
If your $_SESSION structure must stay "as is", IMO it is Ok.
However if all 'p_' elements could go under an array index like $_SESSION['p'] = array('key1' => 'val1', ...), you could retrieve all 'p' elements at once.
BTW this is only micro optimization, go with the structure you're fine with.
EDIT: Just be careful with strstr(): if one day you must store keys like i_123_p_456 into your $_SESSION array, you should switch to if (strpos($k, 'p_') === 0).
I'm adding another answer as I'm taking a completely different approach.
If you're going to do this a lot, you could consider using filters.
function pprefix($var) {
return(strstr($var, 'p_'));
}
$filtered = array_filter(array_keys($_SESSION), "pprefix");
foreach($fildered as $k) {
echo("$k => " . $_SESSION[$k]);
}
try this:
if (array_key_exists('p_', $_SESSION)) {
echo($_SESSION['p_']);
}
Or, if you only knows part of the name of the key (like the beggining), I'd use the array_keys() instead $k => $v, like so:
foreach(array_keys($_SESSION) as $k) {
if(strstr($k, 'p_')){
// do something.
}
}
I have always sucked at complex arrays there must be something in my brain preventing me from ever understanding them. I will try to make this example really simple so we will not go off topic. I use this code to use numbers to represent each file name:
$mod_nums = array('1' => $input_zip_path . '01_mod_1.0.2.zip',
'2' => $input_zip_path . '02_mod_1.0.1.zip',
);
So when I use $mod_nums['01'] it will display the path to that file. I have an array from the script that put these $mod_nums values into an array like so:
$files_to_zip = array(
$mod_nums['1'],
$mod_nums['2']
);
That worked fine. Now I wanted to add a $_POST value so that I can enter numbers like 1,2 and other numbers that I add to the $mod_nums array later like 1,3,6,12 for example. So I used an explode for those posted values:
$explode_mods = explode(",", trim($_POST['mods']));
Now for the big question that is racking my brain and spent hours on and cannot get it to work.... I need for $files_to_zip to still be in an array and display the posted values of $mod_nums. So it would be like:
$files_to_zip = array( HAVE $_POSTED VALUES IN HERE );
I hope that makes sense. I need $files_to_zip to remain in array format, grab the file path to the zip files from the $mod_nums array, and display it all correctly so it would dynamically output:
$files_to_zip = array('01_mod_1.0.2.zip', '02_mod_1.0.1.zip');
so posted numbers will appear in an array format for the $files_to_zip variable. Make sense? In short I need an array to have dynamic values. Thanks :)
EDIT
Phew I figured it out myself from memory when I worked on something similar many years ago. This looks tough but it isn't. I had to use a foreach and assign the variable into an array like so:
$blah = array();
foreach ($explode_mods as $value)
{
$blah[] = $mod_nums[$value];
}
then I just assigned $files_to_zip to $blah:
$files_to_zip = $blah;
works perfectly :) I just forgot how to dynamically assign values into an array.
// filenames array
$mod_nums = array('1' => $input_zip_path . '01_mod_1.0.2.zip',
'2' => $input_zip_path . '02_mod_1.0.1.zip',
);
// mod_num keys
$explode_mods = explode(',', trim($_POST['mods']));
// array to hold filenames
$files_to_zip = array();
// loop over all the mod_num keys submitted via POST
foreach($explode_mods as $key){
// save the filename to the corresponding array
$files_to_zip[] = $mod_nums[$key];
}
maybe i havn't understood you right, but won't this just be a simple foreach-loop to add the entrys to $files_to_zip like this:
$explode_mods = explode(",", trim($_POST['mods']));
foreach($explode_mods as $k){
$files_to_zip[] = $mod_nums[$k];
}