Solved

Compare array values from multidimensional array

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

738 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