0

I want to change some values in my multidimension array in PHP.

Suppose that I have:

$photographer[0]['uid'] = '1001';
$photographer[0]['point'] = '0';  
$photographer[1]['uid'] = '1002';
$photographer[1]['point'] = '1';

I want to change point of photographer that have uid = '1001' to 3. How can I do it?

4 Answers 4

3

Rather than the design you have at present, with the array of photographers 0 indexed, why not have the points indexed by uid?

$photographer[1001]['point'] = '0';
$photographer[1002]['point'] = '1';
Sign up to request clarification or add additional context in comments.

Comments

2

Only by looping through each member of the array:

for ($i = 0; $i <= count($photographer); $i++)
 {

   if ($photographer[$i]['uid'] == "1001")
     $photographer[$i]['point'] = 3;
 }

I don't know your situation but maybe it might make sense to use the uid as array key, then you could do this:

$photographer[1001]["point"] = 3;

Comments

0
for ($i = 0; i < count($photographer); ++i) {
  if ($photographer[$i]['uid'] == '1001') {
    $photographer[$i]['point'] = 3;
    break;
  }
}

Comments

-1

If this content is being loaded from a database into the multidimensional array, and the uid is the primary key, your best bet is probably to do as Pekka suggested and set the array key as the uid. That will allow you to identify individual photographers via your table.

1 Comment

This answer doesn't add anything to Pekka's.

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.