Mr.Happy
Mr.Happy

Reputation: 2647

In PHP, how to change specific array key value with condition

I am having bellow array result and task is to change specific key value with some condition.

Current array result:

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
        )

)


I want to convert array value like this with condition:

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


I want to change current array with above mention conditions:

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
        )

)


My SQL Query:

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

I really thankful if you guide me how to do it.

Thanks.

Upvotes: 0

Views: 409

Answers (3)

donald123
donald123

Reputation: 5749

To solve your Problem by 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 

But is out of the Box ... If errors exsists, please post it ...

Upvotes: 1

ʰᵈˑ
ʰᵈˑ

Reputation: 11365

Using 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

Using PHP

  • Loop through your array (by reference, so we can change the values)
  • Change the values depending on criteria

Here's a demo script with output: 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';
   } 
}

Upvotes: 3

Edward Manda
Edward Manda

Reputation: 579

Just use the foreach loop and check each conditions, replacing the appropriate values.

I have used your data and done that and output the two arrays below

<?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>';

Upvotes: 1

Related Questions