Solved

Itterate & Sort 2D Array

Posted on 2014-12-16
5
101 Views
Last Modified: 2014-12-16
Hi Experts,

I'm new to PHP Arrays, but I have:

$employees = array();

$emp['name'] = 'john';
$emp['phone'] = '1234567890';
$emp['id'] = 123;

$employees[] = $emp;

$emp['name'] = 'bob';
$emp['phone'] = '9876543210';
$emp['id'] = 456;

$employees[] = $emp;

Open in new window


My questions are:

1-how can I sort these within $employees by name?

2-how can I itterate through $employees, I understand foreach ($employee as $e), but what is ($employees as $key => $value)?

Thank you
0
Comment
Question by:APD_Toronto
  • 4
5 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40503537
You can use PHP usort() to sort multidimensional arrays.  I'll see if I can come up with an example for this test data set.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40503563
Please see: http://iconoun.com/demo/temp_apd_toronto.php

<?php // demo/temp_apd_toronto.hp
error_reporting(E_ALL);

// MAKE TEH OUTPUT EASY TO READ
echo '<pre>';

// CREATE AN EMPTY ARRAY
$employees = array();

// CREATE A SMALL ASSOCIATIVE ARRAY
$emp['name'] = 'john';
$emp['phone'] = '1234567890';
$emp['id'] = 123;

// INJECT THIS ARRAY INTO THE LARGER ARRAY
$employees[] = $emp;

// CREATE A SMALL ASSOCIATIVE ARRAY
$emp['name'] = 'bob';
$emp['phone'] = '9876543210';
$emp['id'] = 456;

// INJECT THIS ARRAY INTO THE LARGER ARRAY
$employees[] = $emp;

// SHOW THE INITIAL STATE OF THE LARGE ARRAY OF SMALLER ASSOCIATIVE ARRAYS
print_r($employees);

// CREATE A FUNCTION THAT CAN SORT THE LARGER ARRAY
// USING AN ELEMENT OF THE SMALLER ASSOCIATIVE ARRAYS
function cmp_name($a, $b)
{
    if ($a["name"] == $b["name"]) return 0;
    return ($a["name"] < $b["name"]) ? -1 : 1;
}

// RUN THE FUNCTION (MIGHT ALSO USE A CLOSURE)
usort($employees, 'cmp_name');

// SHOW THE WORK PRODUCT
var_dump($employees);

// SHOW HOW TO USE THE ITERATOR
foreach ($employees as $key => $value)
{
    echo PHP_EOL . "KEY: $key";
    echo PHP_EOL . 'VALUE: ';
    print_r($value);
    echo PHP_EOL;
}

Open in new window

0
 

Author Comment

by:APD_Toronto
ID: 40503623
This helps, but what if in the foreach i just want to out my names?
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40503750
PHP echo is used to write output to the browser.  If you're new to PHP and want to learn how to use the language, this article can help steer you in the right direction.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

Please see lines 52, et seq:
<?php // demo/temp_apd_toronto.hp
error_reporting(E_ALL);

// MAKE TEH OUTPUT EASY TO READ
echo '<pre>';

// CREATE AN EMPTY ARRAY
$employees = array();

// CREATE A SMALL ASSOCIATIVE ARRAY
$emp['name'] = 'john';
$emp['phone'] = '1234567890';
$emp['id'] = 123;

// INJECT THIS ARRAY INTO THE LARGER ARRAY
$employees[] = $emp;

// CREATE A SMALL ASSOCIATIVE ARRAY
$emp['name'] = 'bob';
$emp['phone'] = '9876543210';
$emp['id'] = 456;

// INJECT THIS ARRAY INTO THE LARGER ARRAY
$employees[] = $emp;

// SHOW THE INITIAL STATE OF THE LARGE ARRAY OF SMALLER ASSOCIATIVE ARRAYS
print_r($employees);

// CREATE A FUNCTION THAT CAN SORT THE LARGER ARRAY
// USING AN ELEMENT OF THE SMALLER ASSOCIATIVE ARRAYS
function cmp_name($a, $b)
{
    if ($a["name"] == $b["name"]) return 0;
    return ($a["name"] < $b["name"]) ? -1 : 1;
}

// RUN THE FUNCTION (MIGHT ALSO USE A CLOSURE)
usort($employees, 'cmp_name');

// SHOW THE WORK PRODUCT
var_dump($employees);

// SHOW HOW TO USE THE ITERATOR
foreach ($employees as $key => $value)
{
    echo PHP_EOL . "KEY: $key";
    echo PHP_EOL . 'VALUE: ';
    print_r($value);
    echo PHP_EOL;
}

// SHOW HOW TO LIST JUST THE NAMES
echo PHP_EOL . "HERE ARE THE NAME ELEMENTS OF THE ARRAYS OF DATA:";
foreach ($employees as $value)
{
    echo PHP_EOL . $value['name'];
}

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40503804
Thanks for the points - it's a great question.  USort() is one of the PHP features that is often overlooked.  Best of luck with your project, ~Ray
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

831 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