简体   繁体   中英

PHP creating Array from an item list

I have the follow code:

  $states['XX'] = array(
      'undefined' => 'Choose your color',
      'yellow' => 'Yellow',
      'blue' => 'Blue,
      'red' => 'Red'
  );

And I have a list of lots of colors, one per line, example:

yellow
blue
red
green
pink
gray
white
black
...

I want to copy/past the color list on the code, so what is the easiest way to do that instead needing to type in apostrophes and => for each line of code repeatedly?

I believe you are after something like this?

$string = 'yellow
blue
red
green
pink
gray
white
black';
$display ="<h2>Results</h2>";
$to_array = explode("\n" , $string);
$n=0;
foreach($to_array as $color){
//DO CODE HERE
//EXAMPLE
$display .= 'Array '.$n.' color = '.$color.' <br />';
$n++;
}

echo $display;

You use the explode to define the string as an array and using "\n" to define the aray seperator as a new line break

With sed you would use something link that

sed -r "s/^(.)([a-z]*)/'\1\2' => '\U\1\L\2',/g" colors.txt

To get from in colors.txt

yellow
blue
red
green
pink
gray
white
black

To

'yellow' => 'Yellow',
'blue' => 'Blue',
'red' => 'Red',
'green' => 'Green',
'pink' => 'Pink',
'gray' => 'Gray',
'white' => 'White',
'black' => 'Black',

You can use as like,

$collection = "$"."states['XX'] = array(";
$arrayColorNames = array("'undefined'" ,"'yellow'","'blue'","'red'","'green'","'pink'","'gray'","'white'","'black'");

$arrayColorValues= array( "'Choose your color'","'Yellow'","'Blue'","'Red'","'Green'","'Pink'","'Gray'","'White'","'Black'");

$count = count($arrayColorNames);

foreach ($arrayColorNames as $key => $value) {
 $name = $value;
 $value = $arrayColorValues[$key];

 if ($key == $count-1) {
    $setLike = $name ."=>".$value;
    $collection .= $setLike;
  } else {
    $setLike = $name ."=>".$value.",";
    $collection .= $setLike;
  }
}

$collection .=");";
echo $collection;

Result like,

$states['XX'] = array('undefined'=>'Choose your color','yellow'=>'Yellow','blue'=>'Blue','red'=>'Red','green'=>'Green','pink'=>'Pink','gray'=>'Gray','white'=>'White','black'=>'Black'); 

Think It will help you.

Create your needed array with one line of code:

$colors = [
    'red', 'blue', 'yellow',
];
$select = array_combine($colors, array_map('ucfirst', $colors));
print_r($select);

it will output:

Array                                                                                                                                                     
(                                                                                                                                                         
    [red] => Red                                                                                                                                          
    [blue] => Blue                                                                                                                                        
    [yellow] => Yellow                                                                                                                                    
)                                                                                                                                                         

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM