0

I stumbled across some code that I wrote a few years ago when I was first learning PHP and didn't know that arrays start at 0 and not 1.

$afc_east[1] = "Buffalo Bills";
$afc_east[2] = "Miami Dolphins";
$afc_east[3] = "New England Patriots";
$afc_east[4] = "New York Jets";

$afc_west[1] = "Denver Broncos";
$afc_west[2] = "Kansas City Chiefs";
$afc_west[3] = "Oakland Raiders";
$afc_west[4] = "San Diego Chargers";

//.... other divisions...

//Put all of the arrays into one
$afc = array($afc_east, $afc_west, $afc_north, $afc_south);

for($i=0;$i<count($afc);$i++)
{   
    $count = count($afc[$i]);   

    for($y=1;$y<=$count;$y++)
    {
        // I'd like to find out how to echo "afc_east" or "afc_west"
        $name_of_array = ""; //Idk
        echo "$".$name_of_array."[".$y-1."]" = ".$afc[$i][$y].";<br />";
    }

}

I want to make all of my arrays start at 0. But, there are simply too many arrays for me to go back and change the numbers in the arrays to one below what they currently are. It would be much easier for me to have php echo out the array names along with their corresponding values and then copy and paste them into the text editor.

5
  • You should be able to easily decrement all your array indices in any text editor that supports macros. IMO you should not add count to account for the 1 based indexing, as when you eventually do go through and change the arrays to be 0 indexed, you will have even more work to do. Commented Jul 12, 2013 at 4:30
  • I'm using text wrangler. Any idea how I should go about doing that? Commented Jul 12, 2013 at 4:31
  • You cannot get it using your code, you should assign names manually using something like $afc = array('afc_east' => $afc_east, ... Commented Jul 12, 2013 at 4:32
  • Hopefully someone else can help with that. I have never used text wrangler. Sorry Commented Jul 12, 2013 at 4:32
  • Demo of @zerkms's proposal: viper-7.com/zFWoS5 Commented Jul 12, 2013 at 4:43

4 Answers 4

2

If all your elements are declared in the correct order, you can simply remove the hardcoded keys and let PHP enumerate:

$afc_east[1] = "Buffalo Bills";
$afc_east[2] = "Miami Dolphins";
$afc_east[3] = "New England Patriots";
$afc_east[4] = "New York Jets";

Becomes

$afc_east = array(); // Optional
$afc_east[] = "Buffalo Bills";
$afc_east[] = "Miami Dolphins";
$afc_east[] = "New England Patriots";
$afc_east[] = "New York Jets";

Your editor should allow you to substitute via regex, e.g. replace (\$[a-z_]+\[)\d+(\] =) with \1\2.

Sign up to request clarification or add additional context in comments.

8 Comments

Kudos for the specific regex :) Be sure to do this find-and-replace semi-manually rather than doing Replace All, since it'd be easy to create a hard-to-find error by replacing the wrong thing.
@Matchu Indeed :) I always use Replace and find when replacing by regex. Comes from experience (i.e., too much time wasted hunting bug I "cleverly" introduced.)
Wait, wait. My text editor and not a php script will replace the numbers in the arrays?
Yeah. Not exactly sure how to do that in text wrangler though
Text Wrangler claims to support regex replace so I would go hunting in the Edit menu. Not familiar with TW, though.
|
0

PHP doesn't offer this feature. Consider the following:

$foo = array("a", "b", "c");
$bar = $foo;

Is the array's name foo or bar? Both? Neither? The real answer here is that the array is its own object, and it doesn't keep track of where it's been stored. If you store it somewhere else, you lose all information about what other variables it's been stored in.

…sigh. I'm confident there's a better way to do this with your text editor, but, if all we want to do is tweak your code to get it to work as expected, we could use… sheesh, I can't believe I'm gonna say this… variable variables.

The following is a really sketchy PHP feature that I really don't like but might be useful in this very specific situation:

$username = "Matchu";
$var_name = "username";
echo $$var_name; // echoes "Matchu"

Instead of storing the arrays in an array and iterating over those, we could instead iterate over the variable names:

$afc = array("afc_east", "afc_west", "afc_north", "afc_south");
foreach($afc as $name_of_array) {
    $array_value = $$name_of_array;
    // Ta da, you have the variable name and the array stored in it. Go crazy.
}

I very, very strongly recommend against using this feature in your everyday code, but, in this very special case where you actually do want to work with variable names for a very specific reason, it could be handy.

Comments

0

if you have to reindex array and have numerical keys in the array you can do it simply

$myarray = array_values($myarray);
// example 
$afc_east[1] = "Buffalo Bills";
$afc_east[2] = "Miami Dolphins";
$afc_east[3] = "New England Patriots";
$afc_east[4] = "New York Jets";
$afc_east = array_values($afc_east);
echo '<pre>';
print_r($afc_east);

Output :

Array
(
[0] => Buffalo Bills
[1] => Miami Dolphins
[2] => New England Patriots
[3] => New York Jets
)

2 Comments

This doesn't help me get the name of the arrays
@Lance to do create associative array like this:- $afc = array('afc_east'=>$afc_east, 'afc_west'=>$afc_west);
0

Well here is the script, quick and dirty

<?php

$afc_east[1] = "Buffalo Bills";
$afc_east[2] = "Miami Dolphins";
$afc_east[3] = "New England Patriots";
$afc_east[4] = "New York Jets";

$afc_west[1] = "Denver Broncos";
$afc_west[2] = "Kansas City Chiefs";
$afc_west[3] = "Oakland Raiders";
$afc_west[4] = "San Diego Chargers";

$afc = array($afc_east,$afc_west);
$count = 0;
foreach($afc as $arr){
        $total = count($arr);
        for($i=0;$i<=$total-1;$i++){
            $afc[$count][$i] = array_shift($arr);           
        }
        array_unshift($afc[$count],$afc[$count][0]);
        array_pop($afc[$count]);
        array_pop($afc[$count]);
        $count++;
}
//Output code to check if everything went correctly
foreach($afc as $arr){
    foreach($arr as $key=>$value){
            echo $key."->".$value."<br/>";
    }
}

?>

2 Comments

And how to get the name of the array? ex: "afc_east", "afc_west"
That is something that you would have to do manually. That is the only step actually. That is, creating an array of arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.