使用php访问数组元素中字段的正确方法是什么?

问题描述:

I'm getting a few rows from the DB with an ajax call in PHP and I save the result to an array with javascript.
Then I make some changes in the data and wish to update the DB.
So I use another ajax call for that but I can't manage to access the fields inside the rows correctly.
When I try this I get nothing:

echo $bArray[$i].branchId;

When I try this:

echo json_encode($bArray[$i].branchId);

I get ArraybranchId instead of the field value.

What's the correct way to access the field with php?

我从数据库中获取了几行,并在PHP中调用了ajax,并将结果保存到数组中 使用javascript。
然后我对数据进行了一些更改,并希望更新数据库。
我为此使用了另一个ajax调用,但我无法正确访问行内的字段。 br> 当我尝试这个时我什么都没得到: p>

  echo $ bArray [$ i] .branchId; 
  code>  pre> 
 
 当我尝试这个时: p> 
 
 
  echo json_encode($ bArray [$ i] .branchId); 
  code>  pre> 
 
 

我得到 ArraybranchId code>而不是字段值。 p>

使用php访问该字段的正确方法是什么? p> div>

Try either for array:

$bArray[$i]['branchId']

or for object:

$bArray[$i]->branchId

depending which type $bArray[$i] is (array or object). You have not written in your question, so I showed both ways.

I take it branchId is the name of the field, and you want the value for that field?

If so, it's:

echo $bArray['branchId']; or echo $bArray[$i]['branchId']

Edit: Also, you'll need to make sure you're using mysql_fetch_assoc not mysql_fetch_array!