Last active
February 3, 2019 10:40
-
-
Save damiencarbery/b2d250c80ff01cb01fb243e513cbd60b to your computer and use it in GitHub Desktop.
Inject new array into an array at a specific position - Everything in Its Right Place: https://www.damiencarbery.com/2019/02/inject-new-array-into-an-array-at-a-specific-position/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
array ( | |
'col1' => | |
array ( | |
'Price:' => '_listing_price', | |
'Min Price:' => '_listing_min_price', // Inserted element | |
'Max Price:' => '_listing_max_price', // Inserted element | |
'Address:' => '_listing_address', | |
'City:' => '_listing_city', | |
'State:' => '_listing_state', | |
'ZIP:' => '_listing_zip', | |
), | |
'col2' => | |
array ( | |
'MLS #:' => '_listing_mls', | |
'Square Metres:' => '_listing_sqft', | |
'Bedrooms:' => '_listing_bedrooms', | |
'Bathrooms:' => '_listing_bathrooms', | |
'Basement:' => '_listing_basement', | |
), | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Change AgentPress Listings fields | |
Plugin URI: https://www.damiencarbery.com/2019/02/inject-new-array-into-an-array-at-a-specific-position/ | |
Description: Change 'Square Feet' to 'Square Metres' AgentPress Listings fields. | |
Author: Damien Carbery | |
Version: 0.1 | |
*/ | |
add_filter( 'agentpress_property_details', 'dcwd_change_agentpress_property_details', 20 ); | |
function dcwd_change_agentpress_property_details( $details ) { | |
unset( $details['col2']['Square Feet:'] ); | |
$details['col2']['Square Metres:'] = '_listing_sqft'; | |
return $details; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$details = array( | |
'col1' => array( | |
'Price:' => '_listing_price', | |
'Address:' => '_listing_address', | |
'City:' => '_listing_city', | |
'State:' => '_listing_state', | |
'ZIP:' => '_listing_zip' | |
), | |
'col2' => array( | |
'MLS #:' => '_listing_mls', | |
'Square Feet:' => '_listing_sqft', | |
'Bedrooms:' => '_listing_bedrooms', | |
'Bathrooms:' => '_listing_bathrooms', | |
'Basement:' => '_listing_basement' | |
) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// https://www.damiencarbery.com/2019/02/inject-new-array-into-an-array-at-a-specific-position/ | |
function insert_array( $array, $after_key, $insert_array ) { | |
// Return immediately if $after_key isn't in the array. | |
if( ! array_key_exists( $after_key, $array ) ) { | |
return $array; | |
} | |
$new_array = array(); | |
foreach ( $array as $k => $v ) { | |
// Copy key and value to the new array. | |
$new_array[ $k ] = $v; | |
// If we find $after_key | |
if ( $after_key == $k ) { | |
// then add each key/value of $insert_array | |
foreach ( $insert_array as $nk => $nv ) { | |
$new_array[ $nk ] = $nv; | |
} | |
} | |
} | |
return $new_array; | |
} | |
// Use it to insert 'Min Price:' and 'Max Price:' directly after 'Price:' | |
$ins_array = array( 'Min Price:' => '_listing_min_price', 'Max Price:' => '_listing_max_price' ); | |
$details[ 'col1' ] = insert_array( $details[ 'col1' ], 'Price:', $ins_array ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// https://www.damiencarbery.com/2019/02/inject-new-array-into-an-array-at-a-specific-position/ | |
// From: https://stackoverflow.com/a/21299719/8605943 | |
function change_key( $array, $old_key, $new_key ) { | |
if( ! array_key_exists( $old_key, $array ) ) { | |
return $array; | |
} | |
$keys = array_keys( $array ); | |
$keys[ array_search( $old_key, $keys ) ] = $new_key; | |
return array_combine( $keys, $array ); | |
} | |
// Use this function to change 'Square Feet:' to 'Square Metres:' | |
$details['col2'] = change_key( $details['col2'], 'Square Feet:', 'Square Metres:'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment