0

not sure if this is the best way to do what I need but this is my issue. In my edit function view I recreate a forms fields based on what's in $this->data->expense but I need the array to be in a certain order or the fields get generated in the wrong order. This is the array I have:

'Expense' => array(
    (int) 0 => array(
        'id' => '98',
        'date' => '2012-08-23',
        'sitename' => '123',
        'detail' => 'Breakfast',
        'amount' => '100.00',
        'miles' => null,
        'total' => '100.00',
        'expense_claim_id' => '63',
        'created' => '2012-08-23 09:08:52',
        'modified' => '2012-08-23 09:08:52',
        'ExpenseClaim' => array(
            'id' => '63',
            'user_id' => '3',
            'claim_status_id' => '1',
            'created' => '2012-08-23 09:08:52',
            'modified' => '2012-08-23 10:14:10',
            'approved' => false,
            'approved_by' => '0',
            'date_submitted' => '2012-08-23 09:08:52'
        ),
        'ExpenseCode' => array(
            (int) 0 => array(
                'id' => '1',
                'name' => 'Plane fare',
                'code' => '1',
                'created' => '2012-07-31 09:52:02',
                'modified' => '2012-07-31 09:53:57'
            )
        )
    ),

This is how I need it to be ordered (ExpenseCode) appears higher up:

'Expense' => array(
    (int) 0 => array(
        'id' => '98',
        'date' => '2012-08-23',
        'sitename' => '123',
        'detail' => 'Breakfast',
        'ExpenseCode' => array(
            (int) 0 => array(
                'id' => '1',
                'name' => 'Plane fare',
                'code' => '1',
                'created' => '2012-07-31 09:52:02',
                'modified' => '2012-07-31 09:53:57'
            )
        ),
                    'amount' => '100.00',
        'miles' => null,
        'total' => '100.00',
        'expense_claim_id' => '63',
        'created' => '2012-08-23 09:08:52',
        'modified' => '2012-08-23 09:08:52',
        'ExpenseClaim' => array(
            'id' => '63',
            'user_id' => '3',
            'claim_status_id' => '1',
            'created' => '2012-08-23 09:08:52',
            'modified' => '2012-08-23 10:14:10',
            'approved' => false,
            'approved_by' => '0',
            'date_submitted' => '2012-08-23 09:08:52'
        )
    ),

How can I achieve this and will changing the structure of it affect cake when I post?

2
  • 1
    Why do you need that? Don't you call the values with their keys? Commented Aug 23, 2012 at 12:02
  • What is the criteria you need to be sort an array? Commented Aug 23, 2012 at 12:03

2 Answers 2

1

As far as I know, there is no built-in function on CakePHP to custom sort an array. I guess you will have to do this with pure PHP. https://stackoverflow.com/search?q=custom+sort+array+php

Unless you must have the fields created dynamically, I suggest you to add each field yourself.

will changing the structure of it affect cake when I post?

No, if you simply put the fields in a different order this won't affect CakePHP. The data sent via POST will be in array $this->data['Expense']. When saving the data with $this->model->save(), the order does not matter.

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

1 Comment

Agreed, after taking a break and revisiting it I restructured the loops that were creating the form helper fields thus creating the fields in the ordered I needed them. I was getting myself tied up up array structure and tried to achieve too much dynamically rather than hardcoding certain elements that would never change. Thanks for the input. As this is the most relevant answer to the actual question asked I'll make this as correct.
0

This makes no sense. If the order of the data in an associative array is causing your HTML fields/form inputs..whatever to be in the wrong order, you're doing it wrong.

Try using the keys instead of just looping through in order. Or read more about associative arrays - there are a lot of fun / easy things to do to use & manipulate their data... but again to reiterate, there is NO need to return the data in a specific order.

Edit:

You could always create an array of keys and repeat through those. Might be a cleaner way of managing the order.

1 Comment

Agreed, after taking a break and revisiting it I restructured the loops that were creating the form helper fields thus creating the fields in the ordered I needed them. I was getting myself tied up up array structure and tried to achieve too much dynamically rather than hardcoding certain elements that would never change. Thanks for the input.

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.