Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Use php to convert multidimensional array to a csv file

Posted on 2014-02-12
3
Medium Priority
?
1,625 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

Expert Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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.
Suggested Courses

618 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