PHP auto compute multidimensional arrays against one another [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a multidimensional array.
$parent_array($child1('lat','long'),$child2('lat','long')...)
The amount of child arrays can be of any value per request starting from 2.
The parent array is then called into a function that has a google distance calculator between to or more points.
function distance($parent_array)
{
foreach ( $parent_array as $child){
$distance = file_get_contents('https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins='.$child1['lat'].','.$child1['long'].'&destinations='.$child2['lat'].','.$child2['long'].'&key=********************************');
$trim = trim($this->objectToArray($distance)['rows'][0]['elements'][0]['distance']['text'],' km');
}
return $trim;
}
I need to get this function to calculate the distance between all posted child arrays as illustrated on the above function using
$child1 and $child2

As I understand you, what you need is a cartesian product. This is as simple as 2 foreach cycles:
foreach ($points as $point1) {
foreach ($points as $point2) {
computeDistance($point1, $point2);
}
}

Related

PHP multidimensional array loop through second dimension [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
how do i loop this kind of array?
$arr = array (
"aa"=>array("apple","orange"),
"bb"=>array("373","22"),
"cc"=>array("t0","h0"),
"dd"=>array("1","0")
);
I want to loop through the column of each item.
e.g.: i want to display ('apple','373','t0','1') at the first loop and ('orange','22','h0','0') at the last loop. thanks
We are assuming that all the arrays inside the main array are the same size in this example.
$arr = array (
"aa"=>array("apple","orange"),
"bb"=>array("373","22"),
"cc"=>array("t0","h0"),
"dd"=>array("1","0")
);
for($i = 0; $i<sizeof($arr["aa"]); $i++)
{
foreach($arr as $key=>$item)
{
echo($item[$i]);
}
echo ' - ';
}
Output: (obviously you can do any necessary formatting you need such as new lines or commas)
apple373t01 - orange22h00 -

MySQL PHP - Returned column and split it into 2 arrays [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm using PHP and MySQL to pull some rows from a table. One column is called "item_notes" and this column has a dynamic number of comma-delimited values in it. If I printed 1 row, the column would look something like this:
item_1, new_item_1, ** note_1, ** note_2, item_2, ** note_3, old_item_1, new_item2
Is there a way I can split this into 2 arrays using PHP, where 1 array is has only values that start with a ** and the ones that don't go into the other array?
As you show no effort trying, I suppose I'll just leave it here)))
$s = 'item_1, new_item_1, ** note_1, ** note_2, item_2, ** note_3, old_item_1, new_item2';
$items = array_map('trim', explode(',', $s));
$notes = $starredNotes = [];
foreach ($items as $item) {
if (0 === strpos($item, '**')) {
$starredNotes[] = $item;
} else {
$notes[] = $item;
}
}

Php combine an array of values into an array with double combinations string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How i can combine ad array of values into an array with double combination string without duplications ?
For example, if i have something like:
array('one','two','tre','four','five');
I want to obtain an array of combinations like this ('one / two') just one time and not also ('two / one').
In this way i wanto to get something similar to:
array('one/two', 'one/tre', 'one/four', 'one/five', 'two/tree','two/four' .......
Suggestions ?
You can do it with this code. It won't show two/one, three/two, etc (that's how I understood it):
<?php
$array = array('one','two','tre','four','five');
$newArray = [];
foreach ($array as $el) {
foreach ($array as $el2) {
if ($el === $el2) continue;
$newArray[] = $el."/".$el2;
}
array_shift($array); // remove the element we just went through
}
print_r($newArray);
Demo

Match Array values and merge if the values matches in PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have two array i want to match a key of first array with another array and if the values of both key matches then add the value of second array to array 1
Array 1
[{"currency":1,"amount":23},{"currency":1,"amount":30},{"currency":2,"amount":40},]
Array 2
[{"currency_id": 1,"currency_symbol":$},{"currency_id":2,"currency_symbol":€}]
Desired output is:
[{"currency":$,"amount":23},{"currency":$,"amount":30},{"currency":€,"amount":40}]
The Code i am using is:
foreach($a1 as $key) {
foreach($a2 as $cKey){
if($a1['currency']==$a2['currency_id']){
$a1['currency_symbol'] = $a2['currency_symbol'];
echo $a1['currency_symbol'];
}
}
}
foreach($arr1 as $k=>$key) {
foreach($arr2 as $cKey){
if($key['currency']==$cKey['currency_id']){
$arr3[$k]['currency'] = $cKey['currency_symbol'];
$arr3[$k]['amount'] = $key['amount'];
}
}
}
var_export($arr3);
You only need to assign variable and print outside loop.

Select variable with the highest value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I need to select the largest value between two variables, but not return the value itself. So using max won't do the trick.
Example:
$value1 = 60;
$value2 = 24;
I need $value1 (the variable name) to be returned. This would be simpler to do, were the variables in an array as I could do a foreach loop to return the key with the highest value, but I don't think there's much point creating an array when there's only every going to be two values, unless it won't effect the efficiency at all (I'm not sure it will).
$biggerValue = null;
if ($value1 > $value2){
$biggerValue = 'value1';
} elseif ($value2 > $value1) {
$biggerValue = 'value2';
}

Categories