PHP变量的打印类型不是内容

时间:2016-08-09 11:33:52

标签: php arrays

我已经用php坚持了一段时间..

在我的代码中:

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

如果巧合,结果将是:

  

阵列

但如果我尝试:

print_r ($obj[1]);

结果将是:

  

数组([title] =&gt; my 2title [placement] =&gt; my placement [date] =&gt; my date [time] =&gt;我的时间[网站] =&gt; http://www.g00gle.com)< / p>

如何在该位置获取数组的实际值而不仅仅是类型?

提前致谢

2 个答案:

答案 0 :(得分:1)

  

首先,正如您在自己的帖子中看到的那样,$obj[1]是一个数组,这意味着$obj变量中的所有元素也很可能是数组。不幸的是,你不能只是echo一个数组,而是遍历它以获得 echoable 数据,如下所示:

<?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); 

答案 1 :(得分:0)

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

print_r($job_found);
你能尝试这样吗?