Combination Generator in PHP - php

I need to write a script which takes a set of numeric values, for example: "1", "2" and "3" (the total number of values could be limitless)
And return the total of every combination, up to a given total, i.e. 5, therefore the return would look something like:
(1),
(2),
(3),
(1, 1),
(1, 1, 1),
(1, 1, 1, 1),
(1, 1, 1, 1, 1),
(1, 1, 1, 2),
(1, 2, 2)
....
What is the most efficient way to do this in PHP?
Thanks in advance.

Related

Removing integer from JSON column using JSON_REMOVE

I would like to remove the number 6 from the column access (type json) without knowing its array location.
Example access row 1: [1, 2, 3, 4, 5, 6, 7, 8, 9]
CREATE TABLE example (
id int NOT NULL AUTO_INCREMENT,
access json NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO `example` (`access`) VALUES ('[1, 2, 3, 4, 5, 10, 20, 30]');
SELECT * FROM `example`;
SELECT JSON_SEARCH(`access`, 'one', '30') from `example`;
The JSON_SEARCH doesn't return any results.
You've probably moved on but should anyone else find themselves in the same position as I, trying to search arrays of integers... JSON_SEARCH doesn't support the function.
https://bugs.mysql.com/bug.php?id=79233

how to match two different columns in Mysql which has comma separated values

I have two tables:
CampaignTable
which has following property
id , campaign ,user_group
example would be
1 8867116213 5,11,15,16,18,20
2 8867116214 0,8,22
Then I have another table called User Table
with following property
id emp_id user_group
Example is like this
1 274 0,5,8,9,10,11,21,20
2 275 5,11,20
3 279 19,21,22,25
I have to join this table and create an Array which has campaign wise user
for example for campaign with id 1 it should give me
274, 275
How can I achieve this in Mysql
Thanks
You should definetely normalize your data. For example consider this kind of normalization which renders almost no change to your DB structure:
INSERT INTO CampaignTable
(`campaign`, `user_group`)
VALUES
(8867116213, 5),
(8867116213, 11),
(8867116213, 15),
(8867116213, 16),
(8867116213, 18),
(8867116213, 20),
(8867116214, 0),
(8867116214, 8),
(8867116214, 22)
;
INSERT INTO UserTable
(`emp_id`, `user_group`)
VALUES
(274, 0),
(274, 5),
(274, 8),
(274, 9),
(274, 10),
(274, 11),
(274, 21),
(274, 20),
(275, 5),
(275, 11),
(275, 20),
(279, 19),
(279, 21),
(279, 22),
(279, 25)
;
You could then fetch your data with a query as simple as that:
SELECT c.campaign, GROUP_CONCAT(DISTINCT u.emp_id) FROM CampaignTable c
JOIN UserTable u ON c.user_group = u.user_group
GROUP BY c.campaign
See SQLFiddle

two-dimensional array inside two-dimensional arrays in php

Is it possible to achieve something like this? A two-dimensional array containing two-dimensional jagged array?
$jobOrder = array(array(1, "Web Developer", 100,
array(array(1, "PHP", 1),
array(2, "HTML", 1), array(3, "JAVA", 1)),
array(array(1, "pleasing personality", 1),
array(2, "english skills", 1)), 0),
array(2, "Senior Programmer", 50,
array(array(3, "Phython", 1),
array(5, "RUBY", 1),
array(10, "c#", 1)),
array(array(5, "good social skills", 1),
array(11, "management skills", 1))));
I want to store Job Order details into an array that should contain an orderID, job title, number of openings, skills(may have multiple skills so stored in an array; 2-d because I also wanted to store skillID, skill name and flag: if its been removed or not), qualifications(may have multiple qualifications same with skills), requirements and benefits (also same with skills). I would like to know how to access it.
You can access element like this:
$jobOrder = array(array(1, "Web Developer", 100,
array(array(1, "PHP", 1),
array(2, "HTML", 1), array(3, "JAVA", 1)),
array(array(1, "pleasing personality", 1),
array(2, "english skills", 1)), 0),
array(2, "Senior Programmer", 50,
array(array(3, "Phython", 1),
array(5, "RUBY", 1),
array(10, "c#", 1)),
array(array(5, "good social skills", 1),
array(11, "management skills", 1))));
print_r($jobOrder[0][3]);
I will print the array and also you can access the elements in the array also by adding further indexes.
Yes It is possible as you can store number of array rows in the array: means you can add number of rows as well number of elements inside the array even it may multidimensional array as you defined.

Postgres to php Array

I have a table with values :
CREATE TABLE grade
(id int, name varchar(2), no int);
INSERT INTO grade
(id, name, no)
VALUES
(1, 'A', 7),
(2, 'B', 6),
(3, 'C', 10),
(4, 'D', 12),
(5, 'E', 15),
(6, 'F', 21),
(8, 'B', 16),
(7, 'F', 18),
(9, 'F', 25);
I need output in arrays to use.. i.e. :
[
['range','A','B','F'],
['0.00 - 4.41', 1, 1, 0],
['4.41 - 8.24', 0 , 1, 1]
...
...
['others', 0, 0, 1]
]
This is what I am tring (Fiddle) :
select range, array_agg(name) as name, array_agg(count) as count
from (
select case
when no between 0.00 and 4.41 then '0.00 - 4.41'
when no between 4.41 and 8.24 then '4.41 - 8.24'
when no between 8.24 and 14.77 then '8.24 - 14.77'
when no between 14.77 and 19.35 then '14.77 - 19.35'
when no between 19.35 and 23.00 then '19.35 - 23.00'
else 'Others' end as range, name, count (*) as count
from grade
WHERE name IN ('A','B','F')
group by range, name
order by name
) t
group by range
Is it possible to get desired output from db query only ? or I have use php to iterate through?
Found a way to archive result (Updated Fidddle):
select range, sum(a) as a_name,
sum(b) as b_name,
sum(f) as f_name,
sum(count) as total
from (
select case
when no between 0.00 and 4.41 then '0.00 - 4.41'
when no between 4.41 and 8.24 then '4.41 - 8.24'
when no between 8.24 and 14.77 then '8.24 - 14.77'
when no between 14.77 and 19.35 then '14.77 - 19.35'
when no between 19.35 and 23.00 then '19.35 - 23.00'
else 'Others' end as range,
count(name = 'A' OR NULL) as A,
count(name = 'B' OR NULL) as B,
count(name = 'F' OR NULL) as F,
count (*) as count
from grade
WHERE name IN ('A','B','F')
group by range, name
order by name
) t
group by range

converting a php array into a database schema

Currently I am storing adjacencies in a php file in an array. Here's a sample of it:
$my_neighbor_lists = array(
1=> array(3351=> array (2, 3, 5 , 6, 10)),
2=> array(3264=> array (322, 12, 54 , 6, 10), 3471=>array (122, 233, 35 , 476, 210)),
3=> array(3309=> array (52, 32, 54 , 36, 210), 3469=>array (152, 32, 15 , 836, 10)),
etc
I would like to basically migrate this into a db. Any suggestions on how many table I should have? I am looking at three tables here.
two tables:
1. vertices (id)
2. edgecost (idfrom, idto, time, cost)

Categories