9

我有一个包含 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
        )

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

编辑:数组键未知。

4

10 回答 10

14

如果索引未知:

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

如果您不想在迭代时修改数组,只需找到索引然后进行修改。

$foundIndex = false;
foreach($array as $key => $val) {
    if($val->pagerank == 'R') {
        $foundIndex = $key;
        break;
    }
}
if($foundIndex !== false) {
    $item = $array[$foundIndex];
    unset($array[$foundIndex]);
    array_push($array, $item);
}
于 2013-07-19T13:19:38.853 回答
4
$item = $array[2];
unset($array[2]);
array_push($array, $item); 
于 2013-07-19T13:05:51.083 回答
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/

于 2013-07-19T13:15:36.733 回答
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);
于 2013-07-19T13:21:39.770 回答
0
foreach ($arr as $key => $value){
    if ($value->pagerank == 'R'){
        $arr[] = $value;
        unset($arr[$key]);
        break;
    }
}
$arr = array_values($arr);
于 2013-07-19T13:47:42.040 回答
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);
于 2013-07-19T13:24:22.453 回答
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
}
于 2013-07-19T13:25:00.150 回答
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

于 2013-07-19T13:19:23.823 回答
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);
于 2013-07-19T13:18:41.780 回答
0

只需使用array_filter。

以 $arr 作为输入数组:

$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);

结果将是:

array (size=5)
  0 => 
    object(stdClass)[1]
      public 'pagerank' => int 3
  1 => 
    object(stdClass)[2]
      public 'pagerank' => int 1
  2 => 
    object(stdClass)[3]
      public 'pagerank' => string 'R' (length=1)
  3 => 
    object(stdClass)[4]
      public 'pagerank' => int 2
  4 => 
    object(stdClass)[5]
      public 'pagerank' => int 7

array (size=5)
  0 => 
    object(stdClass)[1]
      public 'pagerank' => int 3
  1 => 
    object(stdClass)[2]
      public 'pagerank' => int 1
  3 => 
    object(stdClass)[4]
      public 'pagerank' => int 2
  4 => 
    object(stdClass)[5]
      public 'pagerank' => int 7
  2 => 
    object(stdClass)[3]
      public 'pagerank' => string 'R' (length=1)
于 2020-04-25T22:14:25.833 回答