У меня на php есть такой массив:

echo "
";
print_r($header_item);

Результат:

Array
(
  [0] => stdClass Object
   (
     [NO_INSPECTION] => 47
     [ID_CONDITION] => 1
     [NAMA_CONDITION] => DIRTY
   )

  [1] => stdClass Object
   (
     [NO_INSPECTION] => 47
     [ID_CONDITION] => 2
     [NAMA_CONDITION] => DAMAGE
    )
  [2] => stdClass Object
   (
    [NO_INSPECTION] => 
    [ID_CONDITION] => 
    [NAMA_CONDITION] => CLEAN
   )
  [3] => stdClass Object
   (
    [NO_INSPECTION] => 
    [ID_CONDITION] => 
    [NAMA_CONDITION] => OFF HIRE
 )
)

У меня такой случай, этот массив будет разделен на два новых объекта массива на основе четного и нечетного индекса. Я пишу так:

NO_INSPECTION);
                array_push($even, $v->ID_CONDITION);
                array_push($even, $v->NAMA_CONDITION);
             } else {
                array_push($odd, $v->NO_INSPECTION);
                array_push($odd, $v->ID_CONDITION);
                array_push($odd, $v->NAMA_CONDITION);
             }
        $breakrow++;
       }
  ?>

Но у меня есть новые массивы вроде этого:

echo "
";
print_r($even);
echo "
"; echo "
";
print_r($odd);

Array
(
 [0] => 47
 [1] => 1
 [2] => DIRTY
 [3] => 
 [4] => 
 [5] => CLEAN
)
Array
(
 [0] => 47
 [1] => 2
 [2] => DAMAGE
 [3] => 
 [4] => 
 [5] => OFF HIRE
)

Как сделать вот так:

  Array
  (
    [0] => stdClass Object
      (
       [NO_INSPECTION] => 47
       [ID_CONDITION] => 1
       [NAMA_CONDITION] => DIRTY
      )
    [1] => stdClass Object
      (
       [NO_INSPECTION] => 
       [ID_CONDITION] => 
       [NAMA_CONDITION] => CLEAN
      )

И:

  Array
  (
    [0] => stdClass Object
      (
       [NO_INSPECTION] => 47
       [ID_CONDITION] => 2
       [NAMA_CONDITION] => DAMAGE
      )
    [1] => stdClass Object
      (
       [NO_INSPECTION] => 
       [ID_CONDITION] => 
       [NAMA_CONDITION] => OFF HIRE
      )

Fadly Dzil

Ответов: 2

Ответы (2)

Вы можете добиться этого, используя array_walk и простой тест четности и нечетности на ключе.

$header_item = [
    0   =>  'zero',
    1   =>  'one',
    2   =>  'two',
    3   =>  'three',
    4   =>  'four',
    5   =>  'five',
    6   =>  'six',
    7   =>  'seven'
];

$odd = array();$even = array();

array_walk($header_item,function($value,$key) use(&$odd,&$even){
    if ($key%2==0) {
        $even[]=$value;
    }
    else{
        $odd[]=$value;
    }
});

print_r($even);
print_r($odd);

Выводит

Array
(
    [0] => zero
    [1] => two
    [2] => four
    [3] => six
)
Array
(
    [0] => one
    [1] => three
    [2] => five
    [3] => seven
)

Попробуйте это: -

 $odd = array();
    $even = array();
    $breakrow = 0;
       foreach ($header_item as $v) {
             if ($breakrow % 2 == 0) {
               $even[] = $v;
             } else {
               $odd[] = $v;
             }
        $breakrow++;
       }

  echo '
'; print_r($even).'
'; echo '
'; print_r($odd).'
';

вывод: -

Array
(
    [0] => Array
        (
            [NO_INSPECTION] => 47
            [ID_CONDITION] => 1
            [NAMA_CONDITION] => DIRTY
        )

    [1] => Array
        (
            [NO_INSPECTION] => 
            [ID_CONDITION] => 
            [NAMA_CONDITION] => CLEAN
        )

)
Array
(
    [0] => Array
        (
            [NO_INSPECTION] => 47
            [ID_CONDITION] => 2
            [NAMA_CONDITION] => DAMAGE
        )

    [1] => Array
        (
            [NO_INSPECTION] => 
            [ID_CONDITION] => 
            [NAMA_CONDITION] => OFF HIRE
        )

)

2022 WebDevInsider