Question

I have an array in php like this :

$temp = array( 'vpc1' => array( 'reg1' => array ( 'sub1', ) ) 'vpc2' => array( 'reg1' => array ( 'sub2', ) ) 'vpc1' => array( 'reg1' => array ( 'sub3', ) ) );

I have to merge the values to get output as :

`$temp = array(
'vpc1' => array(
   'reg1' => array (
       'sub1',
       'sub3',
    )
)
'vpc2' => array(
    'reg2' => array (
       'sub2',
    )
)

);`

Any help will be appreciated. Thanks in advance.

Was it helpful?

Solution

Your first problem is your incoming data structure in $temp1. Think about your data and make it atomar. Thats why some boy give u "-1 vote" ... I think its just a question. I voted your question up again.

 <?php 

//u got this
$temp1 = array("vpc1 | reg1 | sub1", "vpc1 | reg1 | sub2", "vpc1 | reg1 | sub3");
//u want this
$temp2 = array("vpc1 | reg1 | sub1,sub2,sub3");

//u need this, cause thats why we use arrays in complex data structures.
$temp = array(
    'vpc1' => array(
       'reg1' => array (
           'sub1',
           'sub2',
           'sub3',
        )
    )
);

//ugly bug working
foreach ($temp1 as $data) {
    $parts = preg_split("/\|/", $data);

    // most ugly part in it. Thats where you guys should think about your data again.
    $temp[$parts[0]][$parts[1]][$parts[2]]; 
}

var_dump($temp);

// array (size=1)
//     'vpc1' =>
//         array (size=1)
//         'reg1' =>
//             array (size=3)
//                 0 => string 'sub1' (length=4)
//                 1 => string 'sub2' (length=4)
//                 2 => string 'sub3' (length=4)

?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top