开发者

Arrange the array in required sequence

开发者 https://www.devze.com 2023-02-24 12:26 出处:网络
I Have an array Ar开发者_开发问答ray ( [0] => Array ( [COMPANY NAME] => 1 [LPO NUMBER] => PO1

I Have an array

Ar开发者_开发问答ray
(
    [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
        )

)

I need to arrange array in my required sequence of indexes which is like

[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

        )

How It can be done? Pls Help...


Try this (untested):

$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;
}

This will create a new array $reordered which holds all your rows, with the keys reordered according to the order defined in $keys.

Alternatively you could reorder your array in-place using uksort and a callback function (again, untested).

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');
}

A third option is to leave your array alone but to edit your view code:

$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]));
    }
}


You could try to be clever with actually sorting the array, but the easiest way is to simply recreate it:

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

It seems like a very non-sensical thing to do though. Either your array is numerically indexed and ordered, or you're using an associative array. Requiring both is plain weird.


My need is To Iterate the array such that every "<td>" gets its value required specific value, like wise here first "<td>" must have "Reference Number" as first Value

In that case you should just create your table explictly so:

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


You could create functions that make use of array_splice() to reorder array elements. It's most likely better to just rebuild the array how you want it, but just for variety of answers, here it is:

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

?>

Then simply call these functions on your array until the order is how you like it.


Read your requirement from the comments, do not order your associative array. Instead do a cleaner thing like :

  1. Have a integer index to associative array key mapping. eg.

    $myIndexes = Array("Reference Number","Vendor Name","BENEFICIARY NAME" ....);

  2. 2.

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

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

While printing your <td> sequentially, fetch a keyName from $myIndexes, and then fetch the value from your data array.


because your array is quite long, I show it with a simpler example. This is $your_array:

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

put it together with the following code:

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

I hope with that example in hand you can fill out the rest by yourself.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号