Results 1 to 3 of 3

Thread: [RESOLVED] Rearranging Multidimensional Array

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Resolved [RESOLVED] Rearranging Multidimensional Array

    Hello,

    I have an array that looks like this:
    Code:
    Array
    (
        [0] => Array
            (
                [bandwidthlocalTime] => 1349617319
                [bandwidthTx] => 6
                [bandwidthRx] => 1
                [bandwidthAdapter] => eth0
                [bandwidthChannel] => 5sec
            )
    
        [1] => Array
            (
                [bandwidthlocalTime] => 1349617319
                [bandwidthTx] => 1
                [bandwidthRx] => 1
                [bandwidthAdapter] => ppp0
                [bandwidthChannel] => 5sec
            )
    
        [2] => Array
            (
                [bandwidthlocalTime] => 1349617325
                [bandwidthTx] => 2
                [bandwidthRx] => 3
                [bandwidthAdapter] => eth0
                [bandwidthChannel] => 5sec
            )
    
        [3] => Array
            (
                [bandwidthlocalTime] => 1349617325
                [bandwidthTx] => 3
                [bandwidthRx] => 3
                [bandwidthAdapter] => ppp0
                [bandwidthChannel] => 5sec
            )
    
        [4] => Array
            (
                [bandwidthlocalTime] => 1349617331
                [bandwidthTx] => 2
                [bandwidthRx] => 0
                [bandwidthAdapter] => eth0
                [bandwidthChannel] => 5sec
            )
    
        [5] => Array
            (
                [bandwidthlocalTime] => 1349617331
                [bandwidthTx] => 0
                [bandwidthRx] => 0
                [bandwidthAdapter] => ppp0
                [bandwidthChannel] => 5sec
            )
    I need it to basically be like this:
    Code:
    Array
    (
        [0] => Array
            (
                [bandwidthlocalTime] => 1349617319
                [bandwidthTx_eth0] => 6
                [bandwidthRx_eth0] => 1
                [bandwidthTx_ppp0] => 1
                [bandwidthRx_ppp0] => 1
                [bandwidthChannel] => 5sec
            )
    
        [1] => Array
            (
                [bandwidthlocalTime] => 1349617325
                [bandwidthTx_eth0] => 2
                [bandwidthRx_eth0] => 3
                [bandwidthTx_ppp0] => 3
                [bandwidthRx_ppp0] => 3
                [bandwidthChannel] => 5sec
            )
    
        [2] => Array
            (
                [bandwidthlocalTime] => 1349617331
                [bandwidthTx_eth0] => 2
                [bandwidthRx_eth0] => 0
                [bandwidthTx_ppp0] => 0
                [bandwidthRx_ppp0] => 0
                [bandwidthChannel] => 5sec
            )
    Basically, it's pulling some data from a database out like in the first array. There is one entry for each adapter at the same time. I basically need to make the timestamp the primary dimension of the array (With no duplicates) and then have 'bandwidthTx' and 'bandwidthRx' with 'bandwidthAdapter' in the second dimension of the array. It is easy to do with some hard IF statements, but I can't seem to get my head around it with an unknown amount of adapters (There could be more then eth0 and ppp0 for example).

    Here's my current code, but I'm fairly sure it's not close to being correct:
    PHP Code:
    $listLineStatsNumbers=getbwStatsNumbers();

    foreach (
    $listLineStatsNumbers as $listLineStatsNumbersIndexForAdapter => $listLineStatsNumbersValueForAdapter)
    {
      
    $adapterListFull[]=$listLineStatsNumbersValueForAdapter['bandwidthAdapter'];
    }

    $adapterList=array_unique($adapterListFull);

    foreach (
    $listLineStatsNumbers as $listLineStatsNumbersIndexForTime => $listLineStatsNumbersValueForTime)
    {
      foreach (
    $adapterList as $adapterListIndex => $adapterListValue)
      {
        
    $dataToUse[$listLineStatsNumbersValueForTime['bandwidthlocalTime']]=array("bandwidthlocalTime" => $listLineStatsNumbersValueForTime['bandwidthlocalTime'], "bandwidthTx_" $adapterList[$adapterListIndex] => $listLineStatsNumbersValueForTime['bandwidthTx'],  "bandwidthRx_" $adapterList[$adapterListIndex] => $listLineStatsNumbersValueForTime['bandwidthRx']);
      }
      
    $adapterListFull[]=$listLineStatsNumbersValueForTime['bandwidthAdapter'];
    }

    print_r ($unique);
    $indexCounter=0;

    foreach (
    $dataToUse as $listLineStatsNumbersIndex => $listLineStatsNumbersValue)
    {
      
    $data[] = array(date("H:i:s",$listLineStatsNumbersValue['bandwidthlocalTime']),  $listLineStatsNumbersValue['bandwidthTx_' $listLineStatsNumbersValue["bandwidthAdapter"]],  $listLineStatsNumbersValue['bandwidthRx_' $listLineStatsNumbersValue["bandwidthAdapter"]]);
      
    $indexCounter++;
      
    //echo $listLineStatsNumbersValue['bandwidthTx_'] . $listLineStatsNumbersValue["bandwidthAdapter"]"";


  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Rearranging Multidimensional Array

    If you are having trouble with the names, you could either use sql alias and change the returned resultset's column names. Or you could change the keys, when you populate the array from the db values instead.


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Rearranging Multidimensional Array

    $listLineStatsNumbers=getbwStatsNumbers();

    foreach ($listLineStatsNumbers as $listLineStatsNumbersIndexForTime => $listLineStatsNumbersValueForTime)
    {
    $dataToUse[$listLineStatsNumbersValueForTime['bandwidthlocalTime']]["bandwidthTx_" . $listLineStatsNumbersValueForTime["bandwidthAdapter"]] = $listLineStatsNumbersValueForTime['bandwidthTx'];
    $dataToUse[$listLineStatsNumbersValueForTime['bandwidthlocalTime']]["bandwidthRx_" . $listLineStatsNumbersValueForTime["bandwidthAdapter"]] = $listLineStatsNumbersValueForTime['bandwidthRx'];
    }
    Figured it out! Wasn't as complex as I made it out to be XD.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width