Jump to content

Blank array


cmb

Recommended Posts

this form i have takes some csv data and turns it into an array. For some reason the script inserts a blank key into the array and idk why.

this is my script code:

<?php

if(isset($_POST['csv_btn'])){
$csv = $_POST['csv'];

//turns csv into an array with each spot in the array a player and all data seperated by a comma
$csv_mod = explode("Home,",$csv);

$y = count($csv_mod) - 1;
$x = 0;
$team = array();

while ($x <= $y){
	//takes the array from the csv explode and explodes each player and their data
	$tx = $csv_mod[$x];
	$ex_tx = explode(",",$tx);

	$ex_tx_c = count($ex_tx);
	$z = 1;
	//is the players name
	$person = $ex_tx[0];
	$person2 = $ex_tx[0];
	//turn the player into an array
	$person = array();
	//puts the players data into their array
	while ($z < $ex_tx_c){
		array_push($person, $ex_tx[$z]);
		$z++;	
	}
	//adds the player to the team array with their name being the index 
	$team[$person2] = $person;
	$x++;
}
print_r($team);
}

?>

 

this is what i put into the script(exactly like this):

Home,AJ,2,1,2,2,0,0,0,0,0,0,0,0
Home,Conner,0,0,2,2,0,0,0,0,0,0,0,0
Home,Joe,0,0,1,1,0,0,0,1,0,0,0,0
Home,Tyler,0,0,0,0,0,0,0,0,0,0,0,0

 

this is what it outputs:

Array
(
    [] => Array
        (
        )

    [AJ] => Array
        (
            [0] => 2
            [1] => 1
            [2] => 2
            [3] => 2
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0


        )

    [Conner] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 2
            [3] => 2
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0


        )

    [Joe] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 1
            [3] => 1
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 1
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0


        )

    [Tyler] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
        )

)

Link to comment
Share on other sites

Use the built in CSV parser.

 

<?php

$str = 'Home,AJ,2,1,2,2,0,0,0,0,0,0,0,0
Home,Conner,0,0,2,2,0,0,0,0,0,0,0,0
Home,Joe,0,0,1,1,0,0,0,1,0,0,0,0
Home,Tyler,0,0,0,0,0,0,0,0,0,0,0,0';

$parts = explode( "\n", $str );
$parsed = array();

foreach( $parts as $part )
$parsed[] = str_getcsv($part);
unset($parts);

print_r($parsed);

?>

 

Gives

 

Array
(
    [0] => Array
        (
            [0] => Home
            [1] => AJ
            [2] => 2
            [3] => 1
            [4] => 2
            [5] => 2
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
            [12] => 0
            [13] => 0
        )

    [1] => Array
        (
            [0] => Home
            [1] => Conner
            [2] => 0
            [3] => 0
            [4] => 2
            [5] => 2
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
            [12] => 0
            [13] => 0
        )

    [2] => Array
        (
            [0] => Home
            [1] => Joe
            [2] => 0
            [3] => 0
            [4] => 1
            [5] => 1
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 1
            [10] => 0
            [11] => 0
            [12] => 0
            [13] => 0
        )

    [3] => Array
        (
            [0] => Home
            [1] => Tyler
            [2] => 0
            [3] => 0
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
            [12] => 0
            [13] => 0
        )

)

 

If you don't have str_getcsv on your server, you can use a UDF I made for this.

 

<?php

$csv = '1997,Ford,E350,"ac, abs, moon",3000.00
1999,Chevy,"Venture ""Extended Edition""","",4900.00
1999,Chevy,"Venture ""Extended Edition, Very Large""","",5000.00
1996,Jeep,Grand Cherokee,"MUST SELL!
air, moon roof, loaded",4799.00';

// print_r( csv_explode($csv) );

print_r( csv_explode($csv) );


/**
*
* Covert a multi-line CSV string into a 2d array. Follows RFC 4180, allows
* "cells with ""escaped delimiters""" and multi-line enclosed cells
* It assumes the CSV file is properly formatted, and doesn't check for errors
* in CSV format.
* @param string $str The CSV string
* @param string $d The delimiter between values
* @param string $e The enclosing character
* @param bool $crlf Return line breaks as CRLF
* @return array
*/
function csv_explode( $str, $d=',', $e='"', $crlf=FALSE ) {
// Convert CRLF to LF, easier to work with in regex
$str = str_replace("\r\n","\n",$str);
// Get rid of trailing linebreaks that RFC4180 allows
$str = trim($str);
// Do the dirty work
if ( preg_match_all(
	'/(?:
		'.$e.'((?:[^'.$e.']|'.$e.$e.')*+)'.$e.'(?:'.$d.'|\n|$)
			# match enclose, then match either non-enclose or double-enclose
			# zero to infinity times (possesive), then match another enclose,
			# followed by a comma, linebreak, or string end
		|	####### OR #######
		([^'.$d.'\n]*+)(?:['.$d.'\n]|$)
			# match anything thats not a comma or linebreak zero to infinity
			# times (possesive), then match either a comma or a linebreak or
			# string end
	)/x',
	$str, $ms, PREG_SET_ORDER
) === FALSE ) return FALSE;
// Initialize vars, $r will hold our return data, $i will track which line we're on
$r = array(); $i = 0;
// Loop through results
foreach( $ms as $m ) {
	// If the first group of matches is empty, the cell has no quotes
	if( empty($m[1]) )
		// Put the CRLF back in if needed
		$r[$i][] = isset($m[2]) ? $m[2] : '';
	else {
		// The cell was quoted, so we want to convert any "" back to " and
		// any LF back to CRLF, if needed
		$r[$i][] = ($crlf == TRUE) ?
			str_replace(
				array("\n",$e.$e),
				array("\r\n",$e),
				$m[1]) :
			str_replace($e.$e, $e, $m[1]);
	}
	// If the raw match doesn't have a delimiter, it must be the last in the
	// row, so we increment our line count.
	if( substr($m[0],-1) != $d )
		$i++;
}
// An empty array will exist due to $ being a zero-length match, so remove it
array_pop( $r );
return $r;

}

?>

 

Returns

 

Array
(
    [0] => Array
        (
            [0] => 1997
            [1] => Ford
            [2] => E350
            [3] => ac, abs, moon
            [4] => 3000.00
        )

    [1] => Array
        (
            [0] => 1999
            [1] => Chevy
            [2] => Venture "Extended Edition"
            [3] => 
            [4] => 4900.00
        )

    [2] => Array
        (
            [0] => 1999
            [1] => Chevy
            [2] => Venture "Extended Edition, Very Large"
            [3] => 
            [4] => 5000.00
        )

    [3] => Array
        (
            [0] => 1996
            [1] => Jeep
            [2] => Grand Cherokee
            [3] => MUST SELL!
air, moon roof, loaded
            [4] => 4799.00
        )

)

Link to comment
Share on other sites

Script:

<?php

$csv = 'Home,AJ,2,1,2,2,0,0,0,0,0,0,0,0
Home,Conner,0,0,2,2,0,0,0,0,0,0,0,0
Home,Joe,0,0,1,1,0,0,0,1,0,0,0,0
Home,Tyler,0,0,0,0,0,0,0,0,0,0,0,0';

$csv_mod = explode('Home,', $csv);

$rows = count($csv_mod);
for($i=0; $i<$rows; $i++){
$tmp = explode(',', $csv_mod[$i]);
$tmp_rows = count($tmp);
for($j=1; $j<$tmp_rows; $j++){
	$csv_array[$tmp[0]][] = $tmp[$j];
}
}

print_r($csv_array);

?>

 

Output:

Array
(
    [AJ] => Array
        (
            [0] => 2
            [1] => 1
            [2] => 2
            [3] => 2
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0

        )

    [Conner] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 2
            [3] => 2
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0

        )

    [Joe] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 1
            [3] => 1
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 1
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0

        )

    [Tyler] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
        )

)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.