How would I retrieve the members of this array one at a time?

Posted on 2015-02-24
Last Modified: 2015-02-24
When I do a print_r(array_values($array));, I get this:

Array ( [0] => Array ( [0] => 3 [id] => 3 [1] => Weekly Reports [name] => Weekly Reports [2] => Nelson Del Valle [author] => Nelson Del Valle [3] => [email] => [4] =>
Authored by Nelson Del Valle, this is a collection of graphs and tables that reflect the status and progress of several metrics crucial to Verizon's commitment to quality control
 [description] =>
Authored by Nelson Del Valle, this is a collection of graphs and tables that reflect the status and progress of several metrics crucial to Verizon's commitment to quality control
 [5] => [url] => ) )

If I wanted to be able to grab just the id or just the email, how do I do it? I was thinking something along the lines of "echo $array[0] but that's not working.

How do I do it?
Question by:brucegust
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
  • 2
  • 2
LVL 35

Accepted Solution

gr8gonzo earned 333 total points
ID: 40628417
You have a multi-dimensional array, so it would be $array[0]["id"] or $array[0]["name"], etc...

If you had more than one record in your array, and you wanted to get the name of the 2nd record, it'd be $array[1]["name"].
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 167 total points
ID: 40628427
Your array seems the result of a query which returns database value in both associative and number indexed array.
If you're using PDO you can ue PDO::FETCH_ASSOC to get only an associative array

$result = $tmt->fetchAll($query, PDO::FETCH_ASSOC);

Open in new window

Anyway, to get email you can just use this:
echo $arr[0]['email'];

Open in new window

or this
echo $arr[0][3];

Open in new window

LVL 35

Assisted Solution

gr8gonzo earned 333 total points
ID: 40628430
Also, that output looks like it's the result of a mysql_fetch_array or mysqli_fetch_array call. IMHO, the _fetch_array leads to problems. You have more than one way to access the data, which can lead to inconsistent code (one piece of code uses the numeric indexes, while another piece of code might use the column names). It's better to pick one (associative is usually better) and use that instead. You should be able to replace _fetch_array with _fetch_assoc, for example, and it will just leave you with one set of data instead of two.

Author Comment

ID: 40628443
Perfect! Thank, Gonzo!

Author Comment

ID: 40628447
...and Marco!

Featured Post

Industry Leaders: 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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

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