Jump to content

Splitting a multidimensional Array


Nodral

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
https://forums.phpfreaks.com/topic/236224-splitting-a-multidimensional-array/
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.

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

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.