Solved

Itterate & Sort 2D Array

Posted on 2014-12-16
5
98 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 108

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 108

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 108

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 108

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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 four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now