user3378734
user3378734

Reputation: 226

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

All I want is the end 4 and end 33, how do I get them?

Upvotes: 0

Views: 135

Answers (3)

chrki
chrki

Reputation: 6323

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

Upvotes: 0

James Elliott
James Elliott

Reputation: 1062

$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];

Upvotes: 0

dkasipovic
dkasipovic

Reputation: 6120

You really could have solved that by looking at 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. Use mysqli or pdo. Also, if you insist on using mysql_* function use mysql_fetch_assoc instead of mysql_fetch_array.

Upvotes: 2

Related Questions