简体   繁体   English

如何从php关联数组中检索值

[英]How to retrieve a value from an php associative array

I have an array that shows 我有一个数组显示

Array ( [0] => Array ( [subjects_id] => 4 [0] => 4 ) [1] => Array ( [subjects_id] => 33 [0] => 33 ) ) 数组([0] =>数组([臣_id] => 4 [0] => 4)[1] =>数组([臣_id] => 33 [0] => 33))

All I want is the end 4 and end 33 , how do I get them? 我想要的只是结束4和结束33 ,我如何得到它们?

You really could have solved that by looking at http://php.net/array 你真的可以通过查看http://php.net/array来解决这个问题

In the lack of better requirement explanation in the question itself, I'll provide general answer which might or might not be useful: 由于问题本身缺乏更好的需求解释,我将提供可能会或可能没有用的一般答案:

<?
  echo $array[0]['subjects_id'];
?>

and

<?
  echo $array[1]['subjects_id'];
?>

will do the trick. 会做的。 Also, an observation: 另外,一个观察:

You shouldn't use mysql_ functions in php since they are deprecated. 你不应该在php中使用mysql_ functions,因为它们已被弃用。 Use mysqli or pdo. 使用mysqli或pdo。 Also, if you insist on using mysql_* function use mysql_fetch_assoc instead of mysql_fetch_array . 另外,如果你坚持使用mysql_ *函数,请使用mysql_fetch_assoc而不是mysql_fetch_array

$arr = array(
  array(
    'subjects_id' => 4, '0' => '4'
  ),
  array(
    'subjects_id' => '33', '0' => '33'
  )
);
// get the numbers...
$number = $arr[0][0]; // first one
$number2 = $arr[1][0];
echo my_array[0][0] // 4
echo my_array[1][0] // 33

or 要么

echo my_array[0]['subject_id'] // 4
echo my_array[1]['subject_id'] // 33

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM