concatenate two index values in for each - php

I have a loop like this
$rr=array();
foreach($relations as $key=>$type){
$rr[$relationType->U2U_Related_USR_ID]=$type[$k]->MSTT_Name.' / '.$type[$k+1]->MSTT_Name;
$k++;
}
Am getting only first index value. how to concatenate two index values in for each.

Increment by 2 !
$rr = array();
for ($i = 0, $n = count($type); $i < $n; $i += 2) {
$t1 = $type[$i];
$t2 = $type[$i + 1];
$rr[$relationType->U2U_Related_USR_ID] = $t1->MSTT_Name.' / '.$t2->MSTT_Name;
}
Note: $type's length should be an even number!

you can work with 2 couples key/value inside your loop like this :
foreach($relations as $key=>$type){
list( $odd_key, $odd_value ) = each( $relations );
//... your code here
// This work with a step by 2 elements. If you need step by 1,
// add the following line at the end of the loop :
//prev( $relations )
}

Related

How do I cycle through array and multiply every new number with the next?

This is what I have so far:
<?php
$ask_number = readline("Gimme a number?");
$array = Array();
for ($i = 1; $i <= $ask_number; $i++) {
$array[$i] = $i;
}
return 0;
var_dump($sum);
?>
I can get it to ask for a number and create an array.
What I want it to do;
I want it to do this:
1 * 2 * 3 * 4 * 5 * 6.. etc.
Thank you for taking the time to read this!
You can go for range() and foreach()
<?php
$ask_number = 6;//desired number
$array = range(1,$ask_number);//create array
$multiply = 1; //define multiply variable
foreach( $array as $val ){
$multiply *= $val ; // multiple value
}
var_dump($multiply ); // print value
//in case you want sum
var_dump(array_sum($array));
Output: https://3v4l.org/693sl

Reducing an array

I have an array that goes from '00:00:00' to '23:59:59', something like this ['00:00:00', '00:00:01' ... '23:59:59'].
I need to write a function to reduce the array length into n items but always keeping the first and the last element.
An example:
reduce_into($array, 3) -> ['00:00:00', '12:00:00', '23:59:59']
Note that the array must be "balanced", that means that when I reduce it into 3 elements it will return the first, the one in the middle and the last one.
Here's the code:
function getnewarray($t,$i)
{
$newArr[0] = $t[0];
$a = 1;
$masterdivby = $divby = count($t) / ($i-1);
$ni = $i-2;
while($a <= $ni)
{
$newArr[$a] = $t[$divby];
$divby = $masterdivby + $divby;
$a++;
}
$newArr[$i] = $t[count($t) - 1];
return $newArr;
}
?>

Given an array of integers, what's the most efficient way to get the number of other integers in the array within n?

Given the following array:
$arr = array(0,0,1,2,2,5,6,7,7,9,10,10);
And assuming $n = 2, what is the most efficient way to get a count of each value in the array within $n of each value?
For example, 6 has 3 other values within $n: 5,7,7.
Ultimately I'd like a corresponding array with simply the counts within $n, like so:
// 0,0,1,2,2,5,6,7,7,9,10,10 // $arr, so you can see it lined up
$count_arr = array(4,4,4,4,4,3,3,4,4,4, 2, 2);
Is a simple foreach loop the way to go? CodePad Link
$arr = array(0,0,1,2,2,5,6,7,7,9,10,10);
$n = 2;
$count_arr = array();
foreach ($arr as $v) {
$range = range(($v-$n),($v+$n)); // simple range between lower and upper bound
$count = count(array_intersect($arr,$range)); // count intersect array
$count_arr[] = $count-1; // subtract 1 so you don't count itself
}
print_r($arr);
print_r($count_arr);
My last answer was written without fully groking the problem...
Try sorting the array, before processing it, and leverage that when you run through it. This has a better runtime complexity.
$arr = array(0,0,1,2,2,5,6,7,7,9,10,10);
asort($arr);
$n = 2;
$cnt = count($arr);
$counts = array_pad(array(), $cnt, 0);
for ($x=0; $x<$cnt; $x++) {
$low = $x - 1;
$lower_range_bound = $arr[$x]-$n;
while($low >= 0 && ($arr[$low] >= $lower_range_bound)) {
$counts[$x]++;
$low--;
}
$high = $x + 1;
$upper_range_bound = $arr[$x]+$n;
while($high < $cnt && $arr[$high] <= $upper_range_bound) {
$counts[$x]++;
$high++;
}
}
print_r($arr);
print_r($counts);
Play with it here: http://codepad.org/JXlZNCxW

PHP remove undefined offset:

I would like to ask what is this kind of error undefined offset:4
my code is
$url = 'http://gogo.com, http://yoyo.com, http://gogo.com, http://yoyo.com, http://gogo.com, http://yoyo.com';
$key = 'key1, key2, key3';
$xurl = explode( "\n", $url );
$xkey = explode( "\n", $key );
$count = count( $xkey );
echo $count;
$i = 0;
while ( $i <= $count ) {
if(empty($xkey[$i])){
unset($xkey[$i]);
}
echo $xkey[$i];
$i++;
}
the echo is key1 key2 key3
but the thing is that i need to loop the xkey equal to my url
so the echo should be but i only have 3keyword i mean the keyword is less than the url.
how can i make it something like this below...
http://gogo.com - key1
http://yoyo.com - key2
http://gogo.com - key3
http://yoyo.com - key1
http://gogo.com - key2
http://yoyo.com - key3
What it means is that the script is looking for the value of $xkey[4], but that element doesn't exist. This is happening because array keys like this are 0-based, so the fourth element will be $xkey[3]. Change your while statement to while ( $i < $count ) as count will be 4, but the max key will be 3.
You're doing
while ( $i <= $count ) {
where $count is the number of element in $xkey (let say 4 elements)
As arrays are 0 indexed, the element $xkey[3] is the 4th and last element.
$xkey[4] will bring you that error.
Now, delete "=" in this while ( $i <= $count ) { and it should disappear.
I'm not sure where to start explaining what your problem is, you have an entirely wrong approach. To get the result you want you need to do this:
$urlString = 'http://gogo.com, http://yoyo.com, http://gogo.com, http://yoyo.com, http://gogo.com, http://yoyo.com';
$keyString = 'key1, key2, key3';
$urls = explode(',', $urlString);
$keys = explode(',', $keyString);
$i = 0;
$count = count($keys);
foreach ($urls as $url) {
echo $url, ' - ', $keys[$i % $count], PHP_EOL;
$i++;
}

Convert Python for -loop to PHP

How can you convert the following code to PHP?
summat = [sum(arra[i:i+4]) for i in range(0,len(arra),4)]
My attempt
$summat = array()
foreach ( range(0, $arra.length, 4) as $i) {
$summat = array ( array_sum( array_slice( $array, $i, $i+5) ) ) // don't know how to append the sums the array
$sum = array();
foreach(range(0, count($a), 4) as $i)
$sum []= array_sum(array_slice($a, $i, 4));
"[]=" is an append-to-array operator
slice's second parameter is slice length, not the last index
or even simpler
$sum = array_map('array_sum', array_chunk($a, 4));
To append a value to an array, use:
$summat[] = array_sum(...);
The PHP way of doing ranges is similar to the C way:
for($i = 0; $i < count($arra); $i += 4) {
// ...
}

Categories