×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

How to print out an array

How to print out an array

How to print out an array

(OP)
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

RE: How to print out an array

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

RE: How to print out an array

(OP)
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?

RE: How to print out an array

There are several ways to "print" out an array.

As Vacunita pointed out you can echo or print

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 var_dump or print_r.

CODE

var_dump($types);
print_r($types); 

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

array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  array(3) {
    [0]=>
    string(1) "a"
    [1]=>
    string(1) "b"
    [2]=>
    string(1) "c"
  }
} 

RE: How to print out an array

(OP)
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

RE: How to print out an array

(OP)
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

RE: How to print out an array

Let's look at this line by line:

CODE

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

If you remove (or move) the line $titles = $file_contents[0];, 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); //assumes delimiter on every line is a ";"
    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>";
} 

RE: How to print out an array

(OP)
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, 

RE: How to print out an array

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;
            //repeat other cases here
        }
        return $value;
    } 

RE: How to print out an array

(OP)
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;
	} 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close