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';
}
Related
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 -
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;
}
}
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);
}
}
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
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 7 years ago.
Improve this question
From a mySQL row I get the below from column coop as appeared (with commas)
1,2,6,27
My question is how can I have something like
for
as numbers as the column
do the loop {
{
Assuming you have the values stored in a string, let's call it $dbValue, you can split that string into an array:
$values = explode(",", $dbValue);
Then just loop over that array:
foreach ($values as $value) {
// do something with each value
}
As an aside... Storing delimited values in a single database field is very often a bad idea. Each database field should contain one discrete value. If multiple values are needed, you'd create a separate table with a many-to-one relationship.
seems foreach
$tmp = explode(',', $yourvalue) // $yourvalue = '1,2,6,27'
foreach ( $tmp as $key => $value ) {
echo $value;
}