Solved

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

Posted on 2015-02-24
5
83 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 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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Checkout Page Input Field not aligned 1 26
Need to remove %20 from url in get 17 36
PHP Mail error 3 27
How to delete values from an array using php 4 23
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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 dynamically set the form action using jQuery.

837 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