Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

Monday, 4 January 2021

Perfect Ranking With Ties Using By PHP Array

 


<?php

#Ex:  Short Set Ranking Hosting Provider Founded By Year

// INPUT

$Hosting_Years = array(

    'Bluehost' => 2003,

    'Dreamhost' => 1996,

    'Hostinger' => 2004,

    'HostGator' => 2002,

    'A2 Hosting' => 2001,

    'GreenGeeks' => 2008,

    'WP Engine' => 2010,

    'Inmotion' => 2001,

    'SiteGround' => 2004,

    'Nexcess' => 2000,

);


function setRanking($standings) {

    $rankings = array();

    arsort($standings);

    $rank = 1;

    $tie_rank = 0;

    $prev_score = -1;

    foreach ($standings as $name => $score) {

        if ($score != $prev_score) {  //this score is not a tie

            $count = 0;

            $prev_score = $score; 

            $rankings[$name] = array('score' => $score, 'rank' => $rank);

        } else { //this score is a tie

            $prev_score = $score;

            if ($count++ == 0) {

                $tie_rank = $rank - 1;

            }

            $rankings[$name] = array('score' => $score, 'rank' => $tie_rank);

        }

        $rank++;

    }

    return $rankings;

}


// OUTPUT 

$set_Host_Ranking = setRanking($Hosting_Years);

foreach($set_Host_Ranking as $host => $value){

    echo "<br> Rank: ".sprintf("%03d", $value['rank']). " &emsp; Founded in  : ".$value['score']. "&emsp; Host: ".$host;

}

?>

According To Question:

#Ranking/Position on value of array in PHP

#Rank Of An Element In An Array

#What Is Rank Of An Array

#Array Ascending Order In PHP

#PHP Sort Multidimensional Array By Value Alphabetically

#Ksort Multidimensional Array PHP

#PHP Sort Multidimensional Array By Key

Friday, 1 January 2021

How Can I Remove A Specific Item From An Array by jQuery?




//INPUT
var array = [2020, 2021, 2022, 2023];

console.log(array);

var index = array.indexOf(2020);
if (index > -1) {
  array.splice(index, 1);
}

// OUTPUT  : array = [2021, 2022, 2023]
console.log(array);

Note: Run This Code In Browser Console For Testing

According To Question:
#Remove Specific Element From Array Javascript
#Javascript Remove Object From Array By Value
#Remove Element From Array Jav
#Array Filter
#Splice Javascript
#Jquery Remove Item From Array By Index
#Lodash Remove Item From Array
#Remove Object From Json Array Javascript

Tuesday, 11 August 2020

How To Search In PHP Array Element Containing String | Like Binod


$example = array(
        "who" => "Who is Binod?",
        "name" => "Dose Binod Tharu is Binod?",
"where" => "Where is Binod?",
"whose" => "Whose name is binod?",
"YouTube" => "Dose Binod in the YouTube?",
"meaning" => "What is Binod meaning?",
"fullform" => "What is Binod full form?",
"pronunciation" => "What is Binod pronunciation?",
"english" => "Dose Vinod in english",
"how" => "How to write Vinod in sanskrit?",
"nickname" => "Which best nickname for Vinod?",
"kon" => "Binod kon he?",
"kaha" =>"Binod kaha he?",
"kiska" =>"Binod kiska name he?"
);

$searchword = 'Binod';

$search_results = array_filter($example, function($var) use ($searchword) {
return preg_match("/\b$searchword\b/i", $var); 
});

echo "Search Word: ".$searchword;
echo "<br>Search Results";
echo "<pre>";
echo "Total Search: ".count($search_results);
echo "<hr>";
print_r($search_results);
echo "</pre>";

OutPut:

Tuesday, 31 December 2019

How to create and manage log file in php?


Create and manage crud operation log data PHP

<?php
function crud_log($action = '',$message = '',$array_data = array()){
    global $handle_crud_log;
    date_default_timezone_set("Australia/Sydney"); //Ex. set australia/sydney timezone   
    $crud_log_dir = "crud-log"; //Create this DIR(crud-log) on file dir location
   
    if (!file_exists($crud_log_dir)){
        // create directory/folder uploads.
        mkdir($crud_log_dir, 755, true);
    }   

    $quote_log_file_name =  "/crud_log_".date("m_Y").".log";
    $ip_address = @$_SERVER['REMOTE_ADDR']." #### ". @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $date_time = date("d/m/Y H:i:s");
    $action_name = $action; // (Ex. Create/Read/Update/Delete)
    $crud_msg = $message; //Create, Read, Update and Delete message;
    $array_data = $array_data; //Array data type Like $_POST
       
    $handle_crud_log = fopen($crud_log_dir.$quote_log_file_name, "a");
    fwrite($handle_crud_log, "=================================================\r\n");   
    fwrite($handle_crud_log, "At: {$date_time}\r\n");
    fwrite($handle_crud_log, "From: {$ip_address}\r\n");
    fwrite($handle_crud_log, "Action: {$action_name} \r\n");
    fwrite($handle_crud_log, "Message: {$crud_msg}\r\n");
    fwrite($handle_crud_log, "Data: ". print_r($array_data, true));
    fwrite($handle_crud_log, "=================================================\r\n\r\n");
}
$action = "Update";
$message = "Update emplyee detail";
$array_data = array("f_name"=>"PHP", "l_name"=>"Kishan");
crud_log($action, $message, $array_data); // call function for create log
?>

Thursday, 1 August 2019

How to define an empty object in PHP?

<?php   
    $ObjAraay = new stdClass();
    $ObjAraay->fname = "PHP";
    $ObjAraay->lname = "Beginner";

    echo "<pre>";
    print_r($ObjAraay);
    echo "</pre>";
?>

Result: