php table of values - php

I have a table of values like this:
http://www.conversiontable.org/clothingsizeconversiontable.html
and I want to declare these values in a php class and then manipulate them easily.
In your opinion, what is the best way to do such thing?

You can build an array indexed by country name and then by size number:
$country_sizes = array(
'United States' => array(6, 8, 10, 12, 14, 16, 18),
...
)
But in order to avoid mistakes and make the code more readable, I would assign a label to each size type. Then I would build an array indexed by size type first, and then by country:
$sizes = array(
'S' => array(
'United States' => 6,
'United Kingdom' => 28,
...
),
'M' => array(
'United States' => 8,
'United Kingdom' => 30,
...
),
...
);
This second way is more tedious to build, but seems more natural to me (what's the S size in U.K.?). Anyway, it's your choice according to your needs ;)

something like:
$sizes = array(
'United States' => array(6, 8, 10, 12, 14, 16, 18),
'United Kingdom' => array (28, 30, 32, 34, 36, 38, 40)
);
echo('United states first size: ' . $sizes['United States'][0]);

Related

How to Traversal this?

I am working on an application that has a web based backend and a Android app based front end.
The backend is used to input data into MySQl (using PHP) and then generate a JSON which is then supplied to the Android App (front end) and displayed.
So here is what I have:
This is my Database
CREATE TABLE IF NOT EXISTS `tddept` (
`deptId` int(11) NOT NULL AUTO_INCREMENT,
`deptName` varchar(50) NOT NULL,
`deptIsRoot` tinyint(1) NOT NULL,
`deptHasChild` tinyint(1) NOT NULL,
`deptParentId` int(11) NOT NULL,
PRIMARY KEY (`deptId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ;
--
-- Dumping data for table `tddept`
--
INSERT INTO `tddept` (`deptId`, `deptName`, `deptIsRoot`, `deptHasChild`, `deptParentId`) VALUES
(2, 'One', 1, 1, 0),
(3, 'Two', 1, 1, 0),
(4, 'Three', 1, 1, 0),
(5, 'One1', 0, 0, 2),
(6, 'One2', 0, 0, 2),
(7, 'One3', 0, 0, 2),
(12, 'Two1', 0, 0, 3),
(13, 'Three1', 0, 0, 4),
(14, 'Dept1', 1, 1, 0),
(15, 'dept1.1', 0, 0, 14);
My intention is simple to create a tree structure. only that there are multiple root nodes. and hence multiple leafs.
If put in simple words, I am trying to create a directory tree structure. I would want to display the data in the above table in to a dropdown menu (i.e. using <select> and <option>) as:
One
One1
One2
One3
Two
Two1
Three
Three1
Dept1
dept1.1
Where One, Two, Three, Dept1 are root departments while the rest are sub-departments.
There can definitely be situations in the future where in I might have to have something like:
- One
- - One1
- - - One1a
- - - One1b
so on and so forth.
How can I traversal through them using Core PHP and how can I display them in the very structure as above in to a <select>/<option> (dropdown) menu.
PS: any heads up on JSON would also be a great help.
Here's the solution for image2
echo '<select>';
structureTree(0,0);
echo '</select>';
function structureTree($deptParentId, $level){
$result = getDataFromDb($deptParentId); // change this into SQL
foreach($result as $result){
echo '<option value="'.$deptParentId.'">'.str_repeat('-',$level).$result['deptName'].'</option>';
structureTree($result['deptId'], $level+2);
}
}
// mocking database record
function getDataFromDb($deptParentId)
{
$data = array(
array(
'deptId' => 1,
'deptName' => 'IT',
'deptParentId' => 0,
),
array(
'deptId' => 2,
'deptName' => 'Human Resources',
'deptParentId' => 1,
),
array(
'deptId' => 3,
'deptName' => 'Opreational',
'deptParentId' => 1,
),
array(
'deptId' => 4,
'deptName' => 'Networking Department',
'deptParentId' => 1,
),
array(
'deptId' => 5,
'deptName' => 'Software Development',
'deptParentId' => 1,
),
array(
'deptId' => 6,
'deptName' => 'Mobile Software Development',
'deptParentId' => 5,
),
array(
'deptId' => 7,
'deptName' => 'ERP',
'deptParentId' => 5,
),
array(
'deptId' => 8,
'deptName' => 'Product Development',
'deptParentId' => 5,
),
);
$result = array();
foreach ($data as $record) {
if ($record['deptParentId'] === $deptParentId) {
$result[] = $record;
}
}
return $result;
}
regarding 1.png Here's the SQL based on your sample db table
SELEC dep.deptId As DepId
,dep.deptName As DepartmenName
,parent.deptId As ParentId
,parent.deptName As ParentDepartmentName
FROM tddept As dep
JOIN tddept As parent ON parent.deptParentId = dep.deptId;
recursion is the solution for this problem. While the parent still has a child then it keeps querying until there's no child left.
e.g
One
.One1
.One2
.One3
Here's the php application
function structureTree($depParentId = 0){
$result = queryFunction("SELECT *FROM
tddept WHERE deptParentId = '$depParentId'");
foreach($results as $result){
echo $result['deptName'].'<br/>';
structureTree($resultp['deptParentId']);
}
}
structureTree();

Fill in the blanks in an array

I have an array which I'd like to show all days of weeks or each month of the year, even if data is 0, is it possible to look inside the array and fill in what's not there?
The data I'm returning from mysql table does not show 0 - details below
Show values even if empty
So, the following shows months of the year and the count, I'd like to fill in the months which aren't there with i.e. 'Feb' => '0' .. 'Sep' => '0' .. 'Dec' => '0'
Array example:
$data = array(
'Jan' => 12,
'Mar' => 10,
'Apr' => 7,
'May' => 80,
'Jun' => 67,
'Jul' => 45,
'Aug' => 66,
'Oct' => 23,
'Nov' => 78,
);
Use array_fill_keys to create a "known good starting point" and then array_merge or array addition to incorporate your data.
Example:
$data = array_fill_keys(array('Jan', 'Feb', 'etc'), 0);
$data = array('Feb' => 40) + $data;
Caveat: the result will not end up being ordered by month.
First of all create a blank array with default 0 value than merge this array to your original array.
$data = array(
'Jan' => 0,
'Feb' => 0,
'Mar' => 0,
'Apr' => 0,
'May' => 0,
'Jun' => 0,
'Jul' => 0,
'Aug' => 0,
'Sep' => 0,
'Oct' => 0,
'Nov' => 0,
'Dec' => 0;
);
$data2 = array(
'Jan' => 12,
'Mar' => 10,
'Apr' => 7,
'May' => 80,
'Jun' => 67,
'Jul' => 45,
'Aug' => 66,
'Oct' => 23,
'Nov' => 78,
);
$newarray=array_merge($data, $data2);
As already pointed out in the previous question, you need create some kind of "lookup table" for the month names. If you do not do that within the database, you need to do it in PHP. Such a "table" could be an array of month names:
$months = array(
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
);
You can then iterate over the month names an pick the number if it exists in the result or you take the default value:
$default = 0;
foreach ($months as $month) {
$value = isset($data[$month]) ? $data[$month] : $default;
# $value now contains the month value
...
}
This should just work.
Take care that the month names that the database returns need to be the same you use in your $months array.

get integer / float from string in PHP

I ran into an issue with a data feed I need to import where for some reason the feed producer has decided to provide data that should clearly be either INT or FLOAT as strings-- like this:
$CASES_SOLD = "THREE";
$CASES_STOCKED = "FOUR";
Is there a way in PHP to interpret the text string as the actual integer?
EDIT: I should be more clear-- I need to have the $cases_sold etc. as an integer-- so I can then manipulate them as digits, store in database as INT, etc.
Use an associative array, for example:
$map = array("ONE" => 1, "TWO" => 2, "THREE" => 3, "FOUR" => 4);
$CASES_SOLD = $map["THREE"]; // 3
If you are only interested by "converting" one to nine, you may use the following code:
$convert = array('one' => 1,
'two' => 2,
'three' => 3,
'four' => 4,
'five' => 5,
'six' => 6,
'seven' => 7,
'eight' => 8,
'nine' => 9
);
echo $convert[strtolower($CASES_SOLD)]; // will display 3
If you only need the base 10 numerals, just make a map
$numberMap = array(
'ONE' => 1
, 'TWO' => 2
, 'THREE' => 3
// etc..
);
$number = $numberMap[$CASES_SOLD];
// $number == 3'
If you need something more complex, like interpreting Four Thousand Two Hundred Fifty Eight into 4258 then you'll need to roll up your sleeves and look at this related question.
Impress your fellow programmers by handling this in a totally obtuse way:
<?php
$text = 'four';
if(ereg("[[.$text.]]", "0123456789", $m)) {
$value = (int) $m[0];
echo $value;
}
?>
You need a list of numbers in english and then replace to string, but, you should play with 'thousand' and 'million' clause where must check if after string 'thousend-three' and remove integer from string.
You should play with this function and try change if-else and add some functionality for good conversion:
I'm writing now a simple code for basic, but you know others what should change, play!
Look at million, thousand and string AND, it should be change if no in string like '1345'. Than replace with str_replace each of them separaterly and join them to integer.
function conv($string)
{
$conv = array(
'ONE' => 1,
'TWO' => 2,
'THREE' => 3,
'FOUR' => 4,
'FIVE' => 5,
'SIX' => 6,
'SEVEN' => 7,
'EIGHT' => 8,
'NINE' => 9,
'TEN' => 10,
'ELEVEN' => 11,
'TWELVE' => 12,
'THIRTEEN' => 13,
'FOURTEEN' => 14,
'FIFTEEN' => 15,
'SIXTEEN' => 16,
'SEVENTEEN' => 17,
'EIGHTEEN' => 18,
'NINETEEN' => 19,
'TWENTY' => 20,
'THIRTY' => 30,
'FORTY' => 40,
'FIFTY' => 50,
'SIXTY' => 60,
'SEVENTY' => 70,
'EIGTHY' => 80,
'NINETY' => 90,
'HUNDRED' => 00,
'AND' => '',
'THOUSAND' => 000
'MILLION' => 000000,
);
if (stristr('-', $string))
{
$val = explode('-', $string);
#hardcode some programming logic for checkers if thousands, should if trim zero or not, check if another values
foreach ($conv as $conv_k => $conv_v)
{
$string[] = str_replace($conv_k, $conv_v, $string);
}
return join($string);
}
else
{
foreach ($conv as $conv_k => $conv_v)
{
$string[] = str_replace($conv_k, $conv_v, $string);
}
return join($string);
}
}
Basically what you want is to write a parser for the formal grammar that represents written numbers (up to some finite upper bound). Depending on how high you need to go, the parser could be as trivial as
$numbers = ('zero', 'one', 'two', 'three');
$input = 'TWO';
$result = array_search(strtolower($input), $numbers);
...or as involved as a full-blown parser generated by a tool as ANTLR. Since you probably only need to process relatively small numbers, the most practical solution might be to manually hand-code a small parser. You can take a look here for the ready-made grammar and implement it in PHP.
This is similar to Converting words to numbers in PHP
PHP doesn't have built in conversion functionality. You'd have to build your own logic based on switch statements or otherwise.
Or use an existing library like:
http://www.phpclasses.org/package/7082-PHP-Convert-a-string-of-English-words-to-numbers.html

PHP: Linking an array value with a variable

I have an array that stores names of countries (keys) and age expectancy (values).
$countries = array(
'Costa Rica' => 78, 'Puerto Rico' => 78, 'Colombia' => 73,
'Nicaragua' => 73, 'El Salvador' => 72, 'Peru' => 71,
'Argentina' => 75, 'Uruguay' => 76, 'Ecuador' => 75,
'Mexico' => 76, 'Venezuela' => 73, 'Brasil' => 72,
);
An html form creates a new variable ($country) with a name that corresponds to one countries listed above.
For example: "Costa Rica"
How can I return a variable with the number (age expectancy) that matches the name of the country in the $country variable?
Take for example:
Costa Rica = 78
$ageExpectancy = 78;
Thanks, I hope to be concise.
<?php
$country = $_POST['country']; // assuming post method and form element named 'country'
$ageExpectancy = $countries[$country];
?>

PHP: How will array depth influence performance?

Now I know there is some related questions on this topic but this is somewhat unique.
I have two array structures :
array(
[0] => array(
'stat1' => 50,
'stat2' => 12,
'stat3' => 0,
'country_name' => 'United States'
),
[1] => array(
'stat1' => 40,
'stat2' => 38,
'stat3' => 15,
'country_name' => 'Ireland'
),
[2] => array(
'stat1' => 108,
'stat2' => 0,
'stat3' => 122,
'country_name' => 'Autralia'
)
)
and the second
array(
'stat1' => array(
'countries' => array(
'United States' => 50,
'Ireland' => 40,
'Autralia' => 108,
)
)
),
'stat2' => array(
'countries' => array(
'United States' => 12,
'Ireland' => 38,
)
)
),
etc...
The second array can go even to level 4 or 5 if you add the cities of those respective countries. Further to note is that the second array structure will have no 0 data fields (note that in the second one australia is not there because it is 0) but the first structure will have a whole whack of zeros. Also note that the second structure will have duplicates i.e. 'United States'
My question is this: How does these array structures compare when they are json_encode() and used in a POST ajax request? Will the shallow depth array, with it's whack of zeros be faster or will the better structured array be better in terms of size?
I have done some testing and for a finite dataset the difference in the output data - (I outputted the data into a text file) between the two is insignificant really.
Array structure 1 - All city and country data outputs to 68kb
Array structure 2 - All city and country data outputs to 71kb
So there is a slight difference but it seems that the difference is insignificantly small when taking into account that the data is in JSON format and used in an AJAX request to the google visualization geomap API.
I haven't tested the micro times in loading difference but for a user to look at a loading .gif image for 0.0024microseconds (i'm shooting a random time for the sake of argument) does not make a big dent in usability either way. Thanx all for you comments

Categories