0

I've trawled a lot of questions and php manual, but I can't find a way to get sort this data in a graceful way. There may not be, but I'll settle for non-graceful.

At the moment I have a page that builds 4 arrays with data from post. The number of keys changes depending on the input;

// Grab the Tasks
$arraytask = $_POST["task"];

// Grab the Relusers
$arrayreluser = $_POST["reluser"];

// Grab the Usernames
$arrayuser = $_POST["user"];

// Grab the License Types
$arraylicense = $_POST["license"];

$result = array();
foreach( $arraytask as $key => $val) {
  $result[] = array('key'=>$key, 'value'=>$val);
}
foreach( $arrayreluser as $key => $val) {
  $result[] = array('key'=>$key, 'value'=>$val);
}
foreach( $arrayuser as $key => $val) {
  $result[] = array('key'=>$key, 'value'=>$val);
}
foreach( $arraylicense as $key => $val) {
  $result[] = array('key'=>$key, 'value'=>$val);
}
ksort($result); // I know this does nothing, I was hoping it would recursively sort or something

At the moment, the output on an example submission looks like (and sorry for the long formatting):

print_r($result);
Array (
         [0] => Array (
         [key] => 0
         [value] => 123 ) 
         [1] => Array ( 
         [key] => 1 
         [value] => 456 ) 
         [2] => Array ( 
         [key] => 2 
         [value] => 789 ) 
         [3] => Array ( 
         [key] => 0 
         [value] => qwe ) 
         [4] => Array ( 
         [key] => 1 
         [value] => rty ) 
         [5] => Array ( 
         [key] => 2 
         [value] => uio ) 
         [6] => Array ( 
         [key] => 0 
         [value] => asd ) 
         [7] => Array ( 
         [key] => 1 
         [value] => fgh ) 
         [8] => Array ( 
         [key] => 2 
         [value] => jkl ) 
         [9] => Array ( 
         [key] => 0 
         [value] => license 1 ) 
         [10] => Array ( 
         [key] => 1 
         [value] => license 2 ) 
         [11] => Array ( 
         [key] => 2 
         [value] => license 3 )
 )

However I want the output to be like

print_r($result);
 Array (
         [0] => Array (
         [key] => 0
         [value] => 123 ) 
         [3] => Array ( 
         [key] => 0 
         [value] => qwe ) 
         [6] => Array ( 
         [key] => 0 
         [value] => asd ) 
         [9] => Array ( 
         [key] => 0 
         [value] => license 1 )          
         [1] => Array ( 
         [key] => 1 
         [value] => 456 ) 
         [4] => Array ( 
         [key] => 1 
         [value] => rty ) 
         [7] => Array ( 
         [key] => 1 
         [value] => fgh ) 
         [10] => Array ( 
         [key] => 1 
         [value] => license 2 )          
         [2] => Array ( 
         [key] => 2 
         [value] => 789 ) 
         [5] => Array ( 
         [key] => 2 
         [value] => uio ) 
         [8] => Array ( 
         [key] => 2 
         [value] => jkl ) 
         [11] => Array ( 
         [key] => 2 
         [value] => license 3 )
 )

I know I'm sorting Arrays by their keys... I just can't think of a better way to sort this data.

At the moment I've looked at array_merge() which seems to overwrite duplicate keys, and I've tried a few variations of foreach loops which have just ended in tears for everyone involved.

An alternative way to ask this question would be "If I can't sort these arrays by the keys within them, can I merge my 4 arrays so that the values of each array compile in to a single array, based off key?"

An acceptable (seemingly more graceful) output would also be

 Array (
         [0] => Array (
         [key] => 0
         [value] => 123, qwe, asd, license 1 ) 
         [1] => Array ( 
         [key] => 1 
         [value] => 456, rty, fgh, license 2 ) 
         [2] => Array ( 
         [key] => 2 
         [value] => 789, uio, jkl, license 3 ) 
         )

I'm just not sure I can append values to keys in an array, if I do not explicitly know how many keys there are. Postscript: if there are typos here, that's because this is the example cut down from the actual code for clarity, and I'm sorry. My issue isn't typos.

::SOLUTION::

Thanks to vstm, this worked for combining multiple arrays into a more useful array data;

$result = array();
foreach($arraytask as $key => $val) {
   $result[] = array(
        'key' => $key,
        'task' => $arraytask[$key],
        'reluser' => $arrayreluser[$key],
        'user' => $arrayuser[$key],
        'license' => $arraylicense[$key],
        'value' => implode(', ', array(
            $arraytask[$key],
            $arrayreluser[$key],
            $arrayuser[$key],
            $arraylicense[$key],
        ))
    ); 
}

Shows the output as

 Array ( 
 [0] => Array ( 
 [key] => 0 
 [task] => 123 
 [reluser] => qwe 
 [user] => asd 
 [license] => license 1 
 [value] => 123, qwe, asd, license 1 ) 
 [1] => Array ( 
 [key] => 1 
 [task] => 456 
 [reluser] => rty 
 [user] => fgh 
 [license] => license 2 
 [value] => 456, rty, fgh, license 2 ) )

2 Answers2

1

Well it seems that your input data is already given in a way that would make sorting useless. Try it this way:

// Grab the Tasks
$arraytask = $_POST["task"];

// Grab the Relusers
$arrayreluser = $_POST["reluser"];

// Grab the Usernames
$arrayuser = $_POST["user"];

// Grab the License Types
$arraylicense = $_POST["license"];

$result = array();
foreach($arraytask as $key => $val) {
   $result[] = array(
        'key' => $key,
        'task' => $arraytask[$key],
        'reluser' => $arrayreluser[$key],
        'user' => $arrayuser[$key],
        'license' => $arraylicense[$key],
        'value' => implode(', ', array(
            $arraytask[$key],
            $arrayreluser[$key],
            $arrayuser[$key],
            $arraylicense[$key],
        ))
    ); 
}

Now you have your "seemingly graceful" output, plus access to all the fields which you might need for working with your data. No need for sorting.

vstm
  • 11,709
  • 1
  • 44
  • 45
  • I gave up on foreach after trying a few different loops and getting nowhere; I actually had some like this but I couldn't get it working and assumed that I was chasing the wrong result. Thank you for showing my this is possible vstm! – frostyboskitzo Sep 15 '14 at 01:52
0

Try by usort(). Example here...

function sortByValue($a, $b) {
    return $a['key'] - $b['key'];
}
usort($arr, 'sortByValue');

print '<pre>';
print_r($arr);
alamincse07
  • 11,674
  • 7
  • 29
  • 48