开发者

how to handle associative array in php

开发者 https://www.devze.com 2023-03-31 13:08 出处:网络
I have written a code in php that deals with php and mysql associative array. I have wirtten a query in SQL as

I have written a code in php that deals with php and mysql associative array. I have wirtten a query in SQL as $sql=mysql_query("select x,y from table_name"); Extracted value in associative array as

  while($row=mysql_fetch_assoc($sql))
       { 
        foreach ($row as $value1=>$value) {
                       ...........
                   //$a[value1]=convert($row{'y'});  //this is wrong as i am always getting {"x":"value return from function after passing $row{'y'}","y":"value retur开发者_运维百科n from function after passing $row{'y'}".....ans so on} i.e same value in both the key.
                  }
       }

What my problem is I want to use some function on one of the value from associative array as convert($row{'y'}) shown above and after the value is return from the function again i want that in associative array as

{"x":"value1","y":"value return from function after passing $row{'y'}".....ans so on} again.

How do I achieve this? Thanks in advance.


  while($row=mysql_fetch_assoc($sql))
  {
        $row['y'] = convert($row['y']); 
  }


$result = array();
while($row = mysql_fetch_assoc($sql)) {
    if($row == 'someCondition') {
        $result = someFunction($row[]);
    } else {
        $result =  $row[];
    }
}

now $result will hold all the returned value as an associative array. you can go ahead and use nested if. if you want to add some more condition.


If I am interpreting your question right you want something like this:

while($row = mysql_fetch_assoc($sql) {
    $a[] = array(
        'x' => $row['x'],
        'y' => convert($row['y'])
    );
}

Or, if you need the data to look up the y value based on x:

$a[$row['x']] = convert($row['y']);

0

精彩评论

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