我有下面的数组结果,任务是在某些条件下更改特定的键值。
当前数组结果:
Array
(
[0] => Array
(
[username] => kirit
[property_type] => Apartment buildings
[property_for] => for sale
[address1] =>
[address2] =>
[city] => Jerusalem
[state] =>
[country_id] =>
[rooms] => 3
[floor] => 1
[square_area] => 123
[price] => 12300
[elevator] => 0
[air_condition] => 1
)
[1] => Array
(
[username] => kirit
[property_type] => Apartment buildings
[property_for] => for rent
[address1] =>
[address2] =>
[city] => Jerusalem
[state] =>
[country_id] =>
[rooms] => 3
[floor] => 2
[square_area] => 101
[price] => 10100
[elevator] => 1
[air_condition] => 0
)
[3] => Array
(
[username] => kirit
[property_type] => Apartment buildings
[property_for] => for sale
[address1] =>
[address2] =>
[city] => Jerusalem
[state] =>
[country_id] =>
[rooms] => 2
[floor] => 1
[square_area] => 101
[price] => 10100
[elevator] => 1
[air_condition] => 1
)
)
我想转换这样的数组值,条件是:
Key Value Key Value
=====================================================================================
1. if [property_for] => for sale change to [property_for] => Sale
2. if [property_for] => for rent change to [property_for] => Rent
3. if [elevator] => 0 change to [elevator] => No
---OR---
if [elevator] => 1 change to [elevator] => Yes
4. if [air_condition] => 1 change to [air_condition] => Yes
---OR---
if [air_condition] => 0 change to [air_condition] => No
我想在上述条件下更改当前阵列:
Array
(
[0] => Array
(
[username] => kirit
[property_type] => Apartment buildings
[property_for] => Sale
[address1] =>
[address2] =>
[city] => Jerusalem
[state] =>
[country_id] =>
[rooms] => 3
[floor] => 1
[square_area] => 123
[price] => 12300
[elevator] => No
[air_condition] => Yes
)
[1] => Array
(
[username] => kirit
[property_type] => Apartment buildings
[property_for] => Rent
[address1] =>
[address2] =>
[city] => Jerusalem
[state] =>
[country_id] =>
[rooms] => 3
[floor] => 2
[square_area] => 101
[price] => 10100
[elevator] => Yes
[air_condition] => No
)
[3] => Array
(
[username] => kirit
[property_type] => Apartment buildings
[property_for] => Rent
[address1] =>
[address2] =>
[city] => Jerusalem
[state] =>
[country_id] =>
[rooms] => 2
[floor] => 1
[square_area] => 101
[price] => 10100
[elevator] => Yes
[air_condition] => Yes
)
)
我的SQL查询:
SELECT user_detail.username, property_type.property_type, property_listing.property_for, property_listing.address1, property_listing.address2, property_listing.city, property_listing.state, property_listing.country_id, property_listing.rooms, property_listing.floor, property_listing.square_area, property_listing.price, property_listing.elevator, property_listing.air_condition WHERE property_listing.user_id = 60 ORDER BY property_listing.property_id DESC
如果你能指导我怎么做,我真的很感激。
谢谢。
使用MySQL
SELECT user_detail.username,
property_type.property_type,
IF(property_listing.property_for = 'for sale', 'Sale', IF(property_listing.property_for = 'for rent', 'Rent', property_listing.property_for)) as property_listing.property_for,
property_listing.address1,
property_listing.address2,
property_listing.city,
property_listing.state,
property_listing.country_id,
property_listing.rooms,
property_listing.floor,
property_listing.square_area,
property_listing.price,
IF(property_listing.elevator = 0, 'No', IF(property_listing.elevator = 1, 'Yes', property_listing.elevator)) as property_listing.elevator,
IF(property_listing.air_condition = 0, 'No', IF(property_listing.air_condition = 1, 'Yes', property_listing.air_condition)) as property_listing.air_condition
WHERE property_listing.user_id = 60
ORDER BY property_listing.property_id DESC
使用PHP
- 循环遍历数组(通过引用,这样我们就可以更改值)
- 根据条件更改值
下面是一个带有输出的演示脚本:https://eval.in/207032
foreach($array as &$entry) {
if( $entry['property_for'] == 'for rent' ) {
$entry['property_for'] = 'Rent';
}
if( $entry['property_for'] == 'for sale' ) {
$entry['property_for'] = 'Sale';
}
if( $entry['elevator'] == 1 ) {
$entry['elevator'] = 'Yes';
}
if( $entry['elevator'] == 0) {
$entry['elevator'] = 'No';
}
if( $entry['air_condition'] == 0 ) {
$entry['air_condition'] = 'No';
}
if( $entry['air_condition'] == 1 ) {
$entry['air_condition'] = 'Yes';
}
}
只需使用foreach循环并检查每个条件,替换相应的值。
我已经使用了你的数据,并做到了这一点,并输出下面的两个数组
<?php
$raw_array = [
[
'username' => 'kirit',
'property_type' => 'Apartment buildings',
'property_for' => 'for sale',
'address1' => '',
'address2' => '',
'city' => 'Jerusalem',
'state' => '',
'country_id' =>'',
'rooms' => 3,
'floor' => 1,
'square_area' => 123,
'price' => 12300,
'elevator' => 0,
'air_condition' => 1,
],
[
'username' => 'kirit',
'property_type' => 'Apartment buildings',
'property_for' => 'for rent',
'address1' => '',
'address2' => '',
'city' => 'Jerusalem',
'state' => '',
'country_id' =>'',
'rooms' => 3,
'floor' => 2,
'square_area' => 101,
'price' => 10100,
'elevator' => 1,
'air_condition' => 0,
],
[
'username' => 'kirit',
'property_type' => 'Apartment buildings',
'property_for' => 'for sale',
'address1' => '',
'address2' => '',
'city' => 'Jerusalem',
'state' => '',
'country_id' =>'',
'rooms' => 2,
'floor' => 1,
'square_area' => 101,
'price' => 10100,
'elevator' => 1,
'air_condition' => 1,
]
];
/**new_array**/
$new_array = [];
/**
* Go through all the data
**/
foreach($raw_array as $array){
/**
* Condition 1
* check the property_for and change appropriately
**/
if($array['property_for'] === 'for sale'){
$array['property_for'] = 'Sale';
}elseif($array['property_for'] === 'for rent'){
$array['property_for'] = 'Rent';
}
/**
* Condition 2
* check the elevator and change appropriately
**/
if($array['elevator'] === 0){
$array['elevator'] = 'No';
}elseif($array['elevator'] === 1){
$array['elevator'] = 'Yes';
}
/**
* Condition 3
* check the elevator and change appropriately
**/
if($array['air_condition'] === 0){
$array['air_condition'] = 'No';
}elseif($array['air_condition'] === 1){
$array['air_condition'] = 'Yes';
}
/** add the refined array to the new_array**/
$new_array[] = $array;
}
echo '<pre>';
print_r($raw_array);
print_r($new_array);
echo '</pre>';
通过SQL 解决您的问题
SELECT
user_detail.username,
property_type.property_type,
CASE WHEN property_listing.property_for = 'for sale' THEN 'Sale' WHEN property_listing.property_for = 'for rent' THEN 'Rent' ELSE property_listing.property_for END AS property_for,
property_listing.property_for,
property_listing.address1,
property_listing.address2,
property_listing.city,
property_listing.state,
property_listing.country_id,
property_listing.rooms,
property_listing.floor,
property_listing.square_area,
property_listing.price,
IF(property_listing.elevator=1,'Yes','No') AS elevator,
IF(property_listing.air_condition=1,'Yes','No') AS air_condition
WHERE property_listing.user_id = 60
ORDER BY property_listing.property_id DESC
但它是开箱即用的。。。如果出现错误,请发布。。。