根据某个列对PHP数组进行排序

时间:2016-06-18 10:01:36

标签: php arrays json sorting

我有以下JSON:

    [{
      "id":"1",
      "testimony":"I recently went to Perceptive Mind on behalf of a client to get assistance in the web-development of an online invoicing system.\n\t\t\t\t\t\t\t\t\t\t\t\t\tThe work they did for me was done quickly and with great care. What I found most notable about Perceptive Mind was that it worked with my budget, I didn't feel overcharged and had plenty of tech support and communication throughout the project.\n\t\t\t\t\t\t\t\t\t\t\t\t\tI will definitely be using Perceptive Mind in the future!",
      "author":"Miles Hellyer",
      "title":"Founder",
      "company":"Chalk Marketing",
      "sort_order":"200"
   },
   {
      "id":"2",
      "testimony":"Just like to thank Nick for\r\n\t\t\t\t\t\t\t\t\t\t\t\t\this outstanding and professional work, he has created a great website\r\n\t\t\t\t\t\t\t\t\t\t\t\t\twhich suits our needs. He was efficient, helpful and very\r\n\t\t\t\t\t\t\t\t\t\t\t\t\tfriendly! Definitely would recommend using his services",
      "author":"Jennifer Mouhaweg",
      "title":"Manager",
      "company":"The Lockout Guys",
      "sort_order":"400"
   }]

这是解码JSON并将其放入PHP数组的代码:

$testimonials_array;
        $testimonials_table_json = json_decode(file_get_contents('../app/database/testimonials.JSON'),true);
        foreach($testimonials_table_json as $row) {
            $testimonial = $this->model('TestimonialModel');
            $testimonial->testimony = $row['testimony'];
            $testimonial->author = $row['author'];
            $testimonial->title = $row['title'];
            $testimonial->company = $row['company'];

            $testimonials_array[] = $testimonial;
        }

        return $testimonials_array;

我的问题是,如果我添加$testimonial->sort_order= $row['sort_order'];,我可以使用它来对$testimonial数组进行排序吗?有没有办法在foreach循环之前对其进行排序?

1 个答案:

答案 0 :(得分:0)

使用usort

             tabl = "CREATE TABLE " + tableName  +
                "(" + " " + tablelower + "_currentid INT PRIMARY KEY AUTO_INCREMENT, " +
                tablelower + "_id VARCHAR(8) NOT NULL, " +
                tablelower + "_name VARCHAR(45) NOT NULL, " +
                tablelower + "_type VARCHAR(45) NOT NULL, " +
                tablelower + "_topic  VARCHAR(255) NOT NULL, " +
                tablelower + "_pin  VARCHAR(6) NOT NULL, " +
                tablelower + "_device VARCHAR(100) NOT NULL, " +
                tablelower + "_device_id INT NOT NULL, " +
                "FOREIGN KEY(" + tablelower + "_device_id) REFERENCES Devices(device_currentid)" +
                ")";
             trigg=
                " CREATE TRIGGER " + tablelower + "_trigger BEFORE INSERT ON " + tableName + 
                " FOR EACH ROW" +
                "   SET new." + tablelower + "_id = CONCAT('" + topic + "',LPAD((SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '" + tableName + "'),4,'0'))" +
                "   SET new." + tablelower + "_topic = CONCAT((SELECT device_topic FROM Devices WHERE device_name LIKE new." + tablelower + "_device),'/',(new." + tablelower + "_id))" +
                " END";

        mysqlconn = data.getConnection();
        mysqlconn.createStatement().execute(tabl);
        mysqlconn.createStatement().execute(trigg);