What is an Array in PHP

An array came as a PHP variable. It can store multiple values in a single array. It can be one dimensional or multidimensional too. You can use numeric value or and string as an index of the array.

Simple Array Example:

<?php

$array = array(

    “fname” => “First name”,

    “lname” => “Last name”,

);

?>

There are following types of Arrays:

  • Indexed arrays – An array with numeric index or keys
  • Associative arrays – An array with named or string index or keys
  • Multidimensional arrays – An array contains one or more arrays.

Indexed arrays:

Here is an example of indexed array:

$name= array(‘A’,’B’,’C’);

Or

You can write it like:

$name[0]=’A’;

$name[1]=’B’;

$name[2]=’C’;

If you want to use any specific indexed value in your program then you can use the below example too:

Q: Create an array of names and print second name of array.

A:  To do this you need to write the following:

<?php

//Creating an array

$name=array(‘Tim’,’Peter’,’John’,’Anna’);

echo $name[1];

//Here we used index 1 as array index starts from 0. This will return an output Peter.

?>

Associative arrays : Associative array is an array which used named key as index like

$student_name=array(‘fname’=>’Aarav’,’lname’=>’Maurya’);

Or you can assign values as follows:

$student_name[‘fname’]=’Aarav ’;

$student_name[’lname’]=’Maurya ’;

Multidimensional arrays: An array with one or more array called Multidimensional arrays. You can manage multiple level arrays in php but mostly more than 3 level will be hard to manage or keep remembering for programming.

Here is an example of students of class:

$students=array(

array(‘fname’=>‘Tom,’lname’=>Holland,’e_no’=>1,’marks’=>85),

array(‘fname’=>‘Aarav ’,’lname’=>’Maurya ’,’e_no’=>2,’marks’=>80),

);  

What is 301 Redirection

301 redirect is a permanent redirect through that website owner redirects old page to new permanently.

Google and other search engines also use new URL for listing. So all visitors and search engines redirect on new page. Google and other search engines also pass the 90% to 99% ranking on new URL too.

Basically 301 redirect is HTTP status code. Which is used to move the one page permanently on new page.

We can implement the 301 redirect through .htaccess or coding. In both methods we should define the OLD and New both urls.

New URL uses as a response of 301 HTTP code.

Let’s see this with an example.

If we have a website nitinmaurya.com and there is an OLD page that url is nitinmaurya.com/oldpage but now I have redesigned the website or whatever I did but new page URL has been changed nitinmaurya.com/oldpage to nitinmaurya.com/newpage. In that case we need to implement the 301 redirection.

We should avoid the chain redirection too as this will harm the SEO and this is bad for users too.

Export Mysql Table Data in csv format through single query

Import and Export database or tables is really an important factor of coding and it is most important thing for every programmer. So sometimes we write lots of code to export any table from database in csv format. I am also a PHP programmer so I know these things. I found a good solution to export any table from database in csv format. Here is a query.

SELECT * FROM tablename INTO OUTFILE '/filename.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n’

So you can now easily export and table in csv format using above query in your code.

Generate Alpha Numeric number

Some times you want to generate Alpha Numeric , Numeric or Alphabetic digit for password or some kind of number. It can be 2 to 16 digit. For these problem i have a solutions:

function generateId($length, $type = 'ALPHANUMERIC') {

if($type == 'ALPHANUMERIC')
$chars = "12345CDEFGHI67890NOPQRSTU01492VWXYZ43210XYZFGH98765ABCDEF83574GHIJKLM09876QRSTUV54321";
else if($type == 'NUMERIC')
$chars = "1234567890987654321678905432101928374651029384756162738495061728394055432167890";
$id = "";
while (strlen($id)<$length) {
$id .= $chars{mt_rand(0,strlen($chars))};
}

return $id;

}

generateId(12,  'ALPHANUMERIC');