?
Solved

Compare array values from multidimensional array

Posted on 2014-11-21
5
Medium Priority
?
695 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 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

Expert Comment

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

Featured Post

Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
This article discusses how to implement server side field validation and display customized error messages to the client.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Simple Linear Regression
Suggested Courses

752 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