1

Here is some of my PHP:

$query = $db->prepare('SELECT * FROM villages WHERE owner = ? AND id = ?');
$array = array($_SESSION['id'], $_SESSION['viewing']);
$query->execute($array);
$r = $query->fetch();
$r['HQlevelPlusOne'] = $r['HQlevel'] + 1;
$logsNeeded = $headquartersRequired[$r['HQlevelPlusOne'][0]];

the $logsNeeded doesn't seem to be working, here is the $headquartersRequired array:

$headquartersRequired = array(array(50,     40,     55,    30),
                              array(90,     80,     100,   80));

how for example would I call the value '50' ? Let's say in my current code that $r['HQlevelPlusOne'] is 1. so the value $logsNeeded should be is 90.. but it doesn't seem to be working.. Does anyone know why?

3
  • plus one cuz you are using mysqli even though you're not binding the params :) Commented Nov 12, 2013 at 21:17
  • @Prashank I'm using PDO and I am binding the parameters.. Well, I'm preparing the statements, which is still the same, I think Commented Nov 12, 2013 at 21:27
  • oops sorry i misunderstood. Commented Nov 12, 2013 at 21:28

2 Answers 2

3

By numerical index, the value '50' is at index 0 of a nested array which is itself at index 0. So to retrieve it:

echo $headquartersRequired[0][0];

$headquartersRequired[0] points to the first index of array $headquartersRequired. That value is itself an array, whose elements can be accessed in the same way, so from there $headquartersRequired[0][0] points to the first element of the first array within $headquartersRequired.

If $r['HQlevelPlusOne'] is 1 then $headquartersRequired[$r['HQlevelPlusOne'][0]] is wrong because $r['HQlevelPlusOne'][0] would reference the first element in an array named $r['HQlevelPlusOne']. But $r['HQlevelPlusOne'] is 1 and 1 is not an array.

Sign up to request clarification or add additional context in comments.

2 Comments

ah thanks! I just got a bit mixed up with the syntax, I changed to this: $logsNeeded = $headquartersRequired[$r['HQlevelPlusOne']][0]; and now it works fine :) I was using $r['HQlevelPlusOne'] to reference the index; (I think that's what I mean)
Good, I was afraid I was going too much into details :)
1

The $r is a single-dimensional array, but yet you are referencing the second dimension in the final line of code ($r['HQlevelPlusOne'][0]). That isn't going to work.

The value of 50 is located at the 0th index of the 0th dimension.

It would be in position $headquartersRequired[0][0] = 50.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.