php-excel-reader not displaying format of empty cells - php

I'm working on a little app that takes Excel spreadsheets that display tournament brackets, and then output the excel data and formatting into xml. I'm using Spreadsheet_Excel_Reader (https://code.google.com/archive/p/php-excel-reader/) to retrieve the data from the tournament bracket spreadsheets. The only problem is that any cell will no value will not be put in the Spreadsheet_Excel_Reader->sheets[0]['cells'] array, thus causing the table cell borders to not output. It appears that if I put a space in each cell that I want display, it works fine, but that seems like a pretty hackey way of doing it. Does anyone if there is a better way to output the formatting of a blank cell using the Spreadsheet_Excel_Reader?

I found this question after having the same problem (a spreadsheet row with 5 columns, 2 of them blank, are shown as an array of 3 items, omitting the 2 empty cells).
It took longer than I wanted it to, but I think I figured it out. When you dump your row data, check out the indexes.
array(5) {
["maxrow"]=>
int(0)
["maxcol"]=>
int(0)
["numRows"]=>
int(2)
["numCols"]=>
int(3)
["cells"]=>
array(2) {
[1]=>
array(2) {
[1]=>
string(4) "test"
[2]=>
string(3) "xls"
}
[2]=>
array(2) {
[1]=>
string(5) "empty"
[3]=>
string(4) "test"
}
}
}
In my case, I have an empty cell in the second row, between "empty" and "test", and I can see that "test" as the index '3'.
What you (and I) need to do is, instead of using foreach(), be sure to iterate over each expected index using for() or foreach(range()) ending at numCols.

Related

Accessing an array that is part of an object seems to fail

I have the following code snippet that doesn't do what I expect:
var_dump($pronunciationResults);
$alignEntries = $pronunciationResults->alignEntry;
var_dump($alignEntries);
Which produces for the first var_dump (I have elided out the end of the structure):
object(SimpleXMLElement)#1371 (1) {
["alignEntry"]=>
array(123) {
[0]=>
object(SimpleXMLElement)#1375 (3) {
["#attributes"]=>
array(1) {
["alignType"]=>
string(2) "OK"
}
["target"]=>
string(3) "The"
Followed by the output of the second var_dump
object(SimpleXMLElement)#1373 (3) {
["#attributes"]=>
array(1) {
["alignType"]=>
string(2) "OK"
}
["target"]=>
string(3) "The"
I have a really simple php program that works exactly as expected, and have no idea why in this case I get the first element of the array, rather than the array itself.
So the comment below by #trincot was interesting. However:
var_dump($pronunciationResults->children());
var_dump($pronunciationResults->children()->alignEntry);
var_dump($pronunciationResults->alignEntry->children());
Gives the exact same structure as I got above for each of the var_dump.
It turns out the foreach does walk the original alignEntries array, even though var_dump doesn't show it as an array.
I have no idea what is going on with var_dump
Your objects are not standard objects. You should use them using the appropriate API. In case of an instance of SimpleXMLElement, you can get the children array via the method children():
foreach ($pronunciationResults->children() as $child) {
var_dump($child);
}
Of course, since also those child elements are of the SimpleXMLElement class, you should also treat those via the proper methods. So if you would want to iterate over their attributes, then call the attributes() method on them, ...etc.
Do not focus on what you see in var_dump, except for the class. You'll see undocumented properties which are not supposed to be used directly. Stick to the documented interface for those objects.

Manually Sort An Array with no common key?

I've tried many solutions here but they all seem to require a common key to sort the array by.
My var_dump() for the array is as follows:
array(10) { [0]=> string(11) "Agriculture" [2]=> string(6) "Metals" [12]=> string(10) "Sanitation" [14]=> string(19) "Health & Beauty" [22]=> string(13) "Oil & Gas" [27]=> string(12) "Construction" [31]=> string(13) "Manufacturing" [58]=> string(8) "Retailer" [61]=> string(11) "Distributor" [77]=> string(7) "Service" }
I'd like to be able to sort the strings in the array into the following order:
Service
Distributor
Retailer
Manufacturing
Construction
Oil & Gas
Health & Beauty
Sanitation
Metals
Agriculture
I know this is not an ideal solution but I really need to manually sort these.
Any help would be super appreciated. Thanks!
The main difference is that this one is generated from a database, so removing one from the databases should also remove it from the array. Hence why I can't manually create it.
So, I assume, that if one is added to the database, it should somehow also be "available"?
If one is renamed in the Database, sorting should still work?
The only reliable solution would be to add another column to the database table, let's call it position - and then fetch the entries and sort them by the position value, which could be 1 upto 10.
Hint on that: Start with a step size of thausand (1000,2000,3000,...) This allows you to add an item later somewhere in between without changing all subsequent item as well. (i.e. creating an entry with position 1500 would become the new second entry, and everything else will automatically shift one position down. If another "new second entry" shall be inserted, you can use 1250 and so on... Gives you some "time" until you have to finally reindex the positions - Use decimal/double if you want to keep this going forever^^)
For me it looks like you want to sort you array by index in descending order. For that you should use krsort() function.
http://php.net/manual/en/function.krsort.php
To sort an array in a reverse order use
array array_reverse ( array $array [, bool $preserve_keys = FALSE ]
Parameters
array
The input array.
preserve_keys
If set to TRUE numeric keys are preserved. Non-numeric keys are not affected by this setting and will always be preserved.
http://php.net/manual/en/function.array-reverse.php

Sorting out array of postmeta in database

I need to sort out different components of a metadata string in the table postmeta in my WordPress database.
The string looks like this in the database:
a:1:{i:0;a:4:{s:8:"employer";s:15:"ExampleEmployer";s:9:"job_title";s:9:"Job Title";s:4:"date";s:9:"2014-2016";s:5:"notes";s:20:"Experience(job)notes";}}
I got quite worried when I saw this the first time, and tried to search for a more humane row, but couldn't find one, so this is the only one with the information I need.
The information is from a plugin call WP Job Manager, and I haven't found any documentation on how to sort their array out, so I'm asking you kind people.
Thank you!
EDIT: I'm very tired. The string automatically unserializes when fetched from the database, I just saw that now. Thank you all for your help.
It is a serialized String. You have to unserialize the string and fetch the output.
<?php
$serialized_data = serialize(array('Math', 'Language', 'Science'));
echo $serialized_data . '<br>';
// Unserialize the data
$var1 = unserialize($serialized_data);
// Show the unserialized data;
var_dump ($var1);
?>
Output:
Serialized:
a:3:{i:0;s:4:"Math";i:1;s:8:"Language";i:2;s:7:"Science";}
Unserialized:
array(3) { [0]=> string(4) "Math" [1]=> string(8) "Language" [2]=> string(7) "Science" }

Error "Cannot use object of type stdClass as array" when accessing a clearly existing, integer type data from Laravel Database query result

I got my data from Laravel database query command:
$group = DB::table('groups')->where("id", $group_id)->first();
When I var dump my data, I get:
object(stdClass)#200 (7) {
["id"]=>
int(1)
["levels_id"]=>
int(1)
["title"]=>
string(8) "Novice 1"
["description"]=>
string(11) "Lorem Ipsum"
["max_question_display"]=>
int(5)
["created_at"]=>
NULL
["updated_at"]=>
NULL
}
I want to access the max_question_display. But when I do:
var_dump($group["max_question_display"]);
PHP returns error Cannot use object of type stdClass as array.
When I do:
var_dump($group->max_question_display);
I get:
int(5)
But I don't want the int. I only want the 5. In integer form.
If I foreach loop the $group:
foreach ($group as $t) {
echo "<pre>";
var_dump($t);
echo "</pre>";
}
I get each of the data as a single data each loop.
int(1)
int(1)
string(8) "Novice 1"
string(11) "Lorem Ipsum"
int(5)
NULL
NULL
This is obviously also not the way the result accessed that I'm looking for.
I also tried to get the first element of array, thinking that this might be an array with 1 element, but that also raise the same error.
I get it that the general answer in this site about this error is that "stdClass is not array". I have browsed several question with similar title like mine, but nothing address object that came from Laravel DB. When I read the manual on Laravel DB, I was assured that I can access the data returned like a simple dictionary / hashmap.
EDIT: Sorry, I understand my very, very newbie mistakes. No need to answer this. Thanks.
Notice the first line of your first var_dump:
object(stdClass)#200
Because you're dealing with an object, you access its properties with ->. When you do:
var_dump($group->max_question_display);
The reason you see (int) in the output is that the var_dump function shows the value type, next to the value. To access the value, do
$group->max_question_display;
If you want to see it on screen without the type, use echo
echo $group->max_question_display; // 5
stdClass is an object. You cannot use an object with array syntax to access its properties, if the class does not implement ArrayAccess interface.
As pointed out by #IbrahimLawal , var_dump outputs both the type and value. Just echoing $group->max_question_display will provide just the value
echo $group->max_question_display; // 5
In Summary: You must use arrow syntax when interacting with stdClass.

How to convert a Python List to a PHP array via a MySQL table

I have inherited a MySQL database where one of the fields in a table returns a string that was once a Python list so I get something like this:
$test = "[u'Person 1', u'Person 2']";
What is the cleanest/easiest/best/simplest (I'm not sure how to phrase this) to get this data back into an array in PHP? I am using PHP4 but I could upgrade to PHP5.4 if necessary.
I don't have much experience programming in PHP and my first thought was to do something like this:
$new = explode(",",$test);
This kind of works but it would need cleaning up afterwards (for instance each element of the array has at least u' in front of it) and is obviously fragile if any of the elements contain a comma.
Is there a cleaner/easier/better/simpler way of doing this?
Your best bet is to write a Python script that updates the mysql datastore with JSON, which can be easily parsed by just about every language out there. ( as #Hugo Dozois noted ]
Personally, I wouldn't try to read this in PHP. The example you showed has 2 unicode strings in a flat list... but you're likely going to run into more issues and edge cases as time goes on. You might have some unicode strings, other byte strings, some numbers... possibly even nested lists or dicts.
If you didn't inherit it, and were 100% sure of what's going on - then sure, you could parse stuff. But it should take less than 5 minutes to write and run a Python script that converts this to JSON and solves all your problems.
You could use preg_match_all and do this:
$test = "[u'Person 1', u'Person 2']";
preg_match_all('/u\'(.*?)\'/', $test, $matches);
var_dump($matches);
/*
array(2) {
[0]=> array(2) {
[0]=> string(11) "u'Person 1'"
[1]=> string(11) "u'Person 2'" }
[1]=> array(2) {
[0]=> string(8) "Person 1"
[1]=> string(8) "Person 2"
}
}
*/

Categories