开发者

Questions about manipulation of "mysql_fetch_array" result

开发者 https://www.devze.com 2023-02-04 08:00 出处:网络
By default mysql_fetch_array returns numeric and associative array togeather. This function is usefu开发者_如何学Cl for reading, while you can get values

By default mysql_fetch_array returns numeric and associative array togeather. This function is usefu开发者_如何学Cl for reading, while you can get values

either by statement : echo arr[1]; //circle or echo arr["shape"]; //circle

I am new to PHP and I run into the troubles when I wanted to change the values of that fetched array: arr[1] = square;

I supposed that arr["shape"] will be 'square' too, but I was wrong. arr["shape"] still remains 'circle'. Obviously you have to change both values to 'circle'.

Moreover: I wrote a funcion which should return changed shape array, (which is 2D array). I got different outcomes, if I used the first or

the second line of code.

function ChangeShape($arr)
        {

            for ($i=0; $i<$count($arr); $i++)
            {
         1.    $arr[$i]["shape"] = 'square'; // fail to return changed $arr - it is (probably) reference to number counterpart

         2.    $arr[$i][2] = 'square';       // this WILL return changed $arr
            }
            }
            return $arr;
        }

My questions: 1. is there any technique in PHP by which I can change both values at once? 2. in my app I use assoc nicknames to access array values. But when it is not possible to change numeric alias at the same time, what is

the easiest way to do it?

thank for reading this to the end... hope you answer.

Thanks a lot.


If you just want the data in a simple array once you grab it, you can always just simplify it by putting it in a few array:

 $query = "SELECT * from myTable";
 $result = mysql_query($query) or die(mysql_error());
 $newArray = array();

 $i=0; 
 while($row = mysql_fetch_array($result)){
   array_push(  $newArray, $row[$i]['name'] );
 }
 print_r($newArray);
 $newArray[4] = "some other value than what came from the db";
 print_r($newArray);

It doesn't seem super clear why your data is coming back as complicated as it seems to be, but this might help clear it up.


Do you really need to fetch numeric index? At least based on what you've posted it seems like you just want to call mysql_fetch_assoc to get an array with just the associative version.

The "aliases" in the array returned by mysql_fetch_array aren't really aliases... it just inserts it twice into the result array, once with the numeric index and once with the name of the column. The only way to change them both is to assign them both the desired value.


See this example and maybe it will make sense why its not working. Its not a reference or it would be working. If you had to have this functionality, you could build your array like this:

<?

$a='dog';
$b='cat';

$arr=array ('a' => &$a, 1 => &$a,'b' => &$b,'2' => &$b );

print_r($arr);

$a='horse';

print_r($arr);

$arr[1]='cow';

print_r($arr);

Results:

Array
(
    [a] => dog
    [1] => dog
    [b] => cat
    [2] => cat
)

Array
(
    [a] => horse
    [1] => horse
    [b] => cat
    [2] => cat
)
Array
(
    [a] => cow
    [1] => cow
    [b] => cat
    [2] => cat
)
0

精彩评论

暂无评论...
验证码 换一张
取 消