Solved

Use php to convert multidimensional array to a csv file

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

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 110

Expert Comment

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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySQL limit and not so limited 13 41
How to use session variables in php? 22 51
Echo'd values in dropdowns 6 30
MySQL-Design Help 12 44
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

726 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