Solved

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

Posted on 2015-02-24
5
80 Views
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] => nelson.delvalle@verizonwireless.com [email] => nelson.delvalle@verizonwireless.com [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] => http://southareanetwork.nss.vzwnet.com/weekly_reports/index.php [url] => http://southareanetwork.nss.vzwnet.com/weekly_reports/index.php ) )

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?
0
Comment
Question by:brucegust
  • 2
  • 2
5 Comments
 
LVL 34

Accepted Solution

by:
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"].
0
 
LVL 30

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

0
 
LVL 34

Assisted Solution

by:gr8gonzo
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.
0
 

Author Comment

by:brucegust
ID: 40628443
Perfect! Thank, Gonzo!
0
 

Author Comment

by:brucegust
ID: 40628447
...and Marco!
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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

744 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

13 Experts available now in Live!

Get 1:1 Help Now