Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Itterate & Sort 2D Array

Posted on 2014-12-16
5
Medium Priority
?
107 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
[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
  • 4
5 Comments
 
LVL 111

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 111

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 111

Accepted Solution

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.
Suggested Courses

618 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