Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

I need help working with multidimensional arrays in php

I am having trouble looping through multidimensional array and sorting the data.  I was trying to write a function that would sort and make it easier to display.

here is my data, I have several more position than 2 but they all contain the same setup:

[values] contains and array with up to 10 positions
[frequence] contains only position [0] with a number

Array (
[0] => Array (
  [values] => Array (
    [0] => 47
    [1] => 53
    )
  [frequence] => 2
  )
[1] => Array (
  [values] => Array (
    [0] => 57
    [1] => 33
    [2] => 48
    [3] => 23
    )
  [frequence] => 5
  )
/************RINSE AND REPEAT*********/

I would like to display the data table as such:
Values:               Frequency:
(47, 53)                     (2)
(57, 43, 28, 53)         (5)

also I would like to sort in order of the highest frequence to the lowest.

any insight would be helpful, thanks in advance.




0
bdgbrick
Asked:
bdgbrick
  • 4
  • 2
1 Solution
 
Ray PaseurCommented:
There is a usort() function that might help you.  I'll see if I have an example.
0
 
Ray PaseurCommented:
See it in action here:
http://www.laprbass.com/RAY_temp_usort.php
<?php // RAY_temp_usort.php
error_reporting(E_ALL);
echo "<pre>";

// TEST DATA FROM THE POST AT EE
$thing = array();
$thing[] = Array ( 'values' => Array ( 47, 53 ),         'frequence' => 2 );
$thing[] = Array ( 'values' => Array ( 57, 33, 48, 23 ), 'frequence' => 5 );

// SHOW THE TEST DATA
print_r($thing);

// A FUNCTION TO COMPARE BY 'frequence'
function cmp_freq($a, $b)
{
    if ($a["frequence"] == $b["frequence"]) return 0;
    return ($a["frequence"] > $b["frequence"]) ? -1 : 1;
}


// COPY ARRAY AND SORT ON NAME
$test = $thing;
usort($test, 'cmp_freq');

// SHOW THE WORK PRODUCT
print_r($test);

Open in new window

0
 
bdgbrickAuthor Commented:
That worked great!  Any insight on how to loop through $test and display the data in a table like this:

Values:               Frequency:
(57, 43, 28, 53)         (5)
(47, 53)                     (2)


Thanks
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Ray PaseurCommented:
Sure!  Line 25, et seq.
<?php // RAY_temp_usort.php
error_reporting(E_ALL);
echo "<pre>";

// TEST DATA FROM THE POST AT EE
$thing = array();
$thing[] = Array ( 'values' => Array ( 47, 53 ),         'frequence' => 2 );
$thing[] = Array ( 'values' => Array ( 57, 33, 48, 23 ), 'frequence' => 5 );

// SHOW THE TEST DATA
print_r($thing);

// A FUNCTION TO COMPARE BY 'frequence'
function cmp_freq($a, $b)
{
    if ($a["frequence"] == $b["frequence"]) return 0;
    return ($a["frequence"] > $b["frequence"]) ? -1 : 1;
}


// COPY ARRAY AND SORT ON NAME
$test = $thing;
usort($test, 'cmp_freq');

// SHOW THE WORK PRODUCT IN A TABULAR FORMAT
echo "</pre>";
echo "<table><tr><th>Values</th><th>Frequency</th></tr>" . PHP_EOL;
foreach ($test as $sub_array)
{
    $vals
    = '('
    . implode(', ', $sub_array["values"])
    . ')'
    ;

    $freq
    = '('
    . $sub_array["frequence"]
    . ')'
    ;

    echo "<tr><td>$vals</td><td>$freq</td></tr>" . PHP_EOL;
}
echo "</table>" . PHP_EOL;

Open in new window

0
 
bdgbrickAuthor Commented:
A++++++++++
Wow, I would love to be able to program like that some day.

Thanks!
0
 
Ray PaseurCommented:
Thanks for the points -- it's a great question.  Programming is like anything else, including hitting a golf ball, playing piano, etc.  Practice is what gets you there. ;-)

All the best, ~Ray
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now