Move a child array to parent array and change parent/child name



PHP Snippet 1:

$final_array = array();
foreach($array1 as $offset1 => $array2) {

    $tmp_array = array();

    foreach($array2 as $offset2 =>  $array3) {

        if(is_array($array3)) {
            $tmp_array = $array3;
        } else {
            $tmp_array[$offset2] = $array3
        }

    }

    $final_array = array_merge($final_array, $tmp_array;); 
    //or
    $final_array[] = $tmp_array;

}

PHP Snippet 2:

$aStartArray =  array(array('Data3'=>array('id'=>1, 'cat'=>2), 'amount' => 1, 'Data_id'=>3));

foreach ($aStartArray as $iPos => $aArray) {

    $aKeys = array_keys($aArray); // fetches all the keys
    $aFirstElement = $aArray[$aKeys[0]]; // Get the first element using first key

    // assign/ overwrite data at the same position
    $aStartArray[$iPos] = array($aFirstElement, 'amount' => $aArray['amount'], 'Data_id' => $aArray['Data_id']);
}

echo "<pre>";
var_dump($aStartArray);

PHP Snippet 3:

foreach($arr as $key => $value){
        foreach($value as $k => $val){
            if(is_array($val)){                    
                $arr[$key] = $val;
                unset($arr[$key][$k]);
            }
        }
    }
    echo "<pre>"; print_r($arr);