2 Replies - 182 Views - Last Post: 15 October 2017 - 04:51 PM Rate Topic: -----

#1 Splashsky  Icon User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 284
  • Joined: 25-August 13

Rewriting a List of 151 things into an array

Posted 15 October 2017 - 03:48 PM

This isn't so much a particularly PHP-related thing as just wondering if anyone knows a much better way to turn a list of 151 of these...
INSERT INTO `$monsters` VALUES (1, 'Blue Slime', 4, 3, 1, 1, 1, 1, 0);
INSERT INTO `$monsters` VALUES (2, 'Red Slime', 6, 5, 1, 1, 2, 1, 0);


into these...
['name' => 'Blue Slime', 'health' => 4,  'damage' => 3, 'armor' => 1, 'level' => 1, 'experience' => 1, 'gold' => 1, 'immunity' => 0],
['name' => 'Red Slime', 'health' => 6,  'damage' => 5, 'armor' => 1, 'level' => 1, 'experience' => 2, 'gold' => 1, 'immunity' => 0],


rather than do them all by hand.

This post has been edited by Splashsky: 15 October 2017 - 03:48 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Rewriting a List of 151 things into an array

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13485
  • View blog
  • Posts: 53,857
  • Joined: 12-June 08

Re: Rewriting a List of 151 things into an array

Posted 15 October 2017 - 04:34 PM

Have the data in a database to start off with?
Was This Post Helpful? 0
  • +
  • -

#3 Splashsky  Icon User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 284
  • Joined: 25-August 13

Re: Rewriting a List of 151 things into an array

Posted 15 October 2017 - 04:51 PM

View Postmodi123_1, on 15 October 2017 - 04:34 PM, said:

Have the data in a database to start off with?

While that would've been a perfectly logical solution, I was making a Laravel seeder with all the data from an old open source project. I had to reformat it to work with what I wanted to accomplish...

AND SO I DID.

<?php
$strings = [
    "'Critter', 6, 5, 2, 1, 4, 2, 0",
    "'Creature', 10, 8, 2, 2, 4, 2, 0",
    "'Shadow', 10, 9, 3, 2, 6, 2, 1",
    "'Drake', 11, 10, 3, 2, 8, 3, 0",
    "'Shade', 12, 10, 3, 3, 10, 3, 1",
    "'Drakelor', 14, 12, 4, 3, 10, 3, 0",
    "'Silver Slime', 15, 100, 200, 30, 15, 1000, 2",
    // a bunch more enemies no one cares about
    "'Black Daemon', 400, 400, 280, 50, 3000, 1000, 1",
    "'Lucifuge', 600, 600, 400, 50, 10000, 10000, 2"
];

foreach($strings as $string) {
    $string = explode(', ', $string);
    echo "['name' => {$string[0]}, 'health' => {$string[1]},  'damage' => {$string[2]}, 'armor' => {$string[3]}, 'level' => {$string[4]}, 'experience' => {$string[5]}, 'gold' => {$string[6]}, 'immunity' => {$string[7]}],";
    echo "<br>";
}



hence the single greatest script of all time. Used the multiline select in Atom to do it <3
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1