Reputation: 5062
I have an array which contains list of pagrank values. Consider below array:
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
)
)
I want to shift/move page rank with 'R' like:
[2] => stdClass Object
(
[pagerank] => R
)
to the end of array and it should be on last index of array?
Edit: The array key is unknown.
Upvotes: 9
Views: 21071
Reputation: 26
Just use array_filter.
Taking $arr as input array:
$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);
The result will be:
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)
Upvotes: 0
Reputation: 7556
If the index is unknown:
foreach($array as $key => $val) {
if($val->pagerank == 'R') {
$item = $array[$key];
unset($array[$key]);
array_push($array, $item);
break;
}
}
If you don't want to modify the array while it is being iterated over just find the index then make the modifications.
$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);
}
Upvotes: 14
Reputation: 116
If you have more than one object with a 'R' value:
$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);
Upvotes: 0
Reputation: 7795
foreach ($arr as $key => $value){
if ($value->pagerank == 'R'){
$arr[] = $value;
unset($arr[$key]);
break;
}
}
$arr = array_values($arr);
Upvotes: 0
Reputation: 5038
$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);
Upvotes: 1
Reputation: 593
If you want to place R as the last value and keeping your keys, you could do this:
$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);
This will give you:
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"
}
}
See: http://codepad.org/gPhrktuJ
Upvotes: 0
Reputation: 18666
If what you're looking for is specifically the value of r, you can use array_search
array_search
returns the key if an item exists in the array, otherwise returns 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
}
Upvotes: 0
Reputation: 32740
Try this :
$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);
Upvotes: 0
Reputation: 1452
Something like this?
$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"
}
*/
Ref: http://forums.phpfreaks.com/topic/177878-move-array-index-to-end/
Upvotes: 1
Reputation: 37506
$item = $array[2];
unset($array[2]);
array_push($array, $item);
Upvotes: 4