Solved

Compare array values from multidimensional array

Posted on 2014-11-21
5
654 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
[X]
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
  • 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 110

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 110

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 110

Expert Comment

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

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Six Sigma Control Plans

687 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