Solved

Compare array values from multidimensional array

Posted on 2014-11-21
5
529 Views
Last Modified: 2014-11-24
I currently have a multidimensional array and I want to compare the values from the second date against the values from the first date:
Array
(
    [Jan 01, 2013] => Array
        (
            [COM] => 1.1
            [RES] => 1.0
        )

    [Oct 28, 2014] => Array
        (
            [COM] => 1.5
            [RES] => 1.0
        )
)

Open in new window

For example, for the [COM] key I see that the value of 1.5 is greater than the prior value of 1.1, but for the [RES] key the value of 1.0 is equal to the prior value for that same key.

What would be the best way to accomplish this?  Thanks.
0
Comment
Question by:DanielAttard
  • 3
5 Comments
 
LVL 8

Expert Comment

by:Ahmed Merghani
ID: 40458447
Your result can be an array with the compared keys. And the possible values can be 1 , 0, -1.
date1[key] > date2[key] => 1
date1[key] = date2[key] => 0
date1[key] < date2[key] => -1
For example as your array, the result cam be like this:
Array
        (
            [COM] => -1
            [RES] => 0
        )

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40458781
It would probably be good if you could tell us what you want for output, right?  And are there always only two array elements in the main array?
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40458800
Maybe something like this:
http://iconoun.com/demo/temp_daniel_attard.php

<?php // demo/temp_daniel_attard.php
error_reporting(E_ALL);
echo '<pre>';

/**
Array
(
    [Jan 01, 2013] => Array
        (
            [COM] => 1.1
            [RES] => 1.0
        )

    [Oct 28, 2014] => Array
        (
            [COM] => 1.5
            [RES] => 1.0
        )
)
*/

// CREATE THE TEST DATA
$arr = array
( 'Jan 01, 2013' => Array( 'COM' => 1.1, 'RES' => 1.0 )
, 'Oct 28, 2014' => Array( 'COM' => 1.5, 'RES' => 1.0 )
)
;

// CHECK - DOES IT LOOK RIGHT?  YES
print_r($arr);

// GET A LIST OF THE ARRAY KEYS IN THE MAIN ARRAY
$keys = array_keys($arr);
var_dump($keys);

// ISOLATE THE VALUES IN THE SUB-ARRAYS WITH THIS NOTATION
var_dump($arr[$keys[0]]);
var_dump($arr[$keys[0]]['COM']);
var_dump($arr[$keys[1]]['COM']);

// MAKE A COMPARISON AND SHOW THE RESULTS
if ( $arr[$keys[0]]['COM'] < $arr[$keys[1]]['COM'] ) echo PHP_EOL . "{$keys[0]} HAS VALUE {$arr[$keys[0]]['COM']} THAT IS LESS THAN THE VALUE OF {$arr[$keys[1]]['COM']} IN {$keys[1]}";

Open in new window

0
 

Author Closing Comment

by:DanielAttard
ID: 40462088
Another great solve for @Ray Paseur.  Thanks for showing me the right example, at the right time.  I learned from what you gave me and solved my issue.  Thanks.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40462673
Thanks for the points - it's a great question! ~Ray
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now