按要求的顺序排列阵列

时间:2011-04-13 05:37:33

标签: php arrays

我有一个数组

Array
(
    [0] => Array
        (
            [COMPANY NAME] => 1
            [LPO NUMBER] => PO1
            [LPO DATE] => 2011-04-13 10:08:37
            [LPO AMT] => 1000
            [PENDING AMT] => 1000
            [PRIORITY] => 1
            [AMOUNT] => 200
            [BENEFICIARY NAME] => Self
            [PAYMENT AS] => 1
            [VENDOR NAME] => 0
            [FINAL PAYMENT] => 
            [doc_id] => 2
            [Vendor Name] => Dept. of Economic Development.
            [Reference Number] => PR_XHA_210
            [Pay Cheque Number] => N/A
            [Paid] => 0
        )

    [1] => Array
        (
            [COMPANY NAME] => 1
            [LPO NUMBER] => PO1
            [LPO DATE] => 2011-04-13 10:08:37
            [LPO AMT] => 1000
            [PENDING AMT] => 800
            [PRIORITY] => 1
            [AMOUNT] => 800
            [BENEFICIARY NAME] => Self
            [PAYMENT AS] => 1
            [VENDOR NAME] => 0
            [FINAL PAYMENT] => 
            [doc_id] => 3
            [Vendor Name] => Dept. of Economic Development.
            [Reference Number] => PR_XHA_211
            [Pay Cheque Number] => N/A
            [Paid] => 0
        )

)

我需要在所需的索引序列中安排数组,如

[0] => Array
        (
            [Reference Number] => PR_XHA_210
            [Vendor Name] => Dept. of Economic Development.
            [BENEFICIARY NAME] => Self
            [LPO DATE] => 2011-04-13 10:08:37
            [LPO NUMBER] => PO1
            [LPO AMT] => 1000
            [Paid] => 0
            [AMOUNT] => 200
            [doc_id] => 2
            [doc_id] => 2
            [Pay Cheque Number] => N/A

        )

如何做到这一点? 请帮忙......

5 个答案:

答案 0 :(得分:2)

试试这个(未经测试):

$keys = array(
    'Reference Number',
    'Vendor Name',
    ...
);

$reordered = array();
foreach ($array as $item) {
    $new_item = array();
    foreach ($keys as $key) {
        $new_item[$key] = $item[$key];
    }
    $reordered[] = $new_item;
}

这将创建一个新数组$reordered,它保存所有行,并根据$keys中定义的顺序重新排序键。

或者,您可以使用uksort和回调函数(再次,未经测试)对您的数组进行就地重新排序。

function sort_keys($a, $b) {
    static $keys = array(
        'Reference Number',
        'Vendor Name',
        ...
    );

    return array_search($a, $keys) - array_search($b, $keys);
}

foreach ($array as &$item) {
    uksort($item, 'sort_keys');
}

第三个选项是让您的阵列保持不变,但要编辑您的视图代码:

$keys = array(
    'Reference Number',
    'Vendor Name',
    ...
);

foreach ($array as $item) {
    foreach ($keys as $key) {
        printf('<tr><th>%s</th><td>%s</td></tr>',
            htmlspecialchars($key),
            htmlspecialchars($item[$key]));
    }
}

答案 1 :(得分:1)

你可以尝试巧妙地对数组进行排序,但最简单的方法是简单地重新创建它:

$rearrangedArray = array(
    'Reference Number' => $oldArray['Reference Number'],
    'Vendor Name'      => $oldArray['Vendor Name'],
    ...
);

尽管如此,这似乎是一种非常不感性的事情。要么对数组进行数字索引和排序,要么使用关联数组。要求两者都很奇怪。


  

我的需要是迭代数组,使每个“<td>”获得其值所需的特定值,如此明智地首先“<td>”必须具有“参考编号”作为第一个值

在这种情况下,你应该明确地创建你的表:

<tr>
    <td>Reference Number:</td> <td><?php echo $array['Reference Number']; ?></td>
    <td>Vendor Name:</td>      <td><?php echo $array['Vendor Name']; ?></td>
    ...
</tr>

答案 2 :(得分:1)

从评论中阅读您的要求, 不要命令你的关联数组。相反,做一个更清洁的事情:

  1. 具有关联数组键映射的整数索引。例如

    $ myIndexes = Array(“参考编号”,“供应商名称”,“受益人姓名”......);

  2. 2。

    for($ i = 0; $ i&lt; count($ dataArray); $ i ++)
       {

          $myCurrentKey = $myIndexes[$i];
          $myCurrentData = $dataArray[$myCurrentKey];
          echo "<td>".$myCurrentData."</td>";
     }
    

    按顺序打印<td>时,从$ myIndexes中获取keyName,然后从数据数组中获取值。

答案 3 :(得分:1)

您可以创建使用array_splice()重新排序数组元素的函数。最有可能只是按照你想要的方式重建阵列,但只是为了各种答案,这里是:

<?php

// $input  (Array) - the array containing the element
// $index (int) - the index of the element you need to move

function moveUp($input,$index) {
      $new_array = $input;

       if((count($new_array)>$index) && ($index>0)){
                 array_splice($new_array, $index-1, 0, $input[$index]);
                 array_splice($new_array, $index+1, 1);
             } 

       return $new_array;
}

function moveDown($input,$index) {
       $new_array = $input;

       if(count($new_array)>$index) {
                 array_splice($new_array, $index+2, 0, $input[$index]);
                 array_splice($new_array, $index, 1);
             } 

       return $new_array;
 }  

$input = array("red", "green", "blue", "yellow");

$newinput = moveUp($input, 2);
// $newinput is array("red", "blue", "green", "yellow")

$input = moveDown($newinput, 1);
// $input is array("red", "green", "blue", "yellow")

?>

然后只需在数组上调用这些函数,直到订单符合您的喜好。

答案 4 :(得分:0)

因为你的数组很长,我用一个更简单的例子来展示它。这是$ your_array:

Array
(
    [0] => Array
        (
            [COMPANY NAME] => 1
            [LPO NUMBER] => PO1
         )
    [0] => Array
        (
            [AMOUNT] => 200
            [doc_id] => 2
         )
)

将它与以下代码放在一起:

$together = array(
  $your_array[1]['AMOUNT'],
  $your_array[0]['COMPANY NAME'],
  $your_array[0]['LPO NUMBER'],
  $your_array[1]['doc_id']
);

我希望手头有这个例子,你可以自己填写其余部分。