Insert a String inside an Array - php

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?

Related

var_dump array using preg_match

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

PHP Date merging class / method?

I am doing work where I get data in various formats from various sources. I will end up with something like this:
$dataSource1 = ... ;
$dataSource2 = ... ;
$dataSource3 = ... ;
I need to COMBINE these data sources, all with different field names, into one object, that I can sort according to fields, limit to X number etc.... all for display purposes.
What is the best way to do this? Is there a good php library that does this?
Three possible solutions,
You could always create a database and just use that. (Probably the best thing to do)
Alternatively, you could try attempt to do some polymorphisisng? (cant be made into a verb!)
Finally, you could also include all the other pages into the one you will be displaying from.
(I suggest number 1)
I think the simplest way is using an associative array.
$dataSource1 = ...;
$dataSource2 = ...;
...
$dataSourceN = ...;
$data = array()
$data[0] = $dataSource1;
$data[1] = $dataSource2;
And so on. Just remember that a numeric index array always starts at 0. So the first element would be $data[0].
If you want a more complex bind you can create a multidimensional array. It means you can sort by specific fields. See an example:
$data1 = 'Brazil';
$dataArray = array()
$dataArray[] = array(
'countryId' => 'id',
'countryName' => $data1,
'usersFromThisCountry' => $data1Users
);
Now you can sort $dataArray according to 'countryId','countryName','usersFromThisCountry'.

How can I convert similarly named POST variables into an array?

I'm trying to accept an unknown number of similarly named POST variables like the following:
foo[bar[0]] = 56
foo[bar[1]] = 43
foo[bar[2]] = ah84
foo[bar[3]] = 92hs
With the rest of my POST data looking like:
foo[baz] = 1432
foo[expected] = 48hf
Some requests may have no foobars, but most will have 1, and some with have 2-4.
Ideally I would like to end with an array: array( 56, 43, ah84, 92hs)
Is there a way to loop through the POST variables not knowing the number of them? I can create the array if I know what to expect, but in this case I have no way of telling what will come across.
Having a look at this example might be a bit of help.
If you know you have an array (with non-sequential indices) and you don't care about the order, the cheapest and easiest fix would be array_values.
In your case, it'd be something like $some_var = array_values($_POST['foo']['bar']).
One possible solution:
$vars = array_unique($your_post_data);
You could get the request header and get the post info from it.
<?php
$header = getallheaders();
$postInfo = explode("&", $header[count($header) - 1]);
for($i = 0; $i < count($postInfo); $i++)
{
$a = explode("=", $postInfo[$i]);
$postInfo[$i] = $a[1];
}
?>
Not tested & may not work but I think you'll get the idea.

PHP, Best way of setting a value in a multi-dimensional array when the pathway is dynamic?

Solution Found: Dynamic array keys
I have a multi dimensional dynamic array, the format of which varies. for example.
$data = array('blah1'=>array('blah2'=>array('hello'=>'world')));
I then have a dynamic pathway as a string.
$pathway = 'blah1/blah2/hellow';
The pathway is broken up into it's component parts, for simplicities' sake:
$pathway_parts = explode('/', $pathway);
My problem arises from wanting to set the value of 'hello'. The way I currently do it is via eval, but I want to illiminate this evil partly because of the php Suhosin hardening breaking the app, but also because I don't believe this is the best way.
eval('$data["'.implode('"]["', $pathway_parts).'"] = $value;');
$data must always return the full array because further down the array it is serialised and stored. What would the best way to transverse the array to set the value without the use of eval?
You can do this using references to gradually work your way to referencing that value.
$data = array('blah1'=>array('blah2'=>array('hello'=>'world')));
$pathway = 'blah1/blah2/hello';
$pathway_parts = explode('/', $pathway);
$ref = &$data;
foreach ($pathway_parts as $part)
{
// Possibly check if $ref[$part] is set before doing this.
$ref = &$ref[$part];
}
$ref = 'value';
var_export($data);
this doesn't sound like the best structure, but something like this might work:
//$data = array('blah1'=>array('blah2'=>array('hello'=>'world')));
$pathway = 'blah1/blah2/hellow';
$pathway_parts = explode('/', $pathway);
$value = 'some value';
$data = $value;
while($path = array_pop($pathway_parts)){
$data = array($path=>$data);
}
echo '<pre>'.print_r($data,true).'</pre>';
Other than that, you might be able to build a json string and use json_decode on it. json_decode doesn't execute code.

Problem with associative arrays

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];
}

Categories