Asked  7 Months ago    Answers:  5   Viewed   30 times

I have that array :

$a = array(
    "7" => array(
        "id" => 7,
        "parent" => 6
    ),
    "6" => array(
        "id" => 6,
        "parent" => 5
    ),
    "5" => array(
        "id" => 5,
        "parent" => 4
    ),
    "4" => array(
        "id" => 4,
        "parent" => 0
    ),
    "3" => array(
        "id" => 7,
        "parent" => 2
    ),
    "2" => array(
        "id" => 7,
        "parent" => 1
    ),
    "1" => array(
        "id" => 7,
        "parent" => 0
    )
);

the result that I want is that :

$a = array(
    "4" => array(
        "id" => 4,
        "parent" => 0,
        array(
            "5" => array(
                "id" => 5,
                "parent" => 4,
                array(
                    "6" => array(
                        "id" => 6,
                        "parent" => 5,
                        array(
                            "7" => array(
                                "id" => 7,
                                "parent" => 6
                            )
                        )
                    )
                )
            )
        )
    ),
    "2" => array(
        "id" => 7,
        "parent" => 1,
        array(
            "3" => array(
                "id" => 7,
                "parent" => 2
            )
        )
    ),
    "1" => array(
        "id" => 7,
        "parent" => 0
    )
);

the code that I use is this :

foreach($a as $v)
{
    if(isset($a[$v['PARENT']]))
    {
        $a[$v['PARENT']][$v['ID']] = $v;
        unset($a[$v['ID']]);
    }
}

and the problem that I have is that I get that result :

$a = array(
    "4" => array(
        "id" => 4,
        "parent" => 0,
        array(
            "5" => array(
                "id" => 5,
                "parent" => 4
            )
        )
    ),
    "2" => array(
        "id" => 7,
        "parent" => 1,
        array(
            "3" => array(
                "id" => 7,
                "parent" => 2
            )
        )
    ),
    "1" => array(
        "id" => 7,
        "parent" => 0
    )
);

instead of the need it result.

 Answers

21

To solve your problem you need to properly understand how variable referencing/aliasing in PHP works.

Look at the following example code, which does not look much different to yours but makes use of references in order to access any parent even it has already "moved":

# transform $flat into a tree:
foreach($flat as $id => &$value)
{
    # check if there is a parent
    if ($parentId = $value['parent'])
    {
        $flat[$parentId][0][$id] =& $value; # add child to parent
        unset($flat[$id]); # remove reference from topmost level
    }
}
unset($value); # remove iterator reference
print_r($flat); # your tree

$flat now contains all values from $flat - but reordered. Demo.

Wednesday, March 31, 2021
 
Amber
answered 7 Months ago
42
function set_val(array &$arr, $path,$val)
{
   $loc = &$arr;
   foreach(explode('.', $path) as $step)
   {
     $loc = &$loc[$step];
   }
   return $loc = $val;
}
Wednesday, March 31, 2021
 
laurent
answered 7 Months ago
39

When you set cell values individually, you have the option of setting the datatype explicitly, but when you use the fromArray() method, you don't have this option.

However, by default, PHP uses a default value binder to identify datatypes from the values passed, and set the cell datatype accordingly. This default behaviour is defined in a class /PHPExcel/Cell/DefaultValueBinder.php.

So you can create your own value binder, as described in the PHPExcel Documentation, that would set every value as a string datatype.

Something like:

class PHPExcel_Cell_MyColumnValueBinder extends PHPExcel_Cell_DefaultValueBinder implements PHPExcel_Cell_IValueBinder
{
    protected $stringColumns = [];

    public function __construct(array $stringColumnList = []) {
        // Accept a list of columns that will always be set as strings
        $this->stringColumns = $stringColumnList;
    }

    public function bindValue(PHPExcel_Cell $cell, $value = null)
    {
        // If the cell is one of our columns to set as a string...
        if (in_array($cell->getColumn(), $this->stringColumns)) {
            // ... then we cast it to a string and explicitly set it as a string
            $cell->setValueExplicit((string) $value, PHPExcel_Cell_DataType::TYPE_STRING);
            return true;
        }
        // Otherwise, use the default behaviour
        return parent::bindValue($cell, $value);
    }
}

// Instantiate our custom binder, with a list of columns, and tell PHPExcel to use it
PHPExcel_Cell::setValueBinder(new PHPExcel_Cell_MyColumnValueBinder(['A', 'B', 'C', 'E', 'F']));

$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->fromArray($dataArray,null,"A2");
Friday, May 28, 2021
 
Wilk
answered 5 Months ago
70

You need to treat the $show as an array and just extract the part you want, something like this:

<ul>
    <?php foreach( $results as $year => $shows ): ?>
    <li><?= $year ?>
      <ul>
        <?php foreach( $shows as $show_name => $show ): ?>
        <li><?= $show_name ?>
             <ul>
                  <?php foreach( $show as $class ): ?>
                  <li><?= $class ?></li>
                  <?php endforeach; ?>
             </ul>
        </li>
        <?php endforeach; ?>
      </ul>
    </li>
    <?php endforeach; ?>
</ul>
Saturday, May 29, 2021
 
huhushow
answered 5 Months ago
98
$new_arr = array();
foreach($arr as $data) $new_arr[$data['id']] = $data;

foreach($new_arr as $id => &$data) $data['parent'] =& $new_arr[$data['parent']];

Now you have a proper array structure.

But you may have to turn it inside out, since it's structured by child. But your output is by parent.

So instead of the second foreach I posted, do this:

foreach($new_arr as $id => $data) $new_arr[$data['parent']]['children'][] =& $data;
Saturday, May 29, 2021
 
jsuissa
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :