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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
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…

630 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