1

I have an array that looks like the following:

Data = array(
'Offer' => array(),
'Country' = array('Name' => Array()) 
)

Now if i want to sort on the value of the first item of the Name array how would i go around that?

Ive tried this so far:

  $dataArray = $originalData['data'];
    $dataArray = Set::sort($dataArray, '{n}.Country.Name', $direction);

However this did not work

Note im using CakePHP set::Sort is a part of Cakephp

Okay seems i wasnt clear enough so here is a more detailed explaination of my problem:

So ive created my own datasource in cake that collects data from an API.

The API returns data in a way that looks like this:

data = array(
'Offer' => array('id' => 2, 'name' = 'ExampleName'),
'Stat' => array('Stat1' = 1, 'Stat2' = 2),
'Country => array('Name' => array('name1','name2')
);

Now in order to make the user able to sort these data i have to make sure that i display correctly.

This works fine when there is only one value i.e:

                $dataArray = $originalData['data'];
            $dataArray = Set::sort($dataArray, '{n}.Stat.Stat1', 'ASC);
            $originalData['data'] = $dataArray;

However country is an array so in order to "copy" the code above i need to use the first item of the Country['Name'] array.

to do something like this:

 $dataArray = $originalData['data'];
            $dataArray = Set::sort($dataArray, '{n}.Country.Name', $direction);
            $originalData['data'] = $dataArray;

However the above code is failing ...

6
  • didn't get your question, do you want something like this? stackoverflow.com/questions/1597736/… Commented Sep 6, 2013 at 11:07
  • @AtaurRahimChowdhury Cake has a buildin function to sort i wish to use that Commented Sep 6, 2013 at 11:09
  • ahh, what error it is giving? are you getting wrongly sorted data? because it works for me! Commented Sep 6, 2013 at 11:58
  • 1
    look at this code: gist.github.com/anonymous/6462971, may be you are doing something differently? Commented Sep 6, 2013 at 12:13
  • @AtaurRahimChowdhury i actually fixed it your response gave me ideas but not completly please post it as an answer and il accept it ;) Commented Sep 6, 2013 at 13:18

1 Answer 1

2

created test data:

     $dataArray = array();
     $data = array(
        'Offer' => array('id' => 2, 'name' => 'ExampleName'),
        'Stat' => array('Stat1' => 1, 'Stat2' => 2),
        'Country' => array('Name' => array('z','name2'))
     );
      array_push($dataArray,$data);

      $data = array(
        'Offer' => array('id' => 2, 'name' => 'ExampleName'),
        'Stat' => array('Stat1' => 1, 'Stat2' => 3),
        'Country' => array('Name' => array('a','name2'))
     );
      array_push($dataArray,$data);

     print_r(Set::sort($dataArray,'{n}.Country.Name','asc')); 

Link with output array:https://gist.github.com/anonymous/6462971

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

Comments

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.