php基于所有对象字段排序对象数组

时间:2017-09-05 12:34:28

标签: php arrays

我得到了以下数组:

Array
(
[0] => stdClass Object
    (
        [ID] => 1
        [name] => NULL
        //...10 more fields here
        [count] => NULL
    )

[1] => stdClass Object
    (
        [ID] => 2
        [name] => NULL
        //...10 more fields here
        [count] => NULL
    )

[2] => stdClass Object
    (
        [ID] => 3
        [name] => NULL
        //...10 more fields here
        [count] => 4354
    )
[3] => stdClass Object
    (
        [ID] => 4
        [name] => Alice
        //...10 more fields here
        [count] => NULL
    )

....

因为你可以看到我得到了1个和2个元素namecount(以及此处未显示的所有其他字段)NULL以及它们所在的其他元素不是NULL。我需要将除NULL(在本例中为1和2)之外的所有字段为ID的数组的所有元素放到数组的末尾。我怎么能这样做?

我尝试使用usort,但它只根据对象的一个​​属性进行排序。如果我需要一次计算几个对象怎么办?

谢谢。

2 个答案:

答案 0 :(得分:1)

您正在寻找使用功能:https://secure.php.net/manual/en/function.usort.php

以下是代码:

var sample = ["1Y",  // true
              "1y",  // true
              "1YY", // false
              "1mm", // false
              "1LY", // true
              "Y1"]  // false
              
console.log(
  sample.map(s => /^[0-9]+(Y|M|W|D|LY|LM)$/i.test(s))
)

答案 1 :(得分:0)

通常情况下,fileConn1<-file("D:\\Vikas\\x1.csv", open = "a") fileConn2<-file("D:\\Vikas\\x2.csv", open = "a") fileConn3<-file("D:\\Vikas\\x3.csv", open = "a") fileConn4<-file("D:\\Vikas\\x4.csv", open = "a") df <- read.csv("abc.csv", skip = 200000,nrows = 100000) counter <- 3 while(nrow(df)!=0){ colnames(df) <- c("X", "X_raw") m <- 100000 * counter temp1 <- df temp1$X_raw <- gsub("\"","",temp1$X_raw) temp1$X_raw <- as.character(temp1$X_raw) for(i in 1:nrow(temp1)){ test_data <- strsplit(temp1$X_raw[i], ",") if(length(test_data[[1]]) == 6){ writeLines(temp1$X_raw[i], fileConn1) }else if(length(test_data[[1]]) == 12){ writeLines(temp1$X_raw[i], fileConn2) }else if(length(test_data[[1]]) == 18){ writeLines(temp1$X_raw[i], fileConn3) }else{ writeLines(temp1$X_raw[i], fileConn4) } } df <- read.csv("abc.csv", skip = m,nrows = 100000) counter <- counter + 1 } close(fileConn1) close(fileConn2) close(fileConn3) close(fileConn4) 是最小值,但如果您愿意,可以改变世界

NULL
相关问题