Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I create an array of variables based on an associative array's value in PHP?

Given the following array

 ( 
 0 => array( 
     'orders_messaging_info_id' => '1',
     'column_name' => 'message_route',
     'column_info' => 'internal',
     ),
 
 1 => array(
     'orders_messaging_info_id' => '2',
     'column_name' => 'message_route',
     'column_info' => 'external',
     ),
 
 2 => array(
     'orders_messaging_info_id' => '3',
     'column_name' => 'message_type',
     'column_info' => 'SMS',
     ),
 
 3 => array(
     'orders_messaging_info_id' => '4',
     'column_name' => 'message_type',
     'column_info' => 'email',
     ),
 
 4 => array(
     'orders_messaging_info_id' => '5',
     'column_name' => 'message_type',
     'column_info' => 'slack',
     ),
 
 5 => array(
     'orders_messaging_info_id' => '6',
     'column_name' => 'message_type',
     'column_info' => 'plaintext',
     ),
 
 6 => array(
     'orders_messaging_info_id' => '10',
     'column_name' => 'message_text',
     'column_info' => 'JSON',
     ),
 )

how can I create three associative arrays named for the value of ‘column_name’ whose keys are the value at ‘orders_messaging_info_id’ and values are at ‘column_info’?

I tried using extract() in a foreach loop on the value of ‘column_name’ to create three variables — one for each distinct value — but I don’t know how to access the other two values per array to assign them. And once I tackle that, I know I’ll have to figure out a way to add values to the arrays that extract() creates, such as with the array variable that will be named $message_type[] or $message_route[]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The expected array would look like

$message_route = array('1'=>'internal','2'=>'external');

>Solution :

extract() doesn’t really help you here. Instead, you should make use of variable variables (https://www.php.net/manual/en/language.variables.variable.php). Something like:

<?php

$array = array(
 0 => array( 
     'orders_messaging_info_id' => '1',
     'column_name' => 'message_route',
     'column_info' => 'internal',
     ),
 1 => array(
     'orders_messaging_info_id' => '2',
     'column_name' => 'message_route',
     'column_info' => 'external',
     ),
 2 => array(
     'orders_messaging_info_id' => '3',
     'column_name' => 'message_type',
     'column_info' => 'SMS',
     ),
 3 => array(
     'orders_messaging_info_id' => '4',
     'column_name' => 'message_type',
     'column_info' => 'email',
     ),
 4 => array(
     'orders_messaging_info_id' => '5',
     'column_name' => 'message_type',
     'column_info' => 'slack',
     ),
 5 => array(
     'orders_messaging_info_id' => '6',
     'column_name' => 'message_type',
     'column_info' => 'plaintext',
     ),
 6 => array(
     'orders_messaging_info_id' => '10',
     'column_name' => 'message_text',
     'column_info' => 'JSON',
     ),
 );
 
 foreach($array as $entry) {
     $name = $entry['column_name'];
     if (!isset($$name)) {
        $$name = [];    
     }
     $$name[$entry['orders_messaging_info_id']] = $entry['column_info'];
 }
 
 var_dump($message_text, $message_route, $message_type);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading