باستخدام تنهار على المعلومات مجموعة من المكتسبة في حلقة while

StackOverflow https://stackoverflow.com/questions/1640617

  •  08-07-2019
  •  | 
  •  

سؤال

وأواجه مشكلة التالية. لدي 1/2/3/4/5/6 الأرقام وأريد أن قسمها إلى مجموعتين 1/3/5 و2/4/6. اختيار يجب أن تتم على أساس الموقف. هذا الجزء يعمل على ما يرام. وتأتي المشكلة عندما أريد أن مجموعة منهم مرة أخرى، عند استخدام وظيفة تنهار. ترى فقط العدد الأخير التي تم تخزينها. وأنا أعلم أنه لديها ما تفعله معي باستخدام هذه الرموز (لقد اخترت هذا الطريق منذ كمية من الأرقام لتصنيف يختلف في كل مرة):

$q++;
$row0 = $row0 + 2;
$row1 = $row1 + 2;

ولكن لا أستطيع أن الشكل وسيلة لإصلاحه أو طريقة أخرى للحصول على نفس النتيجة. نأمل شخص هنا يمكن لي نقطة في الاتجاه الصحيح. تركت رمز كاملة أدناه.


<?
$string = "1/2/3/4/5/6";
$splitted = explode("/",$string);
$cnt = count($splitted);
$q=0;
$row0=0;
$row1=1;
while($cnt > 2*$q)
{
  $p_row = implode(array($splitted[$row0]));
  echo "$p_row <br>";
  $i_row = implode(array($splitted[$row1]));
  echo "$i_row <br>";

  $q++;
  $row0 = $row0 + 2;
  $row1 = $row1 + 2;
}
$out = "implode(',', $i_row)";
var_dump($out);
?>

هل كانت مفيدة؟

المحلول

ويمكنك تقسيم مجموعة إلى مجموعات باستخدام % على مؤشر الحلقة. وضع كل مجموعة في مجموعة منفصلة. هنا مثال:

<?php
    $string = "1/2/3/4/5/6";
    $splitted = explode("/",$string);
    $group_odd = array();  ## all odd elements of $splitted come here
    $group_even = array(); ## all even elements of $splitted come here
    for ($index = 0; $index < count($splitted); ++$index) {
        ## if number is divided by 2 with rest then it's odd
        ## but we've started calculation from 0, so we need to add 1
        if (($index+1) % 2) { 
            $group_odd[] = $splitted[$index];
        }
        else {
            $group_even[] = $splitted[$index];
        }
    }
    echo implode('/', $group_odd), "<br />";  ## outputs "1/3/5<br />"
    echo implode('/', $group_even), "<br />"; ## outputs "2/4/6<br />"
    print_r($group_odd);
    print_r($group_even);
?>

نصائح أخرى

وأنا missread المشكلة على ما يبدو. بدلا من ذلك أعطي هذا التحسين.

$string = "1/2/3/4/5/6";
$splitted = explode("/", $string);
$group = array();
for ($index = 0, $t = count($splitted); $index < $t; ++$index) { 
    $group[$index & 1][] = $splitted[$index];
} 
$oddIndex = $group[0]; //start with index 1
$evenIndex = $group[1]; //start with index 2

echo "odd index:  " 
    . implode('/', $oddIndex) 
    . "\neven index: " 
    . implode('/', $evenIndex) 
    . "\n";

وبناء على موقفهم؟ لذلك، والانقسام على أساس التوزيع المتساوي / الغرابة مؤشر في مجموعة؟

وشيء من هذا القبيل؟

<?php

$string = "1/2/3/4/5/6";

list( $evenKeys, $oddKeys ) = array_split_custom( explode( '/', $string ) );

echo '<pre>';
print_r( $evenKeys );
print_r( $oddKeys );

function array_split_custom( array $input )
{
  $evens = array();
  $odds = array();
  foreach ( $input as $index => $value )
  {
    if ( $index & 1 )
    {
      $odds[] = $value;
    } else {
      $evens[] = $value;
    }
  }
  return array( $evens, $odds );
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top