1

I have a multidimensional array like this:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => mail_failures_only
            [value] => 0
        )

    [1] => Array
        (
            [id] => 2
            [name] => default_service_ttl
            [value] => 60
        )

    [2] => Array
        (
            [id] => 3
            [name] => version
            [value] => 5.0.0.0
        )

    [3] => Array
        (
            [id] => 4
            [name] => process_all_jobs
            [value] => 1
        )
)

I want each value of "name" and each value of "value" to be converted to an associative array like this:

process_all_jobs =>1

How do a construct loop for this?

I tried something like this:

$system_config_array = array();
        if (is_array($configList)) {
            $keys = array_keys($configList);
            for($i = 0; $i < count($configList); $i++) {
                foreach($configList[$keys[$i]] as $key => $value) {
                    echo $key . " : " . $value . "<br>";
                    if($key!='id')
                        $system_config_array[$value] = '';
                }
            }
       }

where $configList is the initial array.

3
  • 1
    see this : eval.in/1017632 Commented Jun 8, 2018 at 14:17
  • Thanks a ton!!! It worked :) And I was using 2 for loops and an if and going mad .. Commented Jun 8, 2018 at 14:25
  • Please vote up my question too. Commented Jun 8, 2018 at 14:25

2 Answers 2

2

Hope this will help you :

$base = 'your array';
foreach ($base as $key => $value) {
    $data[$value['name']] = $value['value'];
}
print_r($data);

https://eval.in/1017632

Sign up to request clarification or add additional context in comments.

Comments

2

Use array_column.

$result = array_column($arr, "value", "name");  

But this only works if the names are unique.

It doesn't answer your question to create a loop for it, but I belive array_column is a better tool for the job

2 Comments

Your array with value and names
Looping is not an optimal solution

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.