Solved

Use php to convert multidimensional array to a csv file

Posted on 2014-02-12
3
1,529 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

839 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