0

I have a simple PHP array inside an array which looks like this:

$defaults = array( "color" => "White", array( "color" => "0000CC", ) )

The problem becomes when I want to call the second color item--since they are both named the same, I can't figure out how to access the SECOND color item on the command line. This command line code below doesn't work because it only gets the FIRST color item in the first array.

php /Users/me/temp/the_Script/create.php --color='0000CC'

How do I access that SECOND color item in the second array on the command line? Can I even grab both of them in the same command line arguments?

Thanks!

1
  • please provide full code of create.php Commented Jan 30, 2014 at 15:08

2 Answers 2

0

You can access like this:

$defaults[0]["color"]

This is because when you create an associative array they don't have index number, so when you create the second array inside the defaults array, PHP assign that to the first position.

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

1 Comment

How would I then work with it at the command line in a format similar to "php /Users/me/temp/the_Script/create.php --color='0000CC'"
0

In this particular case

$defaults = array( "color" => "White", array( "color" => "0000CC", ) );    
var_dump($defaults[0]['color']);

But you're better off giving it an association

$defaults = array( "color" => "White", "codes" => array( "color" => "0000CC", ) );
var_dump($defaults['codes']['color']);

The general structure of your array doesn't make any sense to me though. I don't know if that's because you're just using it as an example or if you need to rethink the structure.

3 Comments

I think I missed a line in my example above: $defaults = array( "color" => "White", "layers" => array( array( "color" => "0000CC", ) )
On the command line I can say php /Users/me/temp/the_Script/create.php --color='0000CC', but, I want to be able to change the second color argument on the command line and not change the first instance.
try php /Users/me/temp/the_Script/create.php defaults[layers][color]='0000CC' : See stackoverflow.com/questions/2872755/…

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.