0

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 get the desired sequence in display of fields as shown above in PHP?

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

SHakti all the fields are dynamic which could be edited or newly added from administrator, so specifying qith name is not feasible
2

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.

Comments

0
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];

1 Comment

Again I even dont know your right hand side array contain which value in which index its all displayed from Database
0

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]
);

1 Comment

Not talking about indexing my friend I am talking about array with values in ubdesired order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.