Solved

Use php to convert multidimensional array to a csv file

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

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
Thanks for the tips and the quick reply.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Thanks for using EE!  All the best, ~Ray
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

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 …
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…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 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

14 Experts available now in Live!

Get 1:1 Help Now