We help IT Professionals succeed at work.

I need help working with multidimensional arrays in php

bdgbrick
bdgbrick asked
on
326 Views
Last Modified: 2013-11-13
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.




Comment
Watch Question

Most Valuable Expert 2011
Author of the Year 2014

Commented:
There is a usort() function that might help you.  I'll see if I have an example.
Most Valuable Expert 2011
Author of the Year 2014

Commented:
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

Author

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
Most Valuable Expert 2011
Author of the Year 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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

Thanks!
Most Valuable Expert 2011
Author of the Year 2014

Commented:
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
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.