我有3个数组,现在我想合并它们以形成一个新数组,并按照关键brand_name值按升序迭代它们,即我想打印Hp数组第一个NOKIA第二个和三星最后一个< / p>
第一个数组
$first=array(
id: "1",
model_no: "1520",
brand_name: "samsung",
description: "this is samsung",
price: "1200",
);
第二个数组
$second=array(
id: "1",
model_no: "1100",
brand_name: "Hp",
description: "this is hp",
price: "1500",
);
第三阵列
$third=array(
id: "1",
model_no: "1200",
brand_name: "NOKIA",
description: "this is nokia",
price: "1150",
);
答案 0 :(得分:0)
<?php
$bigarray = array(
array(
'id' => 1,
'model_no' => 1520,
'brand_name' => "samsung",
'description' => "this is samsung",
'price' => 1200
),
array(
'id' => 1,
'model_no' => 1100,
'brand_name' => "HP",
'description' => "this is hp",
'price' => 1500
),
array(
'id' => 1,
'model_no' => 1200,
'brand_name' => "NOKIA",
'description' => "this is nokia",
'price' => 1150
)
);
function sortByOrder($a, $b) {
return $a['brand_name'] > $b['brand_name'];
}
usort($bigarray, 'sortByOrder');
print_r($bigarray);
?>
输出:
Array
(
[0] => Array
(
[id] => 1
[model_no] => 1100
[brand_name] => HP
[description] => this is hp
[price] => 1500
)
[1] => Array
(
[id] => 1
[model_no] => 1200
[brand_name] => NOKIA
[description] => this is nokia
[price] => 1150
)
[2] => Array
(
[id] => 1
[model_no] => 1520
[brand_name] => samsung
[description] => this is samsung
[price] => 1200
)
)
现在您的数组将按照您的需要进行排序。
玩得开心。
答案 1 :(得分:0)
你可以试试我的代码...我希望能帮助我的代码...
<?php $finalarray=array();
$first=array('id'=> "1",'model_no'=> "1520",'brand_name'=> "samsung",'description'=> "this is samsung",'price'=> "1200");
$second=array('id'=> "1",'model_no'=> "1100",'brand_name'=> "Hp",'description'=> "this is hp",'price'=> "1500");
$third=array('id'=> "1",'model_no'=> "1200",'brand_name'=> "NOKIA",'description'=> "this is nokia",'price'=> "1150",);
$finalarray =array_merge(array($first),array($second),array($third));
usort($finalarray, function($a, $b) { return $a['brand_name']=="Hp"?-1:1; });
echo "<pre>"; print_r($finalarray);
输出
Array( [0] => Array (
[id] => 1
[model_no] => 1100
[brand_name] => Hp
[description] => this is hp
[price] => 1500
)
[1] => Array
(
[id] => 1
[model_no] => 1520
[brand_name] => samsung
[description] => this is samsung
[price] => 1200
)
[2] => Array
(
[id] => 1
[model_no] => 1200
[brand_name] => NOKIA
[description] => this is nokia
[price] => 1150
))
答案 2 :(得分:0)
尝试使用此代码
<?php
$arraycomplete = array(
array(
'id' => 1,
'model_no' => 1520,
'brand_name' => "samsung",
'description' => "this is samsung",
'price' => 1200
),
array(
'id' => 2,
'model_no' => 1100,
'brand_name' => "HP",
'description' => "this is hp",
'price' => 1500
),
array(
'id' => 3,
'model_no' => 1200,
'brand_name' => "NOKIA",
'description' => "this is nokia",
'price' => 1150
)
);
for($i = 0 ; $i < sizeof($arraycomplete) ; $i++){
echo "ARRAY " . ($i + 1) . (".") . "<br>";
echo "id: " . $arraycomplete[$i]["id"] . "<br>";
echo "model_no: " . $arraycomplete[$i]["model_no"] . "<br>";
echo "brand_name: " . $arraycomplete[$i]["brand_name"] . "<br>";
echo "description: " . $arraycomplete[$i]["description"] . "<br>";
echo "price: " . $arraycomplete[$i]["price"] . "<br>";
echo "<br>";
}
?>