10 Replies - 1451 Views - Last Post: 21 January 2016 - 02:18 AM Rate Topic: -----

#1 Joda-Code   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 23-November 14

How to save content of multidimensional array in csv file

Posted 20 January 2016 - 05:16 PM

Hello, I have an multidimensional array and I am trying to store that content into an file. I have tried with two nested loops and with a single loop. Below My array.
array(1) { [0]=> array(4) { [0]=> string(13) "170.75.162.19" [1]=> string(19) "2016:01:21 00:13:16" [2]=> array(1) { ["dl"]=> string(7) "log.csv" } [3]=> string(109) "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36" } } 


Here is the error that i get when I use nested loops. "Notice: Array to string conversion".

My code
function WriteToFile($file_name, $second_file_name, $data){
		$file = fopen($file_name, 'a');
		if(is_array($data) || is_object($data)){
			foreach ($data as $key => $value) {
				foreach ($value as $key2 => $value2) {
					fputcsv($file, $value2);
				}
			}
		}
		copy($file_name, $second_file_name);
		fclose($file);
	}


Is This A Good Question/Topic? 0
  • +

Replies To: How to save content of multidimensional array in csv file

#2 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2584
  • View blog
  • Posts: 10,353
  • Joined: 03-December 12

Re: How to save content of multidimensional array in csv file

Posted 20 January 2016 - 06:07 PM

You could just implode the data into a string and put the contents into a file.
Was This Post Helpful? 0
  • +
  • -

#3 Joda-Code   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 23-November 14

Re: How to save content of multidimensional array in csv file

Posted 20 January 2016 - 06:25 PM

Can you further explain. Why I cannot just loop and pass the values?
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2584
  • View blog
  • Posts: 10,353
  • Joined: 03-December 12

Re: How to save content of multidimensional array in csv file

Posted 20 January 2016 - 06:30 PM

You can, but why not do it in a simpler way if it is available?

Not sure where you assembled the array, but with as nested as it is, it is going to take some work to get what you want out. In fact, the implode will probably need to be run a few times. If you care to explain how that array came about, you should probably work on it first.
Was This Post Helpful? 0
  • +
  • -

#5 Joda-Code   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 23-November 14

Re: How to save content of multidimensional array in csv file

Posted 20 January 2016 - 06:35 PM

Here how I set up the array.
function GetUserData(){
		$user_ip = $_SERVER['REMOTE_ADDR'];
		$date_time = gmdate('Y:m:d H:i:s');

		$request = $_REQUEST;

		$user_data = $_SERVER['HTTP_USER_AGENT'];

		$data_array = array(
			array($user_ip, $date_time, $request, $user_data));
		return $data_array;
	}

Was This Post Helpful? 0
  • +
  • -

#6 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2584
  • View blog
  • Posts: 10,353
  • Joined: 03-December 12

Re: How to save content of multidimensional array in csv file

Posted 20 January 2016 - 07:00 PM

Now look at this version:
function GetUserData(){
    $user_ip = $_SERVER['REMOTE_ADDR'];
    $date_time = gmdate('Y:m:d H:i:s');

    $request = array();
    if ( !empty( $_REQUEST ) ){
        foreach( $_REQUEST as $key => $value ){
            array_push( $request, "$key: $value");
        }
    }
    $user_data = $_SERVER['HTTP_USER_AGENT'];

    $data_array = array($user_ip, $date_time, implode(',', $request), $user_data);
    return $data_array;
}

echo "<pre>";
print_r( GetUserData());
echo "</pre>";

Was This Post Helpful? 1
  • +
  • -

#7 Joda-Code   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 23-November 14

Re: How to save content of multidimensional array in csv file

Posted 20 January 2016 - 07:20 PM

Ok I notice the difference but I am still having a hard time doing this. Here is an example out of the PHP documentation that I follow. I do not understand why I cannot get this to work.
<?php

$list = array (
    array('aaa', 'bbb', 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('"aaa"', '"bbb"')
);

$fp = fopen('file.csv', 'w');

foreach ($list as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);
?>

Was This Post Helpful? 0
  • +
  • -

#8 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2584
  • View blog
  • Posts: 10,353
  • Joined: 03-December 12

Re: How to save content of multidimensional array in csv file

Posted 20 January 2016 - 07:27 PM

You start with 4 different arrays. You loop over one of them.
Was This Post Helpful? 0
  • +
  • -

#9 Joda-Code   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 23-November 14

Re: How to save content of multidimensional array in csv file

Posted 20 January 2016 - 07:39 PM

Got it to work with implode. Thanks a lot.
Was This Post Helpful? 0
  • +
  • -

#10 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2584
  • View blog
  • Posts: 10,353
  • Joined: 03-December 12

Re: How to save content of multidimensional array in csv file

Posted 20 January 2016 - 07:42 PM

As far as using loops:


$list = array(
    array(
        'aaa',
        'bbb',
        'ccc',
        'dddd'
    ),
    array(
        '123',
        '456',
        '789'
    ),
    array(
        '"aaa"',
        '"bbb"'
    )
);

$fp = fopen('file.csv', 'w');

if (is_array($list)) {
    foreach ($list as $fields) {
        if (is_array($fields)) {
            fputcsv($fp, $fields);
        }
    }
}

fclose($fp);



Was This Post Helpful? 0
  • +
  • -

#11 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4211
  • View blog
  • Posts: 13,301
  • Joined: 08-June 10

Re: How to save content of multidimensional array in csv file

Posted 21 January 2016 - 02:18 AM

View PostJoda-Code, on 21 January 2016 - 01:16 AM, said:

Hello, I have an multidimensional array and I am trying to store that content into an file.

CSV per se cannot hold nested data. itís a simple table-like structure. so you would either have to serialise the nested data (implode(), serialize()) or use a different format that supports nested data (like JSON).
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1