将元素移动到数组的末尾

时间:2013-07-19 13:02:40

标签: php arrays sorting

我有一个包含pagrank值列表的数组。请考虑以下数组:

Array
(
    [0] => stdClass Object
        (
            [pagerank] => 3
        )

    [1] => stdClass Object
        (
            [pagerank] => 1
        )

    [2] => stdClass Object
        (
            [pagerank] => R
        )

    [3] => stdClass Object
        (
            [pagerank] => 2
        )

    [4] => stdClass Object
        (
            [pagerank] => 7
        )

)

我想用'R'来移动/移动页面排名,如:

[2] => stdClass Object
        (
            [pagerank] => R
        )

到数组的末尾,它应该在数组的最后一个索引上?

编辑:数组键未知。

10 个答案:

答案 0 :(得分:13)

如果索引未知:

foreach($array as $key => $val) {
    if($val->pagerank == 'R') {
        $item = $array[$key];
        unset($array[$key]);
        array_push($array, $item); 
        break;
    }
}

答案 1 :(得分:3)

$item = $array[2];
unset($array[2]);
array_push($array, $item); 

答案 2 :(得分:1)

这样的东西?

$var = array(
    'name' => 'thename',
    'title' => 'thetitle',
    'media' => 'themedia'
);

// Remove first element (the name)
$name = array_shift($var);
// Add it on to the end
$var['name'] = $name;

var_dump($var);

/*
array(3) {
  ["title"]=>
  string(8) "thetitle"
  ["media"]=>
  string(8) "themedia"
  ["name"]=>
  string(7) "thename"
}
*/

参考:http://forums.phpfreaks.com/topic/177878-move-array-index-to-end/

答案 3 :(得分:1)

    $item=null;
    foreach ($array['pagerank'] as $key => $value) 
    {
     if( $value=="R")
     {
     $item = $array[$key];
     unset($array[$key]);
     break;
     }
    }
     if($item !=null)
     array_push($array, $item);

答案 4 :(得分:0)

如果您有多个具有“R”值的对象:

$current_array = $sorted_array = Array(
    ...
);
foreach($current_array as $current_key => $element){
    if($element->pagerank == 'R'){
        unset($sorted_array[$current_key]);
        $sorted_array[] = $element;
    }
}
unset($current_array);

答案 5 :(得分:0)

如果您想将R作为最后一个值并保留您的密钥,您可以这样做:

$arr = array(
    (object)array('pagerank' => 1),
    (object)array('pagerank' => 'R'),
    (object)array('pagerank' => 2),
);

// Store in temp var.
$tmp_arr = $arr;

// Sort temp array to put 'R' in top.
asort($tmp_arr);

// Reset to be able to find the first key in the sorted array.
reset($tmp_arr);

// Get key from first value in array.
$key = key($tmp_arr);

// Store value from first key.
$item = $tmp_arr[$key];

// Unset key from original array.
unset($arr[$key]);

// Insert as last value in original array using original key.
$arr[$key] = $item;

// Print result.
var_dump($arr);

这会给你:

array(3) {
  [0]=>
  object(stdClass)#1 (1) {
    ["pagerank"]=>
    int(1)
  }
  [2]=>
  object(stdClass)#3 (1) {
    ["pagerank"]=>
    int(2)
  }
  [1]=>
  object(stdClass)#2 (1) {
    ["pagerank"]=>
    string(1) "R"
  }
}

请参阅:http://codepad.org/gPhrktuJ

答案 6 :(得分:0)

试试这个:

$arr  = array(array("pagerank" => 3),
              array("pagerank" => 1),
              array("pagerank" => 'R'),
              array("pagerank" => 4),
              array("pagerank" => 7),
              array("pagerank" => 5),
              array("pagerank" => 2)
        );

foreach($arr as $key=>$ar){
   if($ar["pagerank"] == "R"){
     $unset  = $key;
     break;
   }
}

$val    = $arr[$unset];
unset($arr[$unset]);
$arr[]  = $val; 

print_r($arr);

答案 7 :(得分:0)

如果您要查找的内容特别是r的值,则可以使用array_search

如果数组中存在某个项,

array_search将返回该键,否则返回false。

$needle = "R";
if($key = array_search($needle, $pageRankArray)) {
   unset($pageRankArray[$key]); // Delete an item from the array
   array_push($pageRankArray, $needle); // inserts element at the end of the array
}

答案 8 :(得分:0)

foreach ($arr as $key => $value){
    if ($value->pagerank == 'R'){
        $arr[] = $value;
        unset($arr[$key]);
        break;
    }
}
$arr = array_values($arr);

答案 9 :(得分:0)

只需使用array_filter。

以$ arr作为输入数组:

/usr/local/lib/python3.6/dist-packages/notebook/static/custom/

结果将是:

$arr=Array
(
    (Object) ['pagerank' => 3],
    (Object) ['pagerank' => 1],
    (Object) ['pagerank' => "R"],
    (Object) ['pagerank' => 2],
    (Object) ['pagerank' => 7],
);
$result=array_filter($arr,function($item){return $item->pagerank!="R";})+$arr;
var_dump($arr,$result);