Asked  7 Months ago    Answers:  5   Viewed   36 times

Say, we have an array: array(1,2,3,4,...) And I want to convert it to:

array(
    1=>array(
        2=>array(
            3=>array(
                4=>array()
            )
        )
    )
)

Can anybody help?
Thanks

EDIT It would be good to have the solution with iterations.

 Answers

80
$x = count($array) - 1;
$temp = array();
for($i = $x; $i >= 0; $i--)
{
    $temp = array($array[$i] => $temp);
}
Wednesday, March 31, 2021
 
Amber
answered 7 Months ago
47
function parameterize_array($array) {
    $out = array();
    foreach($array as $key => $value)
        $out[] = "$key=$value";
    return $out;
}
Wednesday, March 31, 2021
 
Akdeniz
answered 7 Months ago
71

explode and implode are working as expected. The first is used to split a string and return an array with the pieces e.g.

 $pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
 $pieces = explode(" ", $pizza);
 echo $pieces[0]; // piece1
 echo $pieces[1]; // piece2

That is why you get this: Array ( [0] => Array ( [0] => Insole Premium Shore 30 ) [1] => Array ( [0] => G Diabetic Premium closed Sandal Black size 09 ) ) when you print $prdtarray (echo print_r($prdtarray);). Each time you call $prdtarray[] = explode(",", $row1->product_name); you are creating a new array with one element (only one element because $row1->product_name doesn't contains any ",") and adding it to $prdtarray.

implode is used to Join array elements with a string e.g.

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);

echo $comma_separated; // lastname,email,phone

In your code since you have two arrays inside $prdtarray the result of $product is

 Array,Array

So, you can save the $row1->product_name value in an array and then use implode to create a comma separated values result. The code looks like this:

$this->db->select("product_name")
->from('sale_items')
->where('sale_items.sale_id',4221);
$q1 = $this->db->get();
if ($q1->num_rows() > 0) {
 foreach (($q1->result()) as $row1) {
$prdtarray[] = $row1->product_name;
}
$product=implode(',',$prdtarray); 
echo $product ; // Insole Premium Shore 30,G Diabetic Premium closed Sandal Black size 09
Wednesday, March 31, 2021
 
keyBeatz
answered 7 Months ago
36

The following snippet should do the trick, adapted from some PHP Doctrine code I found on the web :

function toHierarchy($collection)
{
        // Trees mapped
        $trees = array();
        $l = 0;

        if (count($collection) > 0) {
                // Node Stack. Used to help building the hierarchy
                $stack = array();

                foreach ($collection as $node) {
                        $item = $node;
                        $item['children'] = array();

                        // Number of stack items
                        $l = count($stack);

                        // Check if we're dealing with different levels
                        while($l > 0 && $stack[$l - 1]['depth'] >= $item['depth']) {
                                array_pop($stack);
                                $l--;
                        }

                        // Stack is empty (we are inspecting the root)
                        if ($l == 0) {
                                // Assigning the root node
                                $i = count($trees);
                                $trees[$i] = $item;
                                $stack[] = & $trees[$i];
                        } else {
                                // Add node to parent
                                $i = count($stack[$l - 1]['children']);
                                $stack[$l - 1]['children'][$i] = $item;
                                $stack[] = & $stack[$l - 1]['children'][$i];
                        }
                }
        }

        return $trees;
}
Saturday, May 29, 2021
 
ajaybc
answered 5 Months ago
10

You're missing only one important piece: when you make the recursive call to formatHtml() you're not actually including the returned content anywhere! Append it to $var and you should get much better results:

function formatHtml($array) {
    $var = '<div>';

    foreach ($array as $k => $v) {

            if (is_array($v['children']) && !empty($v['children'])) {
                $var .= formatHtml($v['children']);
            }
            else {
                $var .= $v['cid'];
            }
    }

    $var.= '</div>';

    return $var;
}
Saturday, May 29, 2021
 
QuantumMechanic
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 :