В PHP, как изменить конкретное значение ключа массива с условием

У меня есть результат массива ниже, и задача состоит в том, чтобы изменить определенное значение ключа с некоторым условием.

Текущий результат массива:

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

Я очень благодарен, если вы укажете мне, как это сделать.

Благодарю.

0

Решение

Использование 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';
}
}
3

Другие решения

Просто используйте цикл 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>';
1

Решить вашу проблему с помощью 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

Но это из коробки … Если ошибки существуют, пожалуйста, напишите это …

1