Displaying php array in three column, first row one element and second and third row two element

$b = 0;
foreach ($settings['items'] as $item){
    $b++;
    if ($b==1){
        $out .='First';
    } else{
        $out2 .='Second';
    }
 }

 echo '<div class="inner">'.$out.'</div>';
 echo '<div class="inner">'.$out2.'</div>';

which output

 <div class="inner">First</div>
 <div class="inner">Second Second Second Second</div>

But I would like to have this structure

 <div class="inner">First</div>
 <div class="inner">Second Second</div>
 <div class="inner">Second Second</div>

One probable solution that I thought is using array_slice But for first loop I only need one element not two, that's why I think I can not do that.

Try this:

$first = true;
$all = [];
$couples = [];
foreach ($settings['items'] as $item){
    if($first){
        $first = false;
        $first_out = 'First'; //or whatever
        $all[] = $first_out;
    }else{
        if(count($couples) == 2){
            $all[] = $couples;
            $couples = [];
        }
        $couples[] = 'Second';
    }
}
foreach($all as $value){
    echo '<div class="inner">'.implode(',', $value).'</div>';
}

A multi-dimensional array or an array of objects from which to pull a column of values from. If an array of objects is provided, then public properties can be directly pulled. In order for protected or private properties to be pulled, the class must implement both the __get() and __isset() magic

A little pre-wrangling, remove the first item, and then merge it into the remaining chunked array.

<?php
$items   = array_fill(0, 5, 'foo'); // An array of five 'foo's.
$first   = array_shift($items);
$chunked = array_chunk($items, 2);
$chunked = array_merge([[$first]], $chunked);

var_dump($chunked);

foreach($chunked as $chunk)
{
    echo '<div class="inner">' . implode(' ', $chunk) . "</div>\n";
}

Output:

array(3) {
    [0]=>
    array(1) {
    [0]=>
    string(3) "foo"
    }
    [1]=>
    array(2) {
    [0]=>
    string(3) "foo"
    [1]=>
    string(3) "foo"
    }
    [2]=>
    array(2) {
    [0]=>
    string(3) "foo"
    [1]=>
    string(3) "foo"
    }
}
<div class="inner">foo</div>
<div class="inner">foo foo</div>
<div class="inner">foo foo</div>

Required. Specifies the multi-dimensional array (record-set) to use. As of PHP 7.0, this can also be an array of objects. column_key: Required. An integer key or a string key name of the column of values to return. This parameter can also be NULL to return complete arrays (useful together with index_key to re-index the array) index_key

Because you don't use value of $settings['items'] in the code, you can just work with that array length

$count = count($settings['items']);
if ($count--) echo `<div class="inner">First</div>`;
while($count > 1) {
   echo '<div class="inner">Second Second</div>';
   $count -= 2;
}
if ($count--) echo `<div class="inner">Second</div>`;

You must note though that using array_shift() will reset all numerical array keys to start counting from zero while literal keys will remain unchanged.; Using both array_shift() and array_pop() resets the array pointer of the input array after use, but in our case that doesn't matter since we're using a copy of the original array, that too with only a single element.

You can try this out,

$b = 0;
for($j = 1; $j <= 2; $j++){
    $b++;
    for($i = 1; $i <= $b;$i++){
        switch($b){
            case '1': echo '<div class="inner">FIRST</div>';break;
            case '2': echo '<div class="inner">SECOND SECOND</div>';break;
        }
    }
}

Parameters. needle. The searched value. Note: . If needle is a string, the comparison is done in a case-sensitive manner.. haystack. The array. strict. If the third parameter strict is set to TRUE then the in_array() function will also check the types of the needle in the haystack.

For higher-dimensional arrays, expand the syntax to match the array dimensions. Consider a random 3-by-3-by-3 numeric array. Access the element in the second row, third column, and first sheet of the array.

A three – dimensional array with 3 array containing 3 rows and 3 columns is shown below: Print 3D array in tabular format: To output all the elements of a Three-Dimensional array, use nested for loops. For this three for loops are required, One to traverse the arrays, second to traverse the rows and another to traverse columns. Example:

row[0] is the first column retrieved in the first resultset row[1] is the second column retrieved in the second resultset. That means row[0] should be the number while row[1] would be the customer. You could try associative array fetch.

Comments
  • Where is $out2 come from?
  • Use the modulo operator on the loop counter. $b % 3 == 0 means first column, $b % 3 != 0 means second or third. (Requires starting your counter at 0 for the first item.)
  • Or split your array into chunks of size 3.
  • You can also get the first element, unset it, and slice the array into chunks of size 2. easy
  • I will use $settings['items']