2016-12-03 67 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>"; 
} 
?> 
嘗試