Solved

Use php to convert multidimensional array to a csv file

Posted on 2014-02-12
3
1,508 Views
Last Modified: 2014-02-12
I'm a php newbie.  I have an array that is the result of a ldap search.  I need to convert the multidimensional array into a file that can is imported by one of our applications.  The import part is automated and file has to delivered in that format.

The array is in the format that follows (I made values generic)

[recordnumber] => Array
        (
            [firstname] => Array
                (
                    [count] => 1
                    [0] => John
                )

            [0] => firstname
            [initials] => Array
                (
                    [count] => 1
                    [0] => R.
                )

            [1] => initials
            [lastname] => Array
                (
                    [count] => 1
                    [0] => smith
                )

            [2] => sn
            [mail] => Array
                (
                    [count] => 1
                    [0] => jsmith@domain.com
                )

            [3] => mail
            [count] => 4
            [dn] => uid=jsmith,dc=domain,dc=com
        )

Files needs to be in this format.  Last field is a value derived on the fly depending on whether person has certain access rights.

|Smith, John R.|jsmith@domain.com||

I found lots of code examples online. None of which I can make work.  I have to flatten the array which seems to be the most difficult part.

Thanks.
0
Comment
Question by:P Silva
  • 2
3 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39854014
Please see http://www.laprbass.com/RAY_temp_lalunita.php

If you want to get started learning PHP this article can help.  Don't try to do this by looking at code examples online.  Most of them are useless or worse.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

<?php // RAY_temp_lalunita.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28363123.html

// SIMULATED TEST DATA
$arr = array('recordnumber' => Array
        (
            'firstname' => Array
                (
                    'count' => 1,
                    '0' => 'John'
                ),

            '0' => 'firstname',
            'initials' => Array
                (
                    'count' => 1,
                    '0' => 'R.'
                ),

            '1' => 'initials',
            'lastname' => Array
                (
                    'count' => 1,
                    '0' => 'smith'
                ),

            '2' => 'sn',
            'mail' => Array
                (
                    'count' => 1,
                    '0' => 'jsmith@domain.com'
                ),

            '3' => 'mail',
            'count' => 4,
            'dn' => 'uid=jsmith,dc=domain,dc=com'
        )
)
;

// ACTIVATE THIS TO SEE THE ARRAY
// var_dump($arr);

// EXTRACT THE DATA FIELDS
$fn = $arr['recordnumber']['firstname'][0];
$mi = $arr['recordnumber']['initials'][0];
$ln = $arr['recordnumber']['lastname'][0];
$em = $arr['recordnumber']['mail'][0];

// CORRECT CAPITALIZATION OF THE LAST NAME
$ln = ucfirst($ln);

// USE TEMPLATE REPLACEMENT WITH HEREDOC NOTATION
$out = <<<EOD
|$ln, $fn $mi|$em||
EOD;

// SHOW THE WORK PRODUCT
echo $out;

Open in new window

Best of luck with your PHP adventures! ~Ray
0
 

Author Closing Comment

by:P Silva
ID: 39854178
Thanks for the tips and the quick reply.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39854296
Thanks for using EE!  All the best, ~Ray
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php construct 5 27
paypal ipn url 5 57
php extract($_REQUEST) 5 51
Can't Setup WAMP. Keep Being Told I've Got the Wrong Directory... 3 17
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…
This article discusses four methods for overlaying images in a container on a web page
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 count occurrences of each item in an array.

773 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