Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to print out an array

Status
Not open for further replies.

someoneneedshelps

Technical User
Mar 3, 2016
75
0
0
GB
How do I print this out please or how do I call it first column and second column which is the type
Code:
	$types = array(
	'user' => 'int',
	'title' => 'text',
	'subtitle' => 'text',
	'starts' => 'time',
	'description' => 'text',
	'pict_url' => 'image',
	'category' => 'int',
	'secondcat' => 'int',
	'minimum_bid' => 'float',
	'shipping_cost' => 'float',
	'shipping_cost_additional' => 'float',
	'reserve_price' => 'float',
	'buy_now' => 'float',
	'auction_type' => 'digitnory',
	'duration' => 'int',
	'increment' => 'float',
	'shipping' => 'yorn',
	'payment' => 'pay',
	'international' => 'yorn',
	'ends' => 'int',
	'current_bid' => 'float',
	'closed' => 'digityorn',
	'photo_uploaded' => 'digityorn',
	'quantity' => 'int',
	'suspended' => 'int',
	'relist' => 'int',
	'relisted' => 'int',
	'num_bids' => 'int',
	'sold' => 'yorn',
	'shipping_terms' => 'text',
	'bn_only' => 'yorn',
	'bold' => 'yorn',
	'highlighted' => 'yorn',
	'featured' => 'yorn',
	'current_fee' => 'float',
	'tax' => 'yorn',
	'taxinc' => 'yorn',
	'asking' => 'float',
	'item_condition' => 'text',
	'item_manufacturer' => 'text',
	'item_model' => 'text',
	'item_colour' => 'text',
	'item_year' => 'text'
	);

Thank you
 
Not sure I understand what exactly you want to do.

If you want to get a value from the array, you use the index key for it.

$value = $types[keyname'];

so

Code:
$value = $types['user'];
echo $value;

would print out 'int';

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thats great thanks but i have to do this on the fly with data aswell, i suppose i could u a switch statement in the function and use join to concatenate into an insert statement .... What you think?
 
There are several ways to "print" out an array.

As Vacunita pointed out you can [tt]echo[/tt] or [tt]print[/tt]

Code:
echo $types['user'];
print $types['user'];
print($types['user']);

You can also combine that with variables

Code:
$key = 'user';
echo $types[$key];
print $types[$key];
print($types[$key]);

You can also output that with other strings like:

Code:
echo 'This is the user: ' . $types['user'] . '.';
echo "This is the user: {$types['user']}.";

*Note* Only a double quoted string will interpolate the variables inside it.

For debugging purposes, you can also output the entire contents of an array using [tt]var_dump[/tt] or [tt]print_r[/tt].

Code:
var_dump($types);
print_r($types);

The following is an example of [tt]var_dump[/tt], which I prefer due to also outputting the data type which [tt]print_r[/tt] does not do.

[pre]array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
}[/pre]
 
Yes thxs for that, see my other thread, this is to parse each column of data out of csv, i keep getting same results out as print_r
 
Ok, so if I load this file into an array

Code:
		$file_contents = file($filename);
		$titles = $file_contents[0];
		$title_array =  explode(";", $titles);
		echo '<br>----   title start ------<br>';
		foreach($title_array as $key => $val)
		{
			echo "value is " . $val . "<br>";
		}		
		echo '<br>----   title end ------<br>';

I get first line, but I have many lines of data, soon as I move
Code:
$titles = $file_contents[0];

I get nothing
 
Let's look at this line by line:

Code:
$file_contents = file($filename);  [COLOR=#4E9A06]// Grabs the contents of $filename and puts it into an array with each line of the file a different index[/color]
$titles = $file_contents[0]; [COLOR=#4E9A06]// Grabs the first line of the file as a string[/color]
$title_array = explode(";", $titles); [COLOR=#4E9A06]// Explodes the string into an array using ";" as a separator[/color]
echo '<br>----  title start ------<br>'; [COLOR=#4E9A06]//echoes the string[/color]
foreach($title_array as $key => $val) [COLOR=#4E9A06]// Loops through the titles from the first line only, putting the array index into $key and the column title into $val[/color]
    echo "value is " . $val . "<br>"; [COLOR=#4E9A06]// Echoes the string and title value[/color]
echo '<br>----  title end ------<br>'; [COLOR=#4E9A06]//echoes the string[/color]

If you remove (or move) the line [tt]$titles = $file_contents[0];[/tt], then that leaves it undefined for the loop and you will get nothing.

So far as I can see it your code is working exactly as expected.

It's unclear as to the outcome you are looking for. If you want to display every row of the file, you will need another loop to loop through the lines of the file:

Code:
$file_contents = file($filename);
foreach($file_contents as $line_number => $line)
{
    $columns = explode(";", $line); [COLOR=#4E9A06]//assumes delimiter on every line is a ";"[/color]
    echo "<br>----  Start Line " . $line_number . " ----<br>";
    foreach($columns as $column_number => $column)
    {
        echo "Column " . $column_number . " value is " . $column . "<br>";
    }
    echo "<br>----  End Line " . $line_number . " ----<br>";
}
 
Great work thanks, I have got there in the end, have a another question its related, I have this data coming into a function one by one

int,text,text,time,text,image,int,int,float,float,float,float,float,digitnory,int,

it then goes through a switch statement

Code:
	function switch_to_get_values($valuetype)
	{
		echo $valuetype;
		switch ($valuetype) 
		{
			case "int":				
				$value=false;
				break;
			
			case "time":
				$value=true;
				break;
						
			case "text":
				$value=true;				
				break;

			case "digityorn":
				$value=true;
				break;
			
			case "digitnory":
				$value=true;
				break;
				
			case "yorn":				
				$value=true;
				break;

			case "float":
				$value=false;
				break;
			default:		
				$value=false;
				break;
				
			return $value;
		}
	}

will the above data get a match wrapped in quotes because mine isn't
Code:
int,text,text,time,text,image,int,int,float,float,float,float,float,digitnory,int,
 
As related as you think it might be, it should be a separate question (remember that for the future).

Nothing is returning, because your "return $value" statement is within the switch block
Code:
function switch_to_get_values($valuetype)
    {
        echo $valuetype;
        switch ($valuetype) 
        {
            case "int":				
                $value=false;
                break;
            [COLOR=#4E9A06]//repeat other cases here[/color]
        }
        return $value;
    }
 
See? Wood for trees .... thanks ive improved the switch also
Code:
function switch_to_get_values($valuetype)
	{
		switch ($valutype)
			case "time":	
			case "text":
				$value=true;				
				break;

			case "digityorn":
			case "digitnory":
			case "yorn":				
				value = true;
                                Break;
			case "float";
                        Case "int";
                                Value = false;
			default:		
				$value=false;
				break;
		}
                return $value;
	}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top