I'm working with a bit of php code called PHP_XLSWriter. It accepts an input, then outputs it to an Excel sheet.
Their example.php page does exactly what I want, and in it, there's a bit of code that determines what specifically gets exported:
$data1 = array(
array('2003','1','-50.5','2010-01-01 23:00:00'),
array('2003','=B2', '23.5','2010-01-01 00:00:00'),
);
I haven't written arrays like this exactly in the past, but I've passed my own array into this page through AJAX from a separate page. I'm not sure if it's important how I did this, but I'm happy to paste in that code if it is.
When I try to execute the page using MY array, the page fails and my console is full of intelligible text.
I can't figure out why this is as the var_dump of our two arrays are exactly the same.
the original var_dump($data1);
array(2) {
[0]=>
array(4) {
[0]=>
string(4) "2003"
[1]=>
string(1) "1"
[2]=>
string(5) "-50.5"
[3]=>
string(19) "2010-01-01 23:00:00"
}
[1]=>
array(4) {
[0]=>
string(4) "2003"
[1]=>
string(3) "=B2"
[2]=>
string(4) "23.5"
[3]=>
string(19) "2010-01-01 00:00:00"
}
}
the var_dump() when I set $data1 equal to the array I've imported:
array(2) {
[0]=>
array(4) {
[0]=>
string(6) "blah"
[1]=>
string(12) "two"
[2]=>
string(6) "0.0160"
[3]=>
string(6) "0.0173"
}
[1]=>
array(4) {
[0]=>
string(3) "blah"
[1]=>
string(14) "three"
[2]=>
string(6) "0.0085"
[3]=>
string(6) "0.0095"
}
}
and echo gettype($data1); outputs array for both of the strings.
What might I be doing to cause this to break? I'm positive it has something to do with my array, as all else held equal, the code works.
Related
I'm trying to combine two array in PHP with array_combine() function, but sometimes it working fine and sometimes it's not. I can't understand why it's working like this!
My Code:
var_dump($selectedDuretion);
var_dump($selectedDuretionType);
$combination = array_combine($selectedDuretion, $selectedDuretionType);
return $combination;
Expected OUTPUT:
array(4)
{
[0]=> string(1) "3"
[1]=> string(2) "12"
[2]=> string(1) "4"
[3]=> string(1) "3"
}
array(4)
{
[0]=> string(4) "days"
[1]=> string(4) "days"
[2]=> string(5) "weeks"
[3]=> string(5) "weeks"
}
{"3":"days","12":"days","3":"weeks","4":"weeks"}
Actual OUTPUT :
array(4)
{
[0]=> string(1) "3"
[1]=> string(2) "12"
[2]=> string(1) "4"
[3]=> string(1) "3"
}
array(4)
{
[0]=> string(4) "days"
[1]=> string(4) "days"
[2]=> string(5) "weeks"
[3]=> string(5) "weeks"
}
{"3":"weeks","12":"days","4":"weeks"}
The combination of arrays it shocking, I'll be thankful if anyone tell me why is this happening and how to solve it.
PHP Does not allow you to have duplicate indices in an array while JSON does allow you to have that for whatever reasons.
Since you are trying to convert PHP arrays to JSON your duplicate key gets eliminated. Hence you will have to manually build the JSON string.
$json="";
for($i=0;$i<count($selectedDuration);$i++)
{
$json.='"'.$selectedDuration[$i].'":"'.$selectedDurationType[$i].'",';
}
$json=rtrim($json,",");
$json="{".$json."}";
echo $json;
Output
{"3":"days","12":"days","4":"weeks","3":"weeks"}
Fiddle
I've been trying to merge this array ONLY WHERE $array[4] exists more than one time, example: $array[4] == 'red' exactly twice. How can I merge only those arrays while keeping the others? I have made several attempts at this and I am willing to include my efforts if asked.
Consider this array:
array(3) {
[0]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "532"
[2]=>
string(1) "2"
[3]=>
string(9) "Domain(s)"
[4]=>
string(20) "red"
}
[1]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "532"
[2]=>
string(7) "License"
[3]=>
string(3) "Fee"
[4]=>
string(20) "red"
}
[2]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "536"
[2]=>
string(7) "License"
[3]=>
string(3) "Fee"
[4]=>
string(16) " University Test"
}
}
TRYING TO ACHIEVE:
array(3) {
[0]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "532"
[2]=>
string(1) "2"
[3]=>
string(9) "Domain(s)"
[4]=>
string(20) " red"
[5]=>
string(3) "Fee"
[6]=>
string(7) "License"
}
[1]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "536"
[2]=>
string(7) "License"
[3]=>
string(3) "Fee"
[4]=>
string(16) " University Test"
}
}
foreach ($test as $item) {
if (!isset($merged[$item[4]])) {
// add the item to the merged array using key=$item[4]
$merged[$item[4]] = $item;
} else {
// merge the item with the item that is already in the array at key=$item[4]
$merged[$item[4]] = array_unique(array_merge($merged[$item[4]], $item));
// array_unique is necessary because array_merge will not overwrite numeric keys
}
}
// convert the keys back to numeric (if you care to)
$merged = array_values($merged);
I am making a mailing system, and my input is array of arrays, i need to combine them to one, i already aggregated based on the email.
input example:
array(2) {
[0]=>
array(15) {
["enabled"]=>
string(1) "1"
["file_size_bytes"]=>
string(6) "200122"
["email"]=>
string(21) "jon#gmail.com"
["content"]=>
string(34) "{"Notice":827,"co":3241,"Co":1555}"
}
[1]=>
array(15) {
["enabled"]=>
string(1) "1"
["file_size_bytes"]=>
string(6) "592024"
["email"]=>
string(21) "jon#gmail.com"
["content"]=>
string(97) "{"Co":388,"co":5564,"xml":2982,"CO":6,"Warning":1957,"warning":42,"Notice":13,"cO":9,"Connect":6}"
}
}
array(1) {
[0]=>
array(15) {
["enabled"]=>
string(1) "1"
["file_size_bytes"]=>
string(5) "19116"
["email"]=>
string(22) "kelly#gmail.com"
["content"]=>
string(8) "{"co":1}"
}
}
input array to the mail function should look like:
array(1) {
[0]=>
array(15) {
["enabled"]=>
string(1) "1"
["file_size_bytes"]=>
string(6) "200122"
["email"]=>
string(21) "jon#gmail.com"
["content"]=>
string(34) "{"Notice":827,"co":3241,"Co":1555}"
["enabled"]=>
string(1) "1"
["file_size_bytes"]=>
string(6) "592024"
["email"]=>
string(21) "jon#gmail.com"
["content"]=>
string(97) " {"Co":388,"co":5564,"xml":2982,"CO":6,"Warning":1957,"warning":42,"Notice":13,"cO":9,"Conne ct":6}"
}
}
array(1) {
[0]=>
array(15) {
["enabled"]=>
string(1) "1"
["file_size_bytes"]=>
string(5) "19116"
["email"]=>
string(22) "kelly#gmail.com"
["content"]=>
string(8) "{"co":1}"
}
}
its basically spouse to combine the two arrays that are in the same array.
how can i do that? thanks :)
The required outcome is not possible since you have duplicate keys in the array which won't be possible in PHP.
e.g.
["content"] => string(34) "{"Notice":827,"co":3241,"Co":1555}"
will be replaced by
["content"] => string(97) "{"Co":388,"co":5564,"xml":2982,"CO":6,"Warning":1957,"warning":42,"Notice":13,"cO":9,"Connect":6}"
You can do this simply using the array_merge function.
In their answer, Maarten suggests this is not possible because duplicate keys would be overwritten. However this only occurs when the keys aren't numeric. In your example above the keys of the first array are 0 and 1 and the second just 0. All numeric.
So all you need to do is:
array_merge($array1,$array2);
I'm trying to make a multidimensional array that should print out like this (without formatting):
array(3) {
[0]=> array(5) {
[0]=> int(0)
[1]=> string(5) "Arena"
[2]=> string(18) "2012-05-3017:00:00"
[3]=> string(18) "2012-05-3000:00:00"
[4]=> string(33) "Masquerade Checkin (Participants)"
},
[1]=> array(5) {
[0]=> int(0)
[1]=> string(10) "Workshop 1"
[2]=> string(18) "2012-05-3017:00:00"
[3]=> string(18) "2012-05-3000:00:00"
[4]=> string(15) "Death Note (Live)"
},
[2]=> array(5) {
[0]=> int(0)
[1]=> string(7) "Video 6"
[2]=> string(18) "2012-05-3017:00:00"
[3]=> string(18) "2012-05-3000:00:00"
[4]=> string(26) "Takeuchi Fan Panel"
}
}
Notice from the above code that the inner array() length is always 5.
Here is my code below:
$loopsArray = array();
$data=array();
// graphing info come in here.
foreach ($events as $key => $event) {
$el=$event['event_location'] ;
$eln=$event['event_locationName'];
$ed=$event['event_date'];
$es=$event['event_start'];
$ee=$event['event_end'];
$en=$event['event_name'];
array_push($loopsArray,$el,$eln, $ed.$es,$ed.$ee,$en);
array_push($data,$loopsArray);
}
var_dump($data);
Here the print out
array(27) {
[0]=> array(5) {
[0]=> int(0)
[1]=> string(5) "Arena"
[2]=> string(18) "2012-05-3017:00:00"
[3]=> string(18) "2012-05-3000:00:00"
[4]=> string(33) "Masquerade Checkin (Participants)"
}
[1]=> array(10) {
[0]=> int(0)
[1]=> string(5) "Arena"
[2]=> string(18) "2012-05-3017:00:00"
[3]=> string(18) "2012-05-3000:00:00"
[4]=> string(33) "Masquerade Checkin (Participants)"
[5]=> int(13)
[6]=> string(11) "Autograph 1"
[7]=> string(18) "2012-06-2419:00:00"
[8]=> string(18) "2012-06-2422:00:00"
[9]=> string(17) "Parents and Anime"
}
//... continues
}
Notice that the inner arrays length double each iteration. array(5) array(10) array(15)array(20).
It doubles up to 60 elements in the last inner array. Each inner array should only have 5 elements in them. I don't understand why it is doubling or how to fix it.
Can you look over my loop and let me know how to fix it?
I have to use this multidimensional array for this code to work in JpGraph.
TIP : write $loopsArray = array(); inside foreach
better approach
instead of
array_push($loopsArray,$el,$eln, $ed.$es,$ed.$ee,$en);
array_push($data,$loopsArray);
try this
$temp = array ($el,$eln, $ed.$es,$ed.$ee,$en);
$data[] = $temp;
$GLOBALS["items"] = array('one', 'two', 'three', 'four', 'five' ,'six', 'seven');
$alter = &$GLOBALS["items"]; // Comment this line
foreach($GLOBALS["items"] as $item) {
echo get_item_id();
}
function get_item_id(){
var_dump(key($GLOBALS["items"]));
}
Check output of this code, with commented and uncommented second line.
My result(PHP 5.3.0).
With second line
int(1) int(2) int(3) int(4) int(5) int(6) NULL
Without second line:
int(1) int(1) int(1) int(1) int(1) int(1) int(1)
Why so strange result?
Here is a possible explanation:
We know that foreach always loops over a copy of the array if it is not referenced:
Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. foreach has some side effects on the array pointer.
That means that the internal pointer of the original array is not changed and key() will always return the same value (as we can see when we comment out the line). And indeed if we do a var_dump($GLOBALS), we get:
["items"]=>
array(7) {
[0]=>
string(3) "one"
[1]=>
string(3) "two"
[2]=>
string(5) "three"
[3]=>
string(4) "four"
[4]=>
string(4) "five"
[5]=>
string(3) "six"
[6]=>
string(5) "seven"
}
(no reference)
But as soon as we generate a reference to the array (with $alter), $GLOBALS['items'] becomes a reference too, because both entries have to point to the same array:
["items"]=>
&array(7) {
[0]=>
string(3) "one"
[1]=>
string(3) "two"
[2]=>
string(5) "three"
[3]=>
string(4) "four"
[4]=>
string(4) "five"
[5]=>
string(3) "six"
[6]=>
string(5) "seven"
}
["alter"]=>
&array(7) {
[0]=>
string(3) "one"
[1]=>
string(3) "two"
[2]=>
string(5) "three"
[3]=>
string(4) "four"
[4]=>
string(4) "five"
[5]=>
string(3) "six"
[6]=>
string(5) "seven"
}
Hence, the foreach loop does iterate over the original array and changes the internal pointer, which affects key().
To sum up: It is a problem with references, not with $GLOBALS.