让我说我有这个数组
Array
(
[status] => success
[total] => 17
[count] => 0
[start] => 0
[result] => Array
(
[Business Services] => Array
(
[0] => Array
(
[maincatid] => 3
[subcatid] => 171
[subcat] => 12345
[maincat] => Business Services
)
[1] => Array
(
[maincatid] => 3
[subcatid] => 22
[subcat] => Business Opportunities
[maincat] => Business Services
)
[2] => Array
(
[maincatid] => 3
[subcatid] => 23
[subcat] => Courier Services
[maincat] => Business Services
)
[3] => Array
(
[maincatid] => 3
[subcatid] => 24
[subcat] => Databases
[maincat] => Business Services
)
[4] => Array
(
[maincatid] => 3
[subcatid] => 25
[subcat] => Office Furniture & Equipment
[maincat] => Business Services
)
[5] => Array
(
[maincatid] => 3
[subcatid] => 26
[subcat] => Other Business Services
[maincat] => Business Services
)
[6] => Array
(
[maincatid] => 3
[subcatid] => 27
[subcat] => Printing & Copying
[maincat] => Business Services
)
[7] => Array
(
[maincatid] => 3
[subcatid] => 28
[subcat] => Publications
[maincat] => Business Services
)
[8] => Array
(
[maincatid] => 3
[subcatid] => 29
[subcat] => Security Products & Services
[maincat] => Business Services
)
[9] => Array
(
[maincatid] => 3
[subcatid] => 30
[subcat] => Translation Services
[maincat] => Business Services
)
)
[Courses] => Array
(
[0] => Array
(
[maincatid] => 6
[subcatid] => 45
[subcat] => Academic Courses
[maincat] => Courses
)
[1] => Array
(
[maincatid] => 6
[subcatid] => 46
[subcat] => Computer Courses
[maincat] => Courses
)
[2] => Array
(
[maincatid] => 6
[subcatid] => 47
[subcat] => Cooking & Baking Lessons
[maincat] => Courses
)
[3] => Array
(
[maincatid] => 6
[subcatid] => 48
[subcat] => Dance Lessons
[maincat] => Courses
)
[4] => Array
(
[maincatid] => 6
[subcatid] => 49
[subcat] => Fitness & Gym Lessons
[maincat] => Courses
)
[5] => Array
(
[maincatid] => 6
[subcatid] => 50
[subcat] => Flower Arrangement Lessons
[maincat] => Courses
)
[6] => Array
(
[maincatid] => 6
[subcatid] => 51
[subcat] => Hair Dressing & Beauty Courses
[maincat] => Courses
)
[7] => Array
(
[maincatid] => 6
[subcatid] => 52
[subcat] => Language Courses
[maincat] => Courses
)
[8] => Array
(
[maincatid] => 6
[subcatid] => 53
[subcat] => Music Lessons
[maincat] => Courses
)
[9] => Array
(
[maincatid] => 6
[subcatid] => 60
[subcat] => Others
[maincat] => Courses
)
[10] => Array
(
[maincatid] => 6
[subcatid] => 54
[subcat] => Photography & Video Lessons
[maincat] => Courses
)
[11] => Array
(
[maincatid] => 6
[subcatid] => 55
[subcat] => Self Improvement Courses
[maincat] => Courses
)
[12] => Array
(
[maincatid] => 6
[subcatid] => 56
[subcat] => Swimming Lessons
[maincat] => Courses
)
[13] => Array
(
[maincatid] => 6
[subcatid] => 57
[subcat] => Tuition
[maincat] => Courses
)
[14] => Array
(
[maincatid] => 6
[subcatid] => 58
[subcat] => Yoga & Pilates Lessons
[maincat] => Courses
)
[15] => Array
(
[maincatid] => 6
[subcatid] => 59
[subcat] => Youth Programs & Courses
[maincat] => Courses
)
)
[Hobbies & Interests] => Array
(
[0] => Array
(
[maincatid] => 11
[subcatid] => 88
[subcat] => Action Figures & Collectibles
[maincat] => Hobbies & Interests
)
[1] => Array
(
[maincatid] => 11
[subcatid] => 89
[subcat] => Antiques & Collectibles
[maincat] => Hobbies & Interests
)
[2] => Array
(
[maincatid] => 11
[subcatid] => 91
[subcat] => Books & Magazines
[maincat] => Hobbies & Interests
)
[3] => Array
(
[maincatid] => 11
[subcatid] => 92
[subcat] => Building Toys
[maincat] => Hobbies & Interests
)
[4] => Array
(
[maincatid] => 11
[subcatid] => 94
[subcat] => Club Memberships
[maincat] => Hobbies & Interests
)
[5] => Array
(
[maincatid] => 11
[subcatid] => 95
[subcat] => Comics & Manga
[maincat] => Hobbies & Interests
)
[6] => Array
(
[maincatid] => 11
[subcatid] => 96
[subcat] => Computer & Console Games
[maincat] => Hobbies & Interests
)
[7] => Array
(
[maincatid] => 11
[subcatid] => 97
[subcat] => Florist
[maincat] => Hobbies & Interests
)
[8] => Array
(
[maincatid] => 11
[subcatid] => 98
[subcat] => Gift Ideas
[maincat] => Hobbies & Interests
)
[9] => Array
(
[maincatid] => 11
[subcatid] => 99
[subcat] => Musical Instruments
[maincat] => Hobbies & Interests
)
[10] => Array
(
[maincatid] => 11
[subcatid] => 104
[subcat] => Others
[maincat] => Hobbies & Interests
)
[11] => Array
(
[maincatid] => 11
[subcatid] => 101
[subcat] => RC (Remote Control)
[maincat] => Hobbies & Interests
)
)
我希望将具有“Others”子标题的“object”数组放在它自己的数组的末尾......怎么做?
答案 0 :(得分:2)
如果要重新索引数组,可以执行类似这样的操作
$tempElement = $bigarray[0][2];
unset($bigarray[0][2]);
$bigarray[0] = array_values($bigarray[0]); // reindex array
$bigarray[0][] = $tempElement;
或使用自定义索引范围
$tempElement = $bigarray[0][2];
unset($bigarray[0][2]);
$bigarray[0] = array_combine(range(0, count($bigarray[0]) - 1), array_values($bigarray[0]));
$bigarray[0][] = $tempElement;
答案 1 :(得分:2)
这是你要找的吗?
<?php
$arr = array(
"status" => "success",
"total" => 17,
"count" => 0,
"start" => 0,
"result" => array(
"Business Services" => array(
0 => array(
"maincatid" => 3,
"subcatid" => 171,
"subcat" => 12345,
"maincat" => "Business Services"
),
1 => array(
"maincatid" => 3,
"subcatid" => 22,
"subcat" => "Business Opportunities",
"maincat" => "Business Services"
),
2 => array(
"maincatid" => 3,
"subcatid" => 23,
"subcat" => "Courier Services",
"maincat" => "Business Services"
),
3 => array(
"maincatid" => 3,
"subcatid" => 24,
"subcat" => "Databases",
"maincat" => "Business Services"
),
4 => array(
"maincatid" => 3,
"subcatid" => 25,
"subcat" => "Office Furniture & Equipment",
"maincat" => "Business Services"
),
5 => array(
"maincatid" => 3,
"subcatid" => 26,
"subcat" => "Other Business Services",
"maincat" => "Business Services"
),
6 => array(
"maincatid" => 3,
"subcatid" => 27,
"subcat" => "Printing & Copying",
"maincat" => "Business Services"
),
7 => array(
"maincatid" => 3,
"subcatid" => 28,
"subcat" => "Publications",
"maincat" => "Business Services"
),
8 => array(
"maincatid" => 3,
"subcatid" => 29,
"subcat" => "Security Products & Services",
"maincat" => "Business Services"
),
9 => array(
"maincatid" => 3,
"subcatid" => 30,
"subcat" => "Translation Services",
"maincat" => "Business Services"
)
),
"Courses" => array(
0 => array(
"maincatid" => 6,
"subcatid" => 45,
"subcat" => "Academic Courses",
"maincat" => "Courses"
),
1 => array(
"maincatid" => 6,
"subcatid" => 46,
"subcat" => "Computer Courses",
"maincat" => "Courses"
),
2 => array(
"maincatid" => 6,
"subcatid" => 47,
"subcat" => "Cooking & Baking Lessons",
"maincat" => "Courses"
),
3 => array(
"maincatid" => 6,
"subcatid" => 48,
"subcat" => "Dance Lessons",
"maincat" => "Courses"
),
4 => array(
"maincatid" => 6,
"subcatid" => 49,
"subcat" => "Fitness & Gym Lessons",
"maincat" => "Courses"
),
5 => array(
"maincatid" => 6,
"subcatid" => 50,
"subcat" => "Flower Arrangement Lessons",
"maincat" => "Courses"
),
6 => array(
"maincatid" => 6,
"subcatid" => 51,
"subcat" => "Hair Dressing & Beauty Courses",
"maincat" => "Courses"
),
7 => array(
"maincatid" => 6,
"subcatid" => 52,
"subcat" => "Language Courses",
"maincat" => "Courses"
),
8 => array(
"maincatid" => 6,
"subcatid" => 53,
"subcat" => "Music Lessons",
"maincat" => "Courses"
),
9 => array(
"maincatid" => 6,
"subcatid" => 60,
"subcat" => "Others",
"maincat" => "Courses"
),
10 => array(
"maincatid" => 6,
"subcatid" => 54,
"subcat" => "Photography & Video Lessons",
"maincat" => "Courses"
),
11 => array(
"maincatid" => 6,
"subcatid" => 55,
"subcat" => "Self Improvement Courses",
"maincat" => "Courses"
),
12 => array(
"maincatid" => 6,
"subcatid" => 56,
"subcat" => "Swimming Lessons",
"maincat" => "Courses"
),
13 => array(
"maincatid" => 6,
"subcatid" => 57,
"subcat" => "Tuition",
"maincat" => "Courses"
),
14 => array(
"maincatid" => 6,
"subcatid" => 58,
"subcat" => "Yoga & Pilates Lessons",
"maincat" => "Courses"
),
15 => array(
"maincatid" => 6,
"subcatid" => 59,
"subcat" => "Youth Programs & Courses",
"maincat" => "Courses"
)
),
"Hobbies & Interests" => array(
0 => array(
"maincatid" => 11,
"subcatid" => 88,
"subcat" => "Action Figures & Collectibles",
"maincat" => "Hobbies & Interests"
),
1 => array(
"maincatid" => 11,
"subcatid" => 89,
"subcat" => "Antiques & Collectibles",
"maincat" => "Hobbies & Interests"
),
2 => array(
"maincatid" => 11,
"subcatid" => 91,
"subcat" => "Books & Magazines",
"maincat" => "Hobbies & Interests"
),
3 => array(
"maincatid" => 11,
"subcatid" => 92,
"subcat" => "Building Toys",
"maincat" => "Hobbies & Interests"
),
4 => array(
"maincatid" => 11,
"subcatid" => 94,
"subcat" => "Club Memberships",
"maincat" => "Hobbies & Interests"
),
5 => array(
"maincatid" => 11,
"subcatid" => 95,
"subcat" => "Comics & Manga",
"maincat" => "Hobbies & Interests"
),
6 => array(
"maincatid" => 11,
"subcatid" => 96,
"subcat" => "Computer & Console Games",
"maincat" => "Hobbies & Interests"
),
7 => array(
"maincatid" => 11,
"subcatid" => 97,
"subcat" => "Florist",
"maincat" => "Hobbies & Interests"
),
8 => array(
"maincatid" => 11,
"subcatid" => 98,
"subcat" => "Gift Ideas",
"maincat" => "Hobbies & Interests"
),
9 => array(
"maincatid" => 11,
"subcatid" => 99,
"subcat" => "Musical Instruments",
"maincat" => "Hobbies & Interests"
),
10 => array(
"maincatid" => 11,
"subcatid" => 104,
"subcat" => "Others",
"maincat" => "Hobbies & Interests"
),
11 => array(
"maincatid" => 11,
"subcatid" => 101,
"subcat" => "RC (Remote Control)",
"maincat" => "Hobbies & Interests"
)
)
)
);
foreach($arr["result"] as &$bigarray){
if(is_array($bigarray)){
$finalarray = array();
foreach($bigarray as $smallarray){
if($smallarray["subcat"] == "Others"){
$temp = $smallarray;
}
else{
$finalarray[] = $smallarray;
}
}
if(isset($temp)){
$finalarray[] = $temp;
}
$bigarray = $finalarray;
}
}
print_r($arr);
?>
请参阅演示here。
答案 2 :(得分:2)
根据您的新编辑,试试这个,
$temp = Array();
foreach ($bigarray['result']['Business Services'] as $k=>$smallarr){
if($smallarr['subcat'] == 'Others'){
$temp = $bigarray['result']['Business Services'][$k];
unset($bigarray['result']['Business Services'][$k]);
$bigarray['result']['Business Services'][] = $temp;
}
}
print_r($bigarray);
上使用您的数组进行完整演示
答案 3 :(得分:2)
感谢你的所有答案先生......我终于弄明白自己该怎么做了。 其中一个答案给了我解决它的想法..谢谢。 这是我自己的解决方案
$temp = '';
foreach($bigarray['result'] as $k => $big){
foreach($big as $key => $smallarr){
//echo $key . ' = ' . $smallarr['subcat']."<br/>";
if($smallarr['subcat'] == 'Others'){
//$temp = $big[$key];
$temp = $bigarray['result'][$k][$key];
unset($bigarray['result'][$k][$key]);
$bigarray['result'][$k][] = $temp;
}
}
}
echo "<pre>",print_r($bigarray),"</pre>";
答案 4 :(得分:1)
我不确定你在找什么,试试看:
$array = $bigarray[2]; //store 3rd element to a variable
unset($bigarray[2]); // removing 3rd element from array
array_push($array, $bigarray); //again pushing it at the end
有关array_push()和unset()的更多信息。
答案 5 :(得分:0)
如果你愿意,我无法理解你想要什么。你可以试试这个。
$bigarray = array(
0 => Array
(
'maincatid' => 3,
'subcatid' => 171,
'subcat' => 'asdfasdf',
'maincat' => 'Business Services'
),
1 => Array
(
'maincatid' => 3,
'subcatid' => 22,
'subcat' => 'Business Opportunities',
'maincat' => 'Business Services'
),
2 => Array
(
'maincatid' => 3,
'subcatid' => 23,
'subcat' => 'Others',
'maincat' => 'Business Services'
),
3 => Array
(
'maincatid' => 3,
'subcatid' => 24,
'subcat' => 'Databases',
'maincat' => 'Business Services'
)
);
$resultArray = array();
$lastArray = array();
for ($i=0;$i<count($bigarray);$i++){
if ($bigarray[$i]['subcat']=='Others'){
$lastArray[] = $bigarray[$i];
}else{
$resultArray[] = $bigarray[$i];
}
}
print_r (array_merge( $resultArray , $lastArray));