How to do maximum array intersection PHP [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I ask a candidate : What do you like ?
His answer is an array [Math,Physics,Chemistry,IT,Sports]
I do have a list :
Software Analyst : [Math,IT,Programing,Stress,Computer]
Truck drive : [Sports,Driving,Traveling,Exchange]
Physic-chemistry teacher :[Math,Physics,Chemistry,Teaching]
What is the best way in PHP to get the jobs with the maximum common tags with the user input?

You use array_intersect() to find the common tags, then compare among the returned tags amount.
$jobs = array(
'software' => array('Math','IT','Programing','Stress','Computer')
'truck' => array('Sports','Driving','Traveling','Exchange')
'physic' => array('Math','Physics','Chemistry','Teaching')
);
$user_tags= array('Math','Physics','Chemistry','IT','Sports');
$max_common = 0;
$desired_job = '';
foreach($jobs as $key=>$job) {
$common = count(array_intersect($job,$user_tags);
if(max_common < $common) {
$desired_job = $key;
$max_common = $common;
}
}

Related

Count rows with model::count laravel [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I am trying to count the records in the table, but it's showing me nothing in the response, I am working on an API, can anybody help please ?
public function devicesCount()
{
$x = Device::count();
if ($x) {
return ['devices' => $x];
} else {
return ['message' => 'ranii karrezt ydyyyyyyk'];
}
}
use $x = Device::all()->count(); in place of $x = Device::count();
or use Laravel Aggregates like
$count = DB::table('devices')->count();

Foreach loop php conversion [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I execute on my main page some php code which includes :
foreach ($fbdata->feed->data as $fbpost)
{
...
}
How can we convert this , into a loop that goes from (i to z)(0 to 10) ?
Simple for loop
for($i = 0; $i < 10; $i++) {
$fbpost = $fbdata->feed->data[$i];
...
}
or if you like to use the as, try using a foreach but slicing the array before using it
$fbPosts = array_slice($fbdata->feed->data, 0, 10);
foreach($fbPosts as $fbpost) {
...
}

How to extract the value in double quotation? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to extract the value of the attributes of a html tag that is defined as a php variable:
[video src="http://localhost/video.mp4" poster="http://localhost/thumb.jpg" preload="none"][/video]
values ​​of src and poster should be separated and cast to array
if your text is a php variable, you can use preg_match :
<?php
$text = '[video src="http://localhost/video.mp4" poster="http://localhost/thumb.jpg" preload="none"][/video]';
preg_match('/src=\"([^\"]+)\"/si', $text, $src);
preg_match('/poster=\"([^\"]+)\"/si', $text, $poster);
$array['src'] = $src[1];
$array['poster'] = $poster[1];
print_r($array);
?>
also you can use simplehtmldom
You can use an html parser for this. See this one: http://simplehtmldom.sourceforge.net/.
$html = str_get_html('<video src="http://localhost/video.mp4" poster="http://localhost/thumb.jpg" preload="none"></video>');
foreach($html->find('video') as $element)
{
$array['src'] = $element->src;
$array['poster'] = $element->poster;
}

How can i identify position of MAX value in array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to get 112 position [01] and use this position[01] to compare something ? Can i do that ? Sorry about stupid question. I'm just a new guy really want to known. Thank for any help.
<?
$sql "SELECT * FROM truck WHERE t_ID = '001'";
$arry = odbc_fetch_array($DataExec);
echo "MAX value position's".$arry;
?>
echo $arry. This 1 i want to show [ MAX value position's 01.]. Or someone have better way.
<?php
$arr_numeric= array('41', '112', '25','54');
$max_position = array_search(max($arr_numeric), $arr_numeric);
echo $max_position;
?>
position start from 0.
OUTPUT
1
$arr = array('41', '112', '25','54');
$index = array_search(max($arr),$arr);
echo "MAX value position's".$index;
Similar Question here
I get it with a google search.
$maxs = array_keys($array, max($array))
Note:
this way you can retrieve every key related to a given max value.
If you are interested only in one key among all simply use $maxs[0]

condition statement doesn't work [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
i'm trying to do a simple condition check like:
$test = 1;
$test_ = $test==-1?"Test--1":$test==1?"Test-1":$test==0?"Test-0":"Test";
echo $test_;
The output is Test-0 but must be Test-1.
Which could be the problem?
Try this : Put brackets for conditions.
$test = 1;
$test_ = ($test==-1)?"Test--1":($test==1?"Test-1":($test==0?"Test-0":"Test"));
echo $test_;
output :
Test-1
change this
$test_ = $test==-1?"Test--1":$test==1?"Test-1":$test==0?"Test-0":"Test";
to
$test_ = $test==-1?"Test--1":($test==1?"Test-1":($test==0?"Test-0":"Test"));
working example http://viper-7.com/msuCyk
// output Test-1
Ternary operator in PHP is left to right, therefore, its executed like this:
$test_ = (( ( $test==-1 ? "Test--1" :$test==1) ? "Test-1":$test==0)?"Test-0":"Test");

Categories