Monday, November 11, 2013

PHP array print method in proper format

Hiiiii, If you are a PHP Developer then you generally face a situation when you want to echo a complete array in the complete array format with indexes in the complete proper format. You get this array echo problem when you post the form variables on next page or on same page, when you fetch the records from database or when you the variables in URL.



So, in this post I'll be telling you how to echo complete array elements in all the above discussed methods :

Code Begins :

--> Form Method :
<form action="" method="POST"> 
<input type="text" name="first name">
<input type="text" name="last name">
<input type="submit" value="submit">
</form>

To get the variables of this form in the array format , use the code below :
<?php
echo "<pre>";
print_r($_POST);
echo "</pre>";
?>

--> URL Method :
http://www.example.com/?last_name=abc&first_name=def

To get the variables of this url in the array format , use the code below :
<?php
echo "<pre>";
print_r($_REQUEST);
echo "</pre>";
?>

--> Fetch Array from Database :
$query=mysql_query("select * from tablename");  this table is having only two columns - first_name and last_name
$result=mysql_fetch_array($query);

To display this database fetched result in the array format , use the code below :
<?php
echo "<pre>";
print_r($result);
echo "</pre>";
?>


All this above examples will give the output as :

Array
(
    [first_name] => abc
    [last_name] => def
)


Hope you like this post……..Please Comment…………!!!!!!!!!


Tags:

0 Responses to “PHP array print method in proper format”

Post a Comment

© 2013 MyCodeStock. All rights reserved.
Designed by SpicyTricks