Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Compare array values from multidimensional array

Posted on 2014-11-21
5
584 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 109

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 109

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 109

Expert Comment

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

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

828 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