HI GEEKS
I am working in PHP and Mysql. I have an array from database, in which I get the values if field name of a form i.e., Name, Company Name, etc. There are lots of field in the array.
I want these field to be displayed in my desired sequence, likewise:
Name:
Company Name:
Date of Birth:
Address:
These values are available in array in unordered format likewise:
array(
Company Name,
Address,
Date Of Birth,
Name
)
Now my question is how can manipulate this array to g开发者_开发百科et the desired sequence in display of fields as shown above in PHP?
I think the better way is you should print them as you need instead of doing sorting on array based on custom desire something like:-
echo $array['Name'];
echo $array['Company Name'];
and so on OR
if the fields are dynamic and edited by admin then you have to add one more field in your mysql database and store the order number of all the fields by which value to be printed on page.
Thanks
You are using associative array. You can output each key,value in any order you like.
If you want each ordering to be configurable, then simply store the key names in a list in order of appearance. Then display values according to this list of keys.
For eg : array name is user_info in which you have
array(
Company Name,
Address,
Date Of Birth,
Name
)
$user_info[0] = $user_info[3];
$user_info[1] = $user_info[0];
$user_info[2] = $user_info[2];
$user_info[3] = $user_info[1];
Unordered arrays do not exist in PHP. If you don't give them an explicit index, the elements will be indexed numerically starting from 0.
Here is an array:
$pizzas = array(
'boston',
'pepperoni',
'cheese'
);
To get the third value, use
$pizzas[2]
Let's say I want to echo the first value, then the third, then the second:
echo $pizzas[0].$pizzas[2].$pizzas[1];
EDIT: to reorder your array in the way you want, here is what you can do (let's take you would like it ordered as in the previous example:
$pizza_nicely_ordered = array (
$pizzas[0],
$pizzas[2],
$pizzas[1]
);
精彩评论