tags:

views:

58

answers:

4

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?

+2  A: 

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;
Pekka
A: 
for ($i = 0; i < count($photographer); ++i) {
  if ($photographer[$i]['uid'] == '1001') {
    $photographer[$i]['point'] = 3;
    break;
  }
}
Dominic Rodger
+3  A: 

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';
Yacoby
A: 

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.

DeaconDesperado
This answer doesn't add anything to Pekka's.
Jordan Ryan Moore