iltp21
iltp21

Reputation: 11

Unable to access array element

I am working on my first program written in PHP. This is a basic question. I am trying to print a string stored in an array. I have two arrays. One array, $content, stores a number at $content[$i][15] (i am using arrays in arrays, and looping through it with a for loop). Lets say this number is 3. I now want to access a string in another array, called $type. This string is at position $type[3]. Logically, I think, $type[3] should print the same thing as $type[$content[$i][15]]. But, when I call $type[$content[$i][15]], it doesn't print anything, but when i print $type[3] it works fine. Is this type of call not allowed in PHP?

$type = array();
$typetemp = readfile("type.csv");
foreach ($typetemp as $sa){
    $type[$sa[0]] = $sa[1];
}

$content = readfile("content.csv");

for($i=0; $i<sizeof($content); $i++){
    if($content[$i][15] == 3){
        if($content[$i][4] == 1){
            $temp = $content[$i][15];
            echo "id is " . $content[$i][0] . "title is " . $content[$i][1] . "introtext is " . $content[$i][2] . "restoftext is " . $content[$i][3] . "visible is " . $content[$i][4] . "category is " . $content[$i][5] . "creation_date is " . $content[$i][6] . "author is " . $content[$i][7] . "img is " . $content[$i][8] . "ordering is " . $content[$i][9] . "rank is " . $content[$i][10] . "vid is " . $content[$i][11] . "start_date is " . $content[$i][12] . "stop_date is " . $content[$i][13] . "event_date is " . $content[$i][14] . "type is " . $content[$i][15] . "thumb is " . $content[$i][16] . "type name is " . $type[$temp] . "<br/>";  
        }
    }
}

function readfile($filename) {
    $line_of_text = array();
    $file_handle = fopen($filename, "r");

    while (!feof($file_handle) ) {
      array_push($line_of_text, fgetcsv($file_handle, 1024));
    }
    fclose($file_handle);
    return $line_of_text;
}

Upvotes: -1

Views: 114

Answers (1)

user1726343
user1726343

Reputation:

You are missing a $ sign before content:

$type[$content[$i][15]]

This accesses the value in $type with index $content[$i][15]

Upvotes: 4

Related Questions