使用空格以相同的顺序显示数组

时间:2015-07-14 07:21:54

标签: php

我有问题显示两个具有混凝土结构的阵列,我不知道是否可能.. 我有2个数组多维

$ A

Array ( 
[0] => Array 
( [id] => 1
  [total] => 4 
  [label] => one 
  [url] => 0 )

[1] => Array 
( [id] => 2
  [total] => 2 
  [label] => two 
  [url] => 500 ) 

[2] => Array 
( [id] => 3
  [total] => 9 
  [label] => three 
  [url] => -100 ) 

[3] => Array 
( [id] => 5
  [total] => 15 
  [label] => five 
  [ten] => 100 ) 
) 

$ B

Array ( 
[0] => Array 
( [id] => 1
  [total] => 2 
  [label] => one 
  [url] => 100 )

[1] => Array 
( [id] => 4
  [total] => 4 
  [label] => four 
  [url] => -100 ) 

[2] => Array 
( [id] => 3
  [total] => 1 
  [label] => three 
  [url] => 200 ) 

[3] => Array 
( [id] => 5
  [total] => 1 
  [label] => five 
  [ten] => -100 ) 
) 

[4] => Array 
( [id] => 7
  [total] => 1 
  [label] => seven 
  [ten] => 500 ) 
) 

[5] => Array 
( [id] => 6
  [total] => 1 
  [label] => six 
  [ten] => 200 ) 
) 

我希望这个结构按标签列出数组

$a      $b
---     ---
one     one
two     -
three   three
-       four
five    five
-       six
-       seven

可能吗?

提前致谢

1 个答案:

答案 0 :(得分:1)

首先,您需要使用array_multisort这样的<{1}}对数组进行排序

id

结果:

    // Define an test array
    $array = array(
        0 => array(
            "id" => 1,
            "total" => 4,
            "label" => "one",
            "url" => 0
        ),
        1 => array(
            "id" => 7,
            "total" => 10,
            "label" => "seven",
            "url" => 0
        ),
        2 => array(
            "id" => 2,
            "total" => 13,
            "label" => "two",
            "url" => 0
        )
    );

    // Get a "id" list from the array
    $ids = array();
    foreach ($array as $key => $row) {
        $ids[$key]  = $row['id'];
    }

对于显示,我们只需要一个循环并检查是否存在array (size=3) 0 => array (size=4) 'id' => int 1 'total' => int 4 'label' => string 'one' (length=3) 'url' => int 0 1 => array (size=4) 'id' => int 2 'total' => int 13 'label' => string 'two' (length=3) 'url' => int 0 2 => array (size=4) 'id' => int 7 'total' => int 10 'label' => string 'seven' (length=5) 'url' => int 0 的节点。

$findIdNode["id"] == $i

结果:

   // Display the result
    for($i=1; $i<=end($array)["id"]; $i++) {
        // Check if the "id" value exist in $array
        $findIdNode = $array[array_search($i, array_column($array, "id"))];
        if($findIdNode["id"] == $i)
            echo $findIdNode["label"]."<br />";
        else
            echo "-<br />";
    }

希望它会有所帮助。