I have an array, and its large. very large. Its for a real estate company. I would like to split the array into sections based on the number of bedrooms each home has. So ideally all the entries (like the one below) that had 0 bedrooms would be set into one array, and with 1, into another. etc...
Is this possible?
object(SimpleXMLElement)#124 (1) {
["unit"]=>
array(40) {
[0]=>
object(SimpleXMLElement)#246 (1) {
["#attributes"]=> array(28) {
["mlsnumber"]=> string(8) "A1837040"
["type"]=> string(0) ""
["unit"]=> string(3) "607"
["maintenancefee"]=> string(4) "1609"
["price"]=> string(6) "890000"
["salesprice"]=> string(0) ""
["bath"]=> string(1) "1"
["hbath"]=> string(1) "0"
["beds"]=> string(1) "0"
["sqft"]=> string(3) "747"
["sqftm"]=> string(5) "69.40"
["pricesqft"]=> string(8) "1,191.43"
["lat"]=> string(16) "25.7683201213024"
["lng"]=> string(16) "-80.132474899292"
["pricemeters"]=> string(9) "12,824.21"
["pricechange"]=> string(5) "-6.32"
["dom"]=> string(3) "181"
["reo"]=> string(1) "N"
["shortsale"]=> string(1) "N"
["dropprice"]=> string(0) ""
["pets"]=> string(3) "Yes"
["furnished"]=> string(1) "U"
["FloorLevel"]=> string(1) "6"
}
}
Loop through the array and push the current array into a new array whereas the index of the new array is the number of bedrooms of the current array. You'll need to convert your object into an array first, then something along the lines of this:
foreach($all_units as $unit){
if(!isset($new[$unit['beds']])){
$new[$unit['beds']] = array();
}
array_push($new[$unit['beds']],$unit);
}
print_r($new);
Suppose $arr holds your result, Try
$result = array();
foreach($arr['unit'] as $prop){
$result[$prop["beds"]][] = $prop;
}
Related
I have session with multidimensions array and this session is update by every form send.
I would like to check if the product_id already exists in my multidimensional array, so as not to add it several times, but only to increase the number of pieces. I create something like a basket. So when somebody add the same product to cart in my session have to check, that this product exists and if exists update just quantity field in array.
I do everything in ajax
array(2) {
[0]=>
array(15) {
["quantity"]=> string(1) "1"
["final_price"]=> string(3) "0.5"
["do_wash"]=> string(2) "on"
["final_wash_price"]=> string(3) "0.2"
["do_bringing"]=> string(2) "on"
["final_bringing_price"]=> string(3) "0.4"
["deposit_price"]=> string(1) "5"
["product_name"]=> string(24) "Talerz płytki Ø 160 mm"
["product_id"]=> string(2) "12"
["product_price"]=> string(3) "0.5"
["product_person_to_bringing"]=> string(1) "1"
["product_wash_price"]=> string(3) "0.2"
["product_bringing_price"]=> string(3) "0.4"
["product_calculate_bringing"]=> string(0) ""
["action"]=> string(9) "send_form"
}
[1]=>
array(15) {
["quantity"]=> string(1) "1"
["final_price"]=> string(3) "0.5"
["do_wash"]=> string(2) "on"
["final_wash_price"]=> string(3) "0.2"
["do_bringing"]=> string(2) "on"
["final_bringing_price"]=> string(3) "0.4"
["deposit_price"]=> string(1) "5"
["product_name"]=> string(24) "Talerz płytki Ø 160 mm"
["product_id"]=> string(2) "12"
["product_price"]=> string(3) "0.5"
["product_person_to_bringing"]=> string(1) "1"
["product_wash_price"]=> string(3) "0.2"
["product_bringing_price"]=> string(3) "0.4"
["product_calculate_bringing"]=> string(0) ""
["action"]=> string(9) "send_form"
}
}
if (isset($_POST['action'])) {
if (!isset($_SESSION['products'])) {
$_SESSION['products'] = [];
}
if (!isset($_SESSION['product'])) {
$_SESSION['product'] = [];
}
$product_object = $_POST;
$_SESSION['product'] = [];
$_SESSION['product'][] = $product_object;
foreach ($_SESSION['product'] as $single_product_array) {
$_SESSION['products'][] = $single_product_array;
}
echo '<pre style="color:#fff;">';
var_dump( $_SESSION['products']);
echo '</pre>';
}
I try to use functions from: PHP multidimensional array search by value and from php.net
$key = array_search(40489, array_column($userdb, 'uid')); but not working for me
Hello i have an array_dif function between 2 arrays and the result it's not as it should.I don't understand why it does not return the status as difference. First array is data second is row and the third is the result. In the result it should also be status because the value is different.
$result = array_diff($data,$row );
array(9) {
["scooter_id"]=>
string(6) "RO0001"
["battery_lvl"]=>
string(2) "80"
["lat"]=>
string(9) "44.312150"
["lng"]=>
string(9) "23.872900"
["alt"]=>
string(1) "0"
["speed"]=>
string(1) "0"
["status"]=>
string(1) "2"
["ip"]=>
string(14) "213.233.101.62"
["port"]=>
int(24600)
}
array(11) {
["battery_lvl"]=>
string(2) "80"
["nr_satelites"]=>
string(1) "1"
["lat"]=>
string(9) "44.312154"
["longi"]=>
string(9) "23.873007"
["alt"]=>
string(1) "0"
["speed"]=>
string(1) "0"
["status"]=>
string(1) "1"
["location"]=>
string(7) "romania"
["ip"]=>
string(14) "213.233.101.62"
["port"]=>
string(5) "24600"
["status_intermediar"]=>
string(1) "2"
}
array(3) {
["scooter_id"]=>
string(6) "RO0001"
["lat"]=>
string(9) "44.312150"
["lng"]=>
string(9) "23.872900"
}
array_diff checks only the values.
Because your 2nd array contains ["status_intermediar"]=> string(1) "2" it finds the value so it doesn't see it as a difference
If you want to check both keys and values you should use array_diff_assoc
Also if you want to find all the different values from BOTH arrays you should run it twice
$difference1=array_diff_assoc($array1,$array2);
$difference2=array_diff_assoc($array2,$array1);
array_dif is one way function ("Compares array1 against one or more other arrays and returns the values in array1 that are not present in any of the other arrays."- https://www.php.net/manual/en/function.array-diff.php).
If you want all diffs, you have to call it twice: array_dif($first, $second) and array_dif($second, $one) and optionally merge results.
$array_difference1 = array_merge(array_diff($array1, $array2),
array_diff($array2, $array1));
$array_differnce = array_merge(array_diff($array_difference1, $array3),
array_diff($array3, $array_difference1));
I'm trying to combine two array in PHP with array_combine() function, but sometimes it working fine and sometimes it's not. I can't understand why it's working like this!
My Code:
var_dump($selectedDuretion);
var_dump($selectedDuretionType);
$combination = array_combine($selectedDuretion, $selectedDuretionType);
return $combination;
Expected OUTPUT:
array(4)
{
[0]=> string(1) "3"
[1]=> string(2) "12"
[2]=> string(1) "4"
[3]=> string(1) "3"
}
array(4)
{
[0]=> string(4) "days"
[1]=> string(4) "days"
[2]=> string(5) "weeks"
[3]=> string(5) "weeks"
}
{"3":"days","12":"days","3":"weeks","4":"weeks"}
Actual OUTPUT :
array(4)
{
[0]=> string(1) "3"
[1]=> string(2) "12"
[2]=> string(1) "4"
[3]=> string(1) "3"
}
array(4)
{
[0]=> string(4) "days"
[1]=> string(4) "days"
[2]=> string(5) "weeks"
[3]=> string(5) "weeks"
}
{"3":"weeks","12":"days","4":"weeks"}
The combination of arrays it shocking, I'll be thankful if anyone tell me why is this happening and how to solve it.
PHP Does not allow you to have duplicate indices in an array while JSON does allow you to have that for whatever reasons.
Since you are trying to convert PHP arrays to JSON your duplicate key gets eliminated. Hence you will have to manually build the JSON string.
$json="";
for($i=0;$i<count($selectedDuration);$i++)
{
$json.='"'.$selectedDuration[$i].'":"'.$selectedDurationType[$i].'",';
}
$json=rtrim($json,",");
$json="{".$json."}";
echo $json;
Output
{"3":"days","12":"days","4":"weeks","3":"weeks"}
Fiddle
COMPANY ARRAY
array(1) {
[0]=> array(19) {
["entityid"]=> string(4) "3626"
["entityparentid"]=> string(1) "0"
["entityduplicateof"]=> string(1) "0"
["entitytype"]=> string(1) "0"
["entityname"]=> string(12) "Facebook Inc"
}
}
DISTANCE ARRAY
array(1) {
["distance"]=> string(4) "1.22"
}
What I'd like the output to look like:
array(1) {
[0]=> array(19) {
["entityid"]=> string(4) "3626"
["entityparentid"]=> string(1) "0"
["entityduplicateof"]=> string(1) "0"
["entitytype"]=> string(1) "0"
["entityname"]=> string(12) "Facebook Inc"
["distance"]=> string(4) "1.22" // here
}
}
Question:
array_push($company_array,$distance_array); seems to not do what I want it do.
It adds it to the end, but not where i want it to (notice the difference in where it is placed):
array(1) {
[0]=> array(19) {
["entityid"]=> string(4) "3626"
["entityparentid"]=> string(1) "0"
["entityduplicateof"]=> string(1) "0"
["entitytype"]=> string(1) "0"
["entityname"]=> string(12) "Facebook Inc"
},
["distance"]=> string(4) "1.22" // not here
}
It has another level inside $company, if you want the single array inside that another nesting, point it to index zero directly, and use array_merge:
$company[0] = array_merge($company[0], $distance);
Sample Output
Another way to merge the two arrays is the + operator:
$company[0] = $company[0] + $distance;
A detailed explanation of the difference between array_merge and the + can be found here.
I am having a hard time extracting a value from the following JSON object
array(3) { [0]=> object(stdClass)#1 (11) { ["Group"]=> string(2) "18" ["GroupName"]=> string(8) "Wireline" ["Region"]=> string(2) "15" ["RegionName"]=> string(8) "Atlantic" ["Province"]=> string(1) "1" ["ProvinceName"]=> string(13) "New Brunswick" ["City"]=> string(2) "11" ["CityName"]=> string(10) "Campbelton" ["Site"]=> string(2) "37" ["SiteName"]=> string(16) "Campbellton PNCT" ["Year"]=> string(4) "2016" }
[1]=> object(stdClass)#2 (5) { ["PlatformID"]=> string(1) "1" ["PlatformTag"]=> string(6) "Access" ["Rack"]=> string(24) "23" Width 36" Depth Rack" ["RackValue"]=> string(1) "2" ["Comments"]=> string(0) "" }
[2]=> object(stdClass)#3 (12) { ["Rack"]=> string(31) "23" Width 36" Depth Rack Access" ["RackValue"]=> string(1) "2" ["RackComments"]=> string(0) "" ["Manufacturer"]=> string(6) "werwer" ["Name"]=> string(6) "werwer" ["Quantity"]=> string(1) "1" ["RackUnits"]=> string(1) "1" ["Power"]=> string(1) "1" ["ActivePassive"]=> string(6) "Active" ["ACDC"]=> string(2) "AC" ["ConnectivityIDs"]=> array(1) { [0]=> string(1) "2" } ["Connectivity"]=> array(1) { [0]=> string(5) "Fiber" } } }
I am trying to extract each item in a foreach loop within PHP Above is the var_dump of the $data[0] JSON object it demonstrates what the Array item looks like.
My foreach is the following
$data = json_decode($_POST["submitdata"]);
$Forecasts = $data[0];
foreach($Forecasts as $Forecast){
echo($Forecast->PlatformID);}
but it returns nothing as a result. Can someone explain to me how to get this from the second Array in the object?
simply place the Index of the inner array along with the object as shown below. This will check for the sub item for the PlatformID and return it to the screen.
foreach($Forecasts as $Forecast){
echo($Forecast[1]->PlatformID);}