Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 108
  • Last Modified:

Itterate & Sort 2D Array

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
APD_Toronto
Asked:
APD_Toronto
  • 4
1 Solution
 
Ray PaseurCommented:
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
 
Ray PaseurCommented:
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
 
APD_TorontoAuthor Commented:
This helps, but what if in the foreach i just want to out my names?
0
 
Ray PaseurCommented:
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
 
Ray PaseurCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now