Page 1 of 1
Make a multidemensional array 1-indexed from string-indexed?
Posted: Wed Mar 30, 2011 1:27 pm
by ShadowDog
Code: Select all
<?php
$ini = parse_ini_file('letsplay.ini',true);
asort($ini);
$cur_num = 1;
while($cur_num <= count($ini))
{
//This should look in the first entry and return the url, name, and author. Instead, it goes "by" for about 693 lines.
echo('<a href="' . $ini[$cur_num]['url'] . '">' . $ini[$cur_num]['name'] . ' by ' . $ini[$cur_num]['author'] . '</a><br>');
$cur_num += 1;
}?>
.ini I'm working with
My results.
I'm having a hard time figuring out how to do this. What I'm trying to do is take an .ini file and display things from it after it's been alphabetized (this also has the nice side effect of removing duplicate entries). I want to access the 'name', 'author', and 'url' keys' values for the
nth array, but it's not working. From the documentation I've look at, I assume that PHP doesn't understand that I want to read the
nth thing in the list, but instead thinks that I'm looking for
n in the list. If I could convert the first part of the multidimensional array to a number after sorting, I'd be set; unfortunately, I haven't got a clue as to how I would go about that. Can anyone help?
P.S. Why does PHP use a period for concatenation rather than a plus?
Re: Make a multidemensional array 1-indexed from string-inde
Posted: Wed Mar 30, 2011 2:00 pm
by AbraCadaver
You need to do a print_r() on the $ini array to see the structure. It's not numerically indexed it's associative and each key is an array (multidimensional). foreach() is awesome. Something like this:
Code: Select all
foreach($ini as $song => $props) {
echo('<a href="' . $props['url'] . '">' . $props['name'] . ' by ' . $props['author'] . '</a><br>');
}
The . is used instead of the + because this is PHP not JS. If it were VB it would be a & and so one.
Re: Make a multidemensional array 1-indexed from string-inde
Posted: Wed Mar 30, 2011 3:43 pm
by ShadowDog
Thank you. I completely messed up last time I tried foreach(). Though I noticed that it did sort based on case, and with genres set were at the top. Oddly, switching from
to
worked perfectly (though the Hexen LP is still being a jerk...). Thanks for the help!
P.S. The reason I was confused about the concatenation thing was because literally every language I had used before used plus, and I thought PHP was literally the only exception. I didn't really realize that with lots of programming languages, there's a high probability of there being things that seem really weird to me.
Re: Make a multidemensional array 1-indexed from string-inde
Posted: Wed Mar 30, 2011 3:59 pm
by AbraCadaver
Also, I only put both $song and $props there out of habit. This should work as well:
Code: Select all
foreach($ini as $song) {
echo('<a href="' . $song['url'] . '">' . $song['name'] . ' by ' . $song['author'] . '</a><br>');
}
Re: Make a multidemensional array 1-indexed from string-inde
Posted: Wed Mar 30, 2011 8:23 pm
by ShadowDog
Thank you. Unfortunately, I've run into another horrible error.
Code: Select all
<?php
function fixit($tofix)
{
if($fix_ini[curlp][$tofix])
{
return $fix_ini[curlp][$tofix];
}
else
{
return $props[$tofix];
}
}
$ini = parse_ini_file('letsplay.ini',true);
$fix_ini = parse_ini_file('fix.ini',true);
//asorti($ini);
uksort($ini, 'strcasecmp');
?>
<table border=1px>
<?php
$cur_num = 1;
foreach($ini as $props)
{
$curlp = $props['name'] . $props['author'];
echo('<tr><td><a href="' . fixit('url') . '">' . $props['name'] . '</a></td><td>' . $props['author'] . '</td><td>' . $props['genre'] . '</td><td><a href="?name=' . $props['name'] . '&author=' . $props['author'] . '">*</a></td></tr><br>');
if($props['altname'])
{
echo('<tr><td>Alternate names: ' . $props['altname'] . '</td></tr><br>');
}
}
?>
</table>
I'm used to being able to check if whatever2.ini has something on blah, and failing that, returning what whatever.ini says. I'm trying to do that here for the url. It should be changing the 302 Error's URL to say "Puppy" (something completely random I came up with that won't mean anything once I update the letsplay.ini file) and leaving the other URLs alone, but instead, it's making all the URLs blank. This seems to be a scope error or something; I honestly haven't got enough PHP experience to diagnosis it myself. Could someone help? Thank you.