Jump to content

Splitting a multidimensional Array


Nodral
 Share

Recommended Posts

Hi All

 

How do I go about splitting a multidimensional array into seperate smaller ones?

 

Currently I have $grade[region][userid]=value.

 

I'd like to split as

  • $region1[userid]=value
  • $region2[userid]=value
  • $region3[userid]=value
  • $region4[userid]=value
  • $region5[userid]=value
  • $region6[userid]=value
     

 

Is there an inbuilt function for this?

Link to comment
Share on other sites

Seems odd that you'd want to split the array like that. Given the data you're working with, an array would make things easier for yourself in terms of looping and access.

 

Never the less... If you want to do this dynamically, you'll need to use a variable variable to dynamically create the variable name with the 1, 2, 3 etc. on the end. I'm not sure by your example data whether the existing region keys contain 'region<#>' or just '<#>', so will provide the code for both.

 

If the keys are 'region<#>':

 

$grade = array(
    'region1' => array(
        1 => 'value',
        2 => 'value'
    ),
    'region2' => array(
        3 => 'value',
        4 => 'value'
    )
);

foreach ($grade as $key => $region)
{
    $$key = $region;
}

print_r($region1);
print_r($region2);

 

.. And if the keys are just '<#>':

 

$grade = array(
    1 => array(
        1 => 'value',
        2 => 'value'
    ),
    2 => array(
        3 => 'value',
        4 => 'value'
    )
);

foreach ($grade as $key => $region)
{
    $var_name = 'region' . $key;
    $$var_name = $region;
}

print_r($region1);
print_r($region2);

 

As I said before though, I think you'd be better off keeping the data within an array.

Link to comment
Share on other sites

Cheers for that.

 

Ok, as you suggest keeping it in one array, I need to provide an HTML table with columns REGION, COUNT of userids, Average value.

I then need to add a total at the bottom totalling count of all userid and an average of value.

 

Is this possible?

 

Sorry, I'm a newbie and been playing with this for a couple of days and getting nowhere fast.

 

Cheers

Link to comment
Share on other sites

Of course it is possible. And, as MrAdam stated, it does not make sense to split the array - it only adds complexity.

 

You don't show the full details of your array, so this is only mock code:

 

<?php

$tableOutoput = '';
foreach($grade as $region => $regionUsers)
{
    //calulate totals/averages for the region
    $userCount = count($regionUsers);
    $userValuesTotal = array_sum($regionUsers);
    $userValuesAverage = ($userCount>0) ? $userTotalValues/$userCount : 0;

    $tableOutoput .= "<tr>\n";
    $tableOutoput .= "<td>{$region}</td>\n";
    $tableOutoput .= "<td>{$userValuesTotal}</td>\n";
    $tableOutoput .= "<td>{$userValuesAverage}</td>\n";
    $tableOutoput .= "</tr>\n";
}
?>
<table>
    <tr>
        <th>Region</th>
        <th>User Count</th>
        <th>User Total Values</th>
        <th>User Average Values</th>
    </tr>
    <?php echo $tableOutoput; ?>
</table>

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.