Parsing JSON Array with PHP foreach

AmitDiwan
Updated on 07-Apr-2020 11:11:49

7K+ Views

The below code can be used to parse JSON array −Example Live Demo

Use RegexIterator in PHP

AmitDiwan
Updated on 07-Apr-2020 11:09:41

341 Views

Regular expression$directory = new RecursiveDirectoryIterator(__DIR__); $flattened = new RecursiveIteratorIterator($directory); // Make sure the path does not contain "/.Trash*" folders and ends eith a .php or .html file $files = new RegexIterator($flattened, '#^(?:[A-Z]:)?(?:/(?!\.Trash)[^/]+)+/[^/]+\.(?:php|html)$#Di'); foreach($files as $file) {    echo $file . PHP_EOL; }Using filtersA base class holds the regex that needs to be used with the filter.classes which will extend this one. The RecursiveRegexIterator is extended.abstract class FilesystemRegexFilter extends RecursiveRegexIterator {    protected $regex;    public function __construct(RecursiveIterator $it, $regex) {       $this->regex = $regex;       parent::__construct($it, $regex);    } }They are basic filters and work ... Read More

Check If Today Is Monday or 1st of the Month Using Timestamp in PHP

AmitDiwan
Updated on 07-Apr-2020 11:06:19

1K+ Views

The date function can be used to return the string formatted based on the format specified by providing the integer timestamp or the current time if no timestamp is givenThe timestamp is optional and defaults to the value of time().Example Live Demoif(date('j', $timestamp) === '1')    echo "It is the first day of the month today"; if(date('D', $timestamp) === 'Mon')    echo "It is Monday today";OutputThis will produce the following output −It is the first day of the month today

Create Nested JSON Object in PHP

AmitDiwan
Updated on 07-Apr-2020 11:01:47

2K+ Views

JSON structure can be created with the below code −$json = json_encode(array(    "client" => array(       "build" => "1.0",       "name" => "xxxx",       "version" => "1.0"    ),    "protocolVersion" => 4,    "data" => array(       "distributorId" => "xxxx",       "distributorPin" => "xxxx",       "locale" => "en-US"    ) ));

Add PHP Variable Inside Echo Statement as HREF Link Address

AmitDiwan
Updated on 07-Apr-2020 10:59:20

4K+ Views

HTML in PHPecho "Link";orecho "Link";PHP in HTML

Sort Multidimensional Array by Inner Array Field in PHP

AmitDiwan
Updated on 07-Apr-2020 10:58:27

265 Views

The usort function can be used to sort a multidimensional array. It sorts with the help of a user defined function.Below is a sample code demonstration −Examplefunction compare_array($var_1, $var_2) {    if ($var_1["price"] == $var_2["price"]) {       return 0;    }    return ($var_1["price"] < $var_2["price"]) ? -1 : 1; } usort($my_Array,"compare_array") $var_1 = 2 $var_2 = 0OutputThis will produce the following output −1Explanation − We have declared var_1 and var)2 with integer values. They are compared and the result is returned.

List Files Recursively in C#

George John
Updated on 07-Apr-2020 10:06:49

2K+ Views

To get the list of files in a directory, use the SearchOptions.AllDirectories in C#.Firstly, set the directory for which you want the files −string[] myFiles = Directory.GetFiles("D:\New\", "*.*", SearchOption.AllDirectories);The following is an example displaying files from the above mentioned directory −Exampleusing System; using System.Linq; using System.IO; class Program {    static void Main() {       string[] myFiles = Directory.GetFiles("D:\New\", "*.*", SearchOption.AllDirectories);       foreach (string res in myFiles) {          Console.WriteLine(res);       }    } }OutputThe following is the output. It lists all the directories of the folder −D:\New\one.txt D:\New\two.html D:\Newature.png

Get the Number of Bytes in a File in C#

Ankith Reddy
Updated on 07-Apr-2020 10:04:34

499 Views

FileInfo type has a Length property that determines how many bytes a file has.Firstly, set the file −FileInfo file = new FileInfo("D:ew");Now use the Length property −file.LengthHere is the complete code −Exampleusing System; using System.Linq; using System.IO; class Program {    static void Main() {       FileInfo file = new FileInfo("D:ew");       long res = file.Length;       Console.WriteLine("Bytes: "+res);    } }OutputThe following is the output −3259244

Reset JShell Session in Java 9

raja
Updated on 07-Apr-2020 09:22:26

610 Views

Java 9 has introduced JShell for Java, and it allows us to evaluate code snippets such as declarations, statements, and expressions.During the JShell session, we need to reset it without closing and re-opening JShell then we can use the internal command: "/reset". By using this command, code entered during the current session has erased. It can be useful when we want to test new classes, create new variables, etc. while keeping the names previously used.In the below snippet, we have created variables x, y, and str. We can able to see all entered code snippets using the "/list" command. After that, we can apply ... Read More

PHP String Cast vs strval Function: Which One Should I Use?

AmitDiwan
Updated on 06-Apr-2020 14:35:36

968 Views

A value can be converted into a string with the help of (string) cast or the strval() function.The strval() function is a function call whereas (string) cast is an internal type casting method.Unless there is some specific dataset or use case, both of these can be used interchangeably.This is because PHP uses automatic type conversion, due to which a variable's type is determined based on the context in which it is used.The strval($var) function returns the string value of $var whereas the (string)$var explicitly converts the "type" of $var during the process of evaluation.The $var can be any scalar type ... Read More

Advertisements