LeeBronwin LeeBronwin - 5 months ago 12
PHP Question

Formatting an Array in php (3 into one)

So currently I have an array that pulls data based on an attribute and it puts the data in its own seperate array. What I need to do is to put these 3 arrays into one, so if one of them is null, it won't give me errors. It should be fairly simply but I can't wrap my head around it.

//CV eqpValue
if (is_array(FullDataResponse->dlr->DesignLayoutRecord)) {
foreach(FullDataResponse->dlr->DesignLayoutRecord as $DesignLayoutRecord_key => $DesignLayoutRecord_value ) {
if ($DesignLayoutRecord_value->cktEqpOptions->CktEqpOptions && is_array($DesignLayoutRecord_value->cktEqpOptions->CktEqpOptions)) {
foreach ($DesignLayoutRecord_value->cktEqpOptions->CktEqpOptions as $cv_obj)
{
if($cv_obj->attribute === 'CDR')
{
$this->cvCDRList[] = array("cdr" => $cv_obj->eqpValue);
}

if($cv_obj->attribute === 'CUSTOMER')
{
$this->cvCustomerList[] = array("customer" => $cv_obj->eqpValue);
}

if($cv_obj->attribute === 'LEASE LINE')
{
$this->cvphoneList[] = array("phoneNumber" => $cv_obj->eqpValue);
}
}
}
}
}


See how they are currently put into separate arrays like cvCDRList, cvCustomerList, and cvphoneList? How would I put them into a single array? Thanks!!

Answer

you can use array_merge.

<?php

   $array1 = array("a12","a12","a13");
   $array2 = array("a21","a22","a23");
   $array3 = array("a31","a32","a33");

   $finalArray = array_merge($array1,$array2,$array3);              
   foreach( $finalArray as $key => $value ){
       echo $key."=>".$value."<br>";
   } 

?>

You can create another property called "allAtributes" . When you finalize to fill your 3 arrays you can call to another method of class than make the merge between 3 array.

The finally you will have got like this :

allAtributes (array)=>{ 
        ["cdr"](array)=> {
            ['cdrkey1'] = cdrAtt1 ,
            ['cdrKey2'] = cdrAtt2 ...
        },
        ["customer"](array)=> {
            ['customerkey1'] = customerAtt1 ,
            ['customerKey2'] = customerAtt2 ...
        },
        ["phoneNumber"](array)=> {
            ['phoneNumberkey1'] = phoneNumberAtt1 ,
            ['phoneNumberKey2'] = phoneNumberAtt2 ...
        } 
}
Comments