2016-12-03 85 views
0

我有3个数组,现在我想合并它们以形成一个新数组,并基于关键的brand_name值在升序中迭代它们,即我想打印HP阵列第一NOKIA第二和三星最后PHP合并3阵列并基于第二个键值升序迭代

第一阵列

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

) 

现在,你想要的你的阵列将被订购。

玩得开心。

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 
    )) 
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>"; 
} 
?> 
尝试