Fill in the blanks in an array - php

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.

Related

PHP Checkif two arrays have the same keys and same count of keys [duplicate]

This question already has answers here:
PHP - Check if two arrays are equal
(19 answers)
Check if two arrays have the same values (regardless of value order) [duplicate]
(13 answers)
How to check if PHP associative arrays are equal, ignoring key ordering?
(1 answer)
Closed 4 years ago.
I'm trying to match 2 arrays that look like below.
$system = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$public = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
My problem is, I need the array keys of both arrays to be the same value and same count.
Which means:
// passes - both arrays have the same key values and same counts of each key
$system = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$public = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
// fails - $public does not have 'blue' => 1
$system = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$public = array('red' => 2, 'green' => 3, 'purple' => 4);
// should fail - $public has 2 'blue' => 1
$system = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$public = array('blue' => 1, 'blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
I've tried using array_diff_keys, array_diff and other php functions, but none can catch extra keys with the same value (i.e. if 'blue' => 1, is repeated it still passes)
What's a good way to solve this?
When you write two values with same key in PHP, the second one will overwrite the value from the first (and this is not an error). Below is what I did on the PHP interactive CLI (run it with php -a):
php > $x = ["x" => 1, "x" => 2, "y" => 2];
php > var_dump($x);
array(2) {
["x"]=>
int(2)
["y"]=>
int(2)
}
So array_diff seems to be working correctly. You are just expecting PHP to behave in a different way than it actually does!

How do I use mysql column contents as index (key) to php array in mySql query?

I want to know how or if I can use the content of a column in mysql as a key (index) to a php array within the mysql query.
Example:
Column name: period
Column contents: (one of) 'Year','Month','Fortnight','Week','Hour','Day'
My php array:
$x = array(
'Year' => 1,
'Month' => 12,
'Fortnight' => 26,
'Week' => 52,
'Hour' => 1872,
'Day' => 365
);
Another variable $y can be any number (say < 1,000,000)
I want to write a WHERE (or HAVING) condition such as...
WHERE '$x[`period`]' * '$y' > 12345
E.g. when the contents of period = 'Month', $y should be multiplied by 12 before being tested to be > 12345
What is the best (most efficient) way to do this, other than (say) a long CASE conditional?
Thank you
i have write demo query
<?php
$x = array(
'Year' => 1,
'Month' => 12,
'Fortnight' => 26,
'Week' => 52,
'Hour' => 1872,
'Day' => 365
);
$y=10;
$sql="select * from table where ".$x['Month']*$y." > 12345" ;
echo $sql;
in $x[] select your value to multiply
You can use case instead of php array:
<?php
$y = 100;
$sql = "select * from tablename
where (case `period`
when 'Year' then 1
when 'Month' then 12
when 'Fortnight' then 26
when 'Week' then 52
when 'Hour' then 1872
when 'Day' then 365
end) * {$y} > 12345";
Try this:
<?php
$y = 10;
$period = "Month";
$x = array(
'Year' => 1,
'Month' => 12,
'Fortnight' => 26,
'Week' => 52,
'Hour' => 1872,
'Day' => 365
);
$sql="select * from table where ".($x[$period] * $y)." > 12345" ;
echo $sql;

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

Get key of max element in array [duplicate]

This question already has answers here:
Return index of highest value in an array
(8 answers)
Closed 9 years ago.
I have an array:
$someArray = array('fb' => 32, 'gp' => 11, 'tw' => 7, 'vk' => 89, 'ok' => 112);
As you can see last element in array has the greatest value. I need to return the key(ok) of last element. How to do this?
Based on https://stackoverflow.com/a/1461363/1641835:
$someArray = array('fb' => 32, 'gp' => 11, 'tw' => 7, 'vk' => 89, 'ok' => 112);
$max_keys = array_keys($someArray, max($someArray));
// $max_keys would now be an array: [ 'ok' ]
$max_keys will now be an array of all the keys that point to the maximum value. If you know there will only be one, or you don't care which you retrieve, you could instead use:
$someArray = array('fb' => 32, 'gp' => 11, 'tw' => 7, 'vk' => 89, 'ok' => 112);
$max_key = array_search(max($someArray), $someArray);
// $max_key would now be 'ok'

php table of values

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

Categories