Arrays

Array, String, Date &Time function

Posted on Updated on

Arrays

An array stores multiple values in one single variable:

An array is a data structure that stores one or more similar type of values in a single value. For example if you want to store 100 numbers then instead of defining 100 variables its easy to define an array of 100 length.

Example

<?php
$cars=array(“Volvo”,”BMW”,”Toyota”);
echo “I like ” . $cars[0] . “, ” . $cars[1] . ” and ” . $cars[2] . “.”;
?>

O/P: I like Volvo, BMW and Toyota.

There are three different kind of arrays and each array value is accessed using an ID c which is called array index.

  • Numeric array – An array with a numeric index. Values are stored and accessed in linear fashion
  • Associative array – An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.
  • Multidimensional array – An array containing one or more arrays and values are accessed using multiple indices

Numeric Array

These arrays can store numbers, strings and any object but their index will be prepresented by numbers. By default array index starts from zero.

Example

Following is the example showing how to create and access numeric arrays.

Here we have used array() function to create array. This function is explained in function reference.

<html>

<body>

<?php

/* First method to create array. */

$numbers = array( 1, 2, 3, 4, 5);

foreach( $numbers as $value )

{

echo “Value is $value <br />”;

}

/* Second method to create array. */

$numbers[0] = “one”;

$numbers[1] = “two”;

$numbers[2] = “three”;

$numbers[3] = “four”;

$numbers[4] = “five”;

foreach( $numbers as $value )

{

echo “Value is $value <br />”;

}

?>

</body>

</html>

This will produce following result:

Value is 1Value is 2Value is 3Value is 4Value is 5Value is oneValue is twoValue is threeValue is fourValue is five

Associative Arrays

The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of their index. Associative array will have their index as string so that you can establish a strong association between key and values.

To store the salaries of employees in an array, a numerically indexed array would not be the best choice. Instead, we could use the employees names as the keys in our associative array, and the value would be their respective salary.

NOTE: Don’t keep associative array inside double quote while printing otheriwse it would not return any value.

Example

1

This will produce following result:

2

Multidimensional Arrays

A multi-dimensional array each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. Values in the multi-dimensional array are accessed using multiple index.

Example

In this example we create a two dimensional array to store marks of three students in three subjects:

This example is an associative array, you can create numeric array in the same fashion.

3

This will produce following result:

4

String Concatenation Operator

To concatenate two string variables together, use the dot (.) operator:

5

If we look at the code above you see that we used the concatenation operator two times. This is because we had to insert a third string.

Between the two string variables we added a string with a single character, an empty space, to separate the two variables.

Using the strlen() function

The strlen() function is used to find the length of a string.

Let’s find the length of our string “Hello world!”:

<?php

echo strlen(“Hello world!”);

?>

This will produce following result:

12

The length of a string is often used in loops or other functions, when it is important to know when the string ends. (i.e. in a loop, we would want to stop the loop after the last character in the string)

Using the strpos() function

The strpos() function is used to search for a string or character within a string.

If a match is found in the string, this function will return the position of the first match. If no match is found, it will return FALSE.

Let’s see if we can find the string “world” in our string:

<?php

echo strpos(“Hello world!”,”world”);

?>

This will produce following result:

6

As you see the position of the string “world” in our string is position 6. The reason that it is 6, and not 7, is that the first position in the string is 0, and not 1.

Date and Time

The PHP Date() Function

The PHP date() function formats a timestamp to a more readable date and time.

Syntax

date(format,timestamp)

Parameter Description
format Required. Specifies the format of the timestamp
timestamp Optional. Specifies a timestamp. Default is the current date and time

A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred.

Get a Simple Date

The required format parameter of the date() function specifies how to format the date (or time).

Here are some characters that are commonly used for dates:

  • d – Represents the day of the month (01 to 31)
  • m – Represents a month (01 to 12)
  • Y – Represents a year (in four digits)
  • l (lowercase ‘L’) – Represents the day of the week

Other characters, like”/”, “.”, or “-” can also be inserted between the characters to add additional formatting.

The example below formats today’s date in three different ways:

Example

<?php
echo “Today is ” . date(“Y/m/d”) . “<br>”;
echo “Today is ” . date(“Y.m.d”) . “<br>”;
echo “Today is ” . date(“Y-m-d”) . “<br>”;
echo “Today is ” . date(“l”);
?>

O/P:

Today is 2014/08/23
Today is 2014.08.23
Today is 2014-08-23
Today is Saturday

Get a Simple Time

Here are some characters that is commonly used for times:

  • h – 12-hour format of an hour with leading zeros (01 to 12)
  • i – Minutes with leading zeros (00 to 59)
  • s – Seconds with leading zeros (00 to 59)
  • a – Lowercase Ante meridiem and Post meridiem (am or pm)

The example below outputs the current time in the specified format:

Example

<?php
echo “The time is ” . date(“h:i:sa”);
?>

O/P: The time is 05:42:07am

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com

Data structures in C

Posted on

USEFUL FOR FYBSC (IT), FYBCA STUDENTS  

 

1. The memory address of the first element of an array is called

a. floor address
b. foundation address
c. first address
d. base address

 

2. The memory address of fifth element of an array can be calculated by the formula

a. LOC(Array[5]=Base(Array)+w(5-lower bound), where w is the number of words per memory cell for the array
b. LOC(Array[5])=Base(Array[5])+(5-lower bound), where w is the number of words per memory cell for the array
c. LOC(Array[5])=Base(Array[4])+(5-Upper bound), where w is the number of words per memory cell for the array
d. None of above

 

3. Which of the following data structures are indexed structures?

a. linear arrays
b. linked lists
c. both of above
d. none of above

 

4. Two dimensional arrays are also called

a. tables arrays
b. matrix arrays
c. both of above
d. none of above

 

5. Which of the following data structure can’t store the non-homogeneous data elements?

a. Arrays
b. Records
c. Pointers
d. None

 

6. Which of the following data structure store the homogeneous data elements?

a. Arrays
b. Records
c. Pointers
d. None

 

7. Binary search algorithm cannot be applied to

a. sorted linked list
b. sorted binary trees
c. sorted linear array
d. pointer array

 

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com