Using more than one array in loop in PHP

Usually we use foreach loop when there is a need to use only one array. But what if there are more than one array…

Foreach loop works only with a single array. To step through multiple arrays it’s better to use the each() function in a while loop. Have a look at it..


while(($code = each($codes)) && ($name = each($names))) {
echo '<option value="' . $code['value'] . '">' . $name['value'] . '</option>';
}

each() returns information about the current key and value of the array and increments the internal pointer by one, or returns false if it has reached the end of the array. This code would not be dependent upon the two arrays having identical keys or having the same sort of elements. The loop terminates when one of the two arrays is finished.

Difference between == and === operator in PHP

When you want to compare values in PHP then you can use either == or === operator. But these both operator are not same. It’s good to know the difference between them.

These both operators fall under the category of Comparison Operators. The first operator ‘==’ is known as Equal Operator in PHP and checks for the equal values and the second operator ‘===’ is known as Identical Operator and checks for equal values and it also checks that if both the values are of same variable type or not.

How to remove index.php from url in CodeIgniter

Yes, you can remove the index.php from your url in your CodeIgniter web application.

You just need to add a .htaccess file in your web application folder.

Create a file and give it name .htaccess and save it in the root folder, and then copy the below block of code and paste it in the .htaccess file.


<IfModule mod_rewrite.c>

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?/$1 [L]

</IfModule>

One more change you have to do in your config.php file.

Go to :

application/config/config.php

and remove index.php from $config[‘index_page’]

All Done. Enjoy!

Using PHP Array in Javascript

If you have a PHP Array and want to perform some javascript operations by looping the PHP Array, then I recommend you to use PHP foreach loop instead of Javascript forEach loop.


function myfunction() {

<?php foreach($myArray as $val) { ?>

console.log('<?=$val;?>');

<?php } ?>

}

 

How to know CodeIgniter Version

To know the current version of CodeIgniter you are using, you just need to echo a constant.


<?php echo CI_VERSION; ?>

Another way to know the version of CodeIgniter is to check it manually in the file where the constant CI_VERSION is declared.

The constant CI_VERSION is declared in :

system/core/CodeIgniter.php

You can check it there as well if you don’t prefer or like the first option.

 

Merging and Sorting two arrays in PHP

Suppose we have two PHP arrays. Now if we want to merge these two array, then there is a function in PHP to do this. Just use the below code to merge two arrays in PHP.


$combinedArray = array_merge($firstArray, $secondArray);

The above code will return a new combined array of the two arrays.

Now if we want to sort this new array, then :


foreach($combinedArray AS $val) {

$dates[] = strtotime($val['date']);  // here 'date' is the key in our $firstArray and $secondArray and our new array will be sorted according to this.

}

array_multisort($dates, SORT_ASC, $combinedArray);

Note :- Both the arrays $firstArray and $secondArray must have the ‘date’ key, because the new array $combinedArray will be sorted according to this.