English 中文(简体)
能否在一片土地上获得财产?
原标题:Is there a shorter way to access a property in an array?

<代码>j_post是指通过<编码>打印_r(<>进行阵列输出 debug。 这一变量是一系列目标:

Array
(
    [0] => stdClass Object
        (
            [ID] => 2571
        )

)

我通过这样的法典,获取物体财产,ID:

$jj_post_id = $jj_post[0]; 
$jj_ID = $jj_post_id->ID;

因此,情况是否缩短了,这是我知道的唯一事情,我认为该法典太长了?

最佳回答

如果你肯定会有j美元——员额总是会是一个阵列,而且总是会有一个 st。 等值班:

$jj_ID = $jj_post[0]->ID;

但有时情况并非如此。 你可能并不总是知道变数的内容,因此,你需要做一些检查,以核实你出入安全、有保障的地区。

如果该法典能很好地开展工作,那么它就是一个问题。

我认为,你有两个选择:

 $jj_ID = @$jj_post[0]->ID;

这确保了时间错误得到无声处理,而不是投向标准产出。

另一种办法是绝对检查每种类型的存在:

$jj_ID = "";
if(is_array($jj_post))
{
  $jj_post_id = $jj_post[0]; 
  if(!empty($jj_post_id))
  {
    $jj_ID = $jj_post_id->ID;
  }
}
问题回答
$jj_ID = $jj_post[0]->ID;




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签