Creating a list and indexing

Viewed 127

Given a two list. Create a third list by picking an odd-index element from the first list and even index elements from the second. For Example:

listOne = [3, 6, 9, 12, 15, 18, 21]
listTwo = [4, 8, 12, 16, 20, 24, 28]

Expected Output:

Printing Final third list [6, 12, 18, 4, 12, 20, 28]

6 Answers
//init result array

//loop over listOne, using for($i=1;$i<sizeof($listOne);$i=$i+2)
//and add to result for each iteration, $resultArr[] = $listOne[$i]

//do the same with listTwo, but for($i=*0*

I think, it will be helpful for you.

<?php 

$listOne = [3, 6, 9, 12, 15, 18, 21];

$listTwo = [4, 8, 12, 16, 20, 24, 28];

$NlistOne=[];
$NlistTwo=[];

//odd-index positions from list one [6, 12, 18]
foreach ($listOne as $key => $value) {
    if($key%2==1){
        array_push($NlistOne, $value);
    }
}


//even-index positions from list two [4, 12, 20, 28]
foreach ($listTwo as $key => $value) {
    if($key%2==0){
        array_push($NlistTwo, $value);
    }
}

//Printing Final third list [6, 12, 18, 4, 12, 20, 28]
print_r(array_merge($NlistOne,$NlistTwo));

?>

Output will be:

Array ( [0] => 6 [1] => 12 [2] => 18 [3] => 4 [4] => 12 [5] => 20 [6] => 28 ) 

You can merge both of arrays and then pick all odd elements

$listOne = [3, 6, 9, 12, 15, 18, 21];
$listTwo = [4, 8, 12, 16, 20, 24, 28];
$result = [];

foreach ( array_merge($listOne, $listTwo) as $value ){
    if ( $key % 2 ) {
        $result[] = $value;
    }
}

If array length isn't fixed, say it could contain not 7 elements, then you need to check it before merging to make it have odd number of elements

$listOne = [3, 6, 9, 12, 15, 18, 21, 777];
$listTwo = [4, 8, 12, 16, 20, 24, 28];
$result = [];
if ( count($listOne) % 2 !== 1 ) {
    $listOne[] = 0;
}
foreach( array_merge($listOne, $listTwo) as $value ){
    if ( $key % 2 ) {
        $result[] = $value;
    }
}

you don't have to loop over whole array array_filter will do this for you, Constant ARRAY_FILTER_USE_KEY will check each key for odd or for even

<?php
$a1 = [3, 6, 9, 12, 15, 18, 21];
$a2 = [4, 8, 12, 16, 20, 24, 28];

function odd($var)
{
    // returns whether the input integer is odd
    return $var & 1;
}

function even($var)
{
    // returns whether the input integer is even
    return !($var & 1);
}

$result= (array_merge(array_filter($a1, 'odd', ARRAY_FILTER_USE_KEY),array_filter($a2, 'even', ARRAY_FILTER_USE_KEY)));

output you will get

Array ( 
[0] => 6 
[1] => 12 
[2] => 18 
[3] => 4 
[4] => 12 
[5] => 20 
[6] => 28 )

Iterate over first one and take only values of odds indices, and loop again through second one and take evens indices.

$listOne = [3, 6, 9, 12, 15, 18, 21];
$listTwo = [4, 8, 12, 16, 20, 24, 28];
$res = [];
for ($i=0; $i < count($listOne); $i++) { 
    if($i & 1) // $i is odd.
        $res[] = $listOne[$i];
}
for ($j=0; $j < count($listTwo); $j++) { 
     if(n % 2 === 0) // $j is even.
        $res[] = $listTwo[$j];
}

Result:

echo "List One :".json_encode($listOne)."<br>";
echo "List Two :".json_encode($listTwo)."<br>";
echo "Lists Merged:".json_encode($res);

Output:*

/*
List One :[3,6,9,12,15,18,21]
List Two :[4,8,12,16,20,24,28]
Lists Merged:[6,12,18,4,12,20,28]
*/

Iterate over arrays:

  • take odds in array starting with index One and increment it by two.
  • take evens in array by starting with index Zero and increment it by two.
$listOne = [3, 6, 9, 12, 15, 18, 21]; $listTwo = [4, 8, 12, 16, 20, 24, 28];
$res = [];
for ($i=1; $i < count($listOne); $i+=2) {
    $res[] = $listOne[$i];
}
for ($j=0; $j < count($listTwo); $j+=2) {
    $res[] = $listTwo[$j];
}
print(json_encode($res)); // [6,12,18,4,12,20,28]
Related