PHP Array Comparison without caring order,count & Display Count - php

I have 2 arrays named
$user = array("Joomla","Java");
and
$cpny= array("PHP","Java","Joomla");
I want to compare this arrays without caring the Order and Count (no: of elements) and also want to Display the Count (no: of elements matched) after the Comparison.
Thanks in Advance
I have some more doubts and i post along with this Question..
These above 2 arrays are Belonging to the Skills of a Candidate and Required Skill for a Job that Posted by a Company(My Project is a Job Portal). To select candidates,company needs to search candidates with required skills.So above comparison take places and the output will be 0,1,2,...etc.(no: of elements matched).assume that value is 3
Now i want to select candidates whose skill is perfectly matched with required skill(comparison o/p =3),and display it in top of the table,then 2(comparison o/p =2) and then 1(comparison o/p =1)

There is a specific method in PHP to achieve this, see php_intersect
In your case, you could do something like
$similar = array_intersect($user, $cpny);
$similar_count = sizeof($similar);
echo($similar_count);

Related

laravel conditional join based on optional external keys

I got this db structure:
ddts contains 3 optional external keys (only one of panel_id, sawn_id or veneer_id can contains an external id and other 2 equals to null) .
So one ddt can be exatly just one of this 3 types:
sawn
panel
veneer
I need to extract for every company_id(another external key) i will sum some data from panels,sawns and veneers, but before sum it i need even to convert some of them in kgs too (with a function implemented by me).
In the ddts model ive the methods: panel(),sawn(),veneer();
I need just the final sum, but i guess that for achieve this i need to build up a huge collection and then manipulate it...
Id like to understand what is best to doing by query and what by code.
My first approach was about:
select all companies
in a foreach loop all ddts for each company
in a foreach loop for each ddt associate a type by selecting it
trough an if condition steatement
in a forache loop selection the value to sum
convert it to kgs where is necessary
sum it;
but it seems so long and im quite shure that the point 2 and 3 should be done by a JOIN but not so clare how!

Defining arrays according to the id

I do not where to start bu I will be as precise as I can be. There are over 200 customer in the database. I use php.
I made 2 calculations and assigned new values into an array but the results of these calculations will be used in another calculation. When I click on the calculate button it should first take the data from the arrays ($snduz=array_values($array5)[0]; and $test=array_values($array4)[0]; and $snduz=array_values($array5)[1]; and $test=array_values($array4)[1]; so on) that belong to related id put that value into the formula and calculate.
When I put the arrays 1 by 1 it works e.g.
$array5[] = ($sab1*(($test-$ort)/$sdsp)+$sab2);
$snduz=array_values($array5)[0];
$snduz1=array_values($array5)[1];
$snduz2=array_values($array5)[2];
.....
$array4[] = ($tams*$enku)/$row4['sn'];
$test=array_values($array4)[0];
$test1=array_values($array4)[1];
$test2=array_values($array4)[2];
...
but I want to get them calculated automatically

mysql filter rows by field value in linked table

Scenario:
I have a main table, say "items". Each item can be of many types, and I use a table "types" and a linked table to keep the relations between the two, say "items_types" in a typical one to many situation. Now, each type has a supertype, with a straight one to one relation.
items: i_id, i_name
types: t_id, t_name, t_st_id
items_types: it_id, it_i_id, it_t_id
supertypes: st_id, st_name
Now I have to filter all the items that are related to at least one supertype in a given set, say supertypes with ids 1,2,3
I am thinking to use a group_concat on the ids of the related supertypes and then filtering using multiple FIND_IN_SET in or between each other, using a WHERE clause.
However this, if working, would slow down the query and I don't like comparing ids as if they were strings.
Any idea?
You can do like this.
select i.*
from items_types it
join items i on i.i_id = it.it_i_id
join types t on t.t_id = it.it_t_id
join supertypes st on st.st_id = t.t_st_id and st_id in (1,2,3)
I found a better solution: instead using related records to keep track of all the types the item belongs to, I am giving each type a numeric code which can be used in binary comparison, e.g:
Type 1: code 1
Type 2: code 2
Type 3: code 4
Type 4: code 8
Type 5: code 10
I think I gave the idea.
Each item will have a field to keep track of all its types, named 'coded_type'. For example if a field has coded_type 7 it means that it belongs to Type 1, Type 2, Type 3.
For instance, if a user then wants to look for all items that are of type 2 or 3 the query will look for all items whose coded_type field binary ANDed against 6 gives a value greater than zero.
That is:
select * from items where (coded_type & 6)>0
I think this way the query will much simpler and run faster. Sorry I thought of this just now.

Ordering an array and then allocating a position based on where elements are

I have an array $orderedUsers in which I have sorted user ID's in a particular order. E.g. $orderedUsers=[6][4][11][2][18][7][5][10][12]. I will be using the order of this array to show a leaderboard based on where the users are in the array, so what I am asking is: how would I get the position of each element in the array. In the above example USERID=6 would be FIRST, USERID=4 would be SECOND, USERID=11 would be THIRD and so on...
Thanks for any help or suggestions
`array_search($needle,$haystack);
$available_colors = ['red','blue','yellow','orange','green'];
$color_position = array_search('yellow', $available_colors)); // 2

MongoDB Indexing vs Array Implementation for our specific application

Here is the issue. We are working with MongoDB-PHP.
In our application, we have many user groups where users can make posts. Presently we are maintaining the post ids these groups in the document of that group in array format. So that, when we need to grab first 10 posts we can grab them from the array using slice operation.
Eg: Case 1
collection posts: //this collection stores all the posts of various groups
{
{"_id":"1","post_text":"....",...}
{"_id":"2","post_text":"....",...}
} `
collection groups: //this collection contains documents for each group
{
{
"_id":"1"
"name":"Group ABC",
"post_ids":{"1","2"...."100"}
//1,2..100 represents MongoIDs of corresponding posts of this group
//so i can slice first 10 posts of this group when someone visits this page
}
}
`
In contrast to storing these post ids in document of the group, if we use indexing on group id and store that in posts collection.
Eg: Case 2
collection posts
{
{"_id":"1","group_id":"1","post_text":"....",...}
{"_id":"2","group_id":"2","post_text":"....",...}
}
Also note that in Case 1 we do not have to apply any sorting operations as array elements are pushed in order while in Case 2 we will have to apply sort(by timestamp criteria) after the find operation, which would read all documents from memory and then apply sorting on them.
Whose performance would be better taking into consideration that indexes would be stored in RAM ?
Please let me know if the issue is not clear from this question.
Doing one query (case #2) would be faster than doing two queries. Also, making documents bigger (e.g., appending new posts to post_ids in #1) is a fairly slow operation.

Categories