Jnewbie Jnewbie - 3 months ago 14
PHP Question

PHP variable is printing type not content

i've stucked for a while with php..

In my code:

for($i = 0; $i < $max;$i++){
if(//my condition){
$job_found.= $obj[$i];
}
else{
echo "jobs not found";
}
echo ($job_found);
}


When it gets a coincidence the results will be:


Array


But if i try:

print_r ($obj[1]);


The result will be:


Array ( [title] => my 2title [placement] => my placement [date] => my date [time] => My time [website] => http://www.g00gle.com )


How can i get the actual value of the array in that position and not just the type?

Thanks in advance

Answer

First, as you can see from your own Post, $obj[1] is an Array, which means that all the Elements in the $obj variable are most likely Arrays as well. Unfortunately, you cannot just echo an Array but iterate through it to get the echoable data like so:

<?php

    $jobsHTML        = "";

    for($i = 0; $i < $max;$i++){
        if(!$jobsStillExist){       // CONDITION TO CHECK IF JOBS STILL EXIST
            echo "jobs not found";
            continue;
        }
        // SINCE, $obj[$i] COULD BE AN ARRAY,
        // YOU NEED TO CHECK THE TYPE 1ST. 
        // IF IT IS A STRING, APPEND IT AS A STRING TO $jobsHTML
        // OTHERWISE, LOOP THROUGH IT TO GET IT'S CONTENT...
        $foundJob   = $obj[$i];
        if(is_array($foundJob)){
            foreach($foundJob as $jobData){
                $jobsHTML .= $jobData["title"]                            . "<br />";
                $jobsHTML .= $jobData["placement"]                        . "<br />";
                $jobsHTML .= date("d/m/Y", strtotime($jobData["date"]))   . "<br />";
                $jobsHTML .= $jobData["time"]                             . "<br />";
                $jobsHTML .= $jobData["website"]                          . "<br /><br />";
            }
        }else if(is_string($foundJob)){
            $jobsHTML .= $foundJob                                        . "<br /><br />";
        }
    }

    echo ($jobsHTML);