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

Time:03-10

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[]

The expected array would look like

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

CodePudding user response:

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);
  • Related