0

I have a array pulled from MySQL with the form below:

Array (
    [0] => Array (
        [id] => 0
        [settings_name] => auto_email
        [value] => 1
        [date_added] => 0
        [date_created] => 0
    )
    [1] => Array (
        [id] => 0
        [settings_name] => email_hour
        [value] => 17
        [date_added] => 0 [date_created] => 0
    )
)

What I am trying to do is to change the array to this form:

Array (
    [0] => Array (
        [id] => 0
        [auto_email] => 1
        [date_added] => 0
        [date_created] => 0
    )
    [1] => Array (
        [id] => 0
        [email_hour] => 17
        [date_added] => 0
        [date_created] => 0 )
    )

I am trying to acces the 'auto_email' setting directly not by settings_name. Could someone help me out?

3 Answers 3

1

Two ways, depending on if you want to create a new array, or just alter the existing one:

Create new:

$newArray = array();
foreach ($array as $value) {
    $newArray[] = array(
        'id' => $value['id'],
        $value['settings_name'] => $value['value'],
        'date_added' => $value['date_added'],
        'date_created' => $value['date_created']
    );
}

Alter existing:

foreach ($array as $key => $value) {
    $array[$key][$value['settings_name']] = $value['value'];
    unset($array[$key]['settings_name']);
    unset($array[$key]['value']);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try the command array_flip. From what you say i think you must save the id first to another array

see the manual: http://php.net/manual/en/function.array-flip.php

Comments

0

You should loop through the items in the array and create a new array the way you want.

$output = array();
foreach ($your_input_array as $v){
    $output[]=array(
                    'id' => $v['id'],
                    $v['settings_name']=>$v['value'], //use the setting name as key here
                    'date_added' => $v['date_added'],
                    'date_created' => $v['date_created']
                    );
}
print_r($output);

If you want to just alter the existing array you can use array_walk

array_walk($your_input_array, function (&$v){
    $v=array(
            'id' => $v['id'],
            $v['settings_name']=>$v['value'], //use the setting name as key here
            'date_added' => $v['date_added'],
            'date_created' => $v['date_created']
            );
});
print_r($your_input_array);

The above works only with PHP 5.3+ as anonymous functions are introduced in PHP 5.3. If you are stuck with version less than PHP 5.3 you can do it without anonymous function.

array_walk($your_input_array,'fixarray');
function fixarray(&$v){
    $v=array(
        'id' => $v['id'],
        $v['settings_name']=>$v['value'], //use the setting name as key here
        'date_added' => $v['date_added'],
        'date_created' => $v['date_created']
        );
}
print_r($your_input_array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.