0

currently this is my one dimensional array looks like

 [0] => Spiderman 
 [1] => Burger 
 [3] => Batman 
 [4] => Robin

now i want to add another key at each array value, for example

 [0]['isMovie'] = 'Yes'
 [1]['isMovie'] = 'No'
 [2]['isMovie'] = 'Yes'
 [3]['isMovie'] = 'Yes'

so that next time when i iterate through the array, i will know which value is a movie or not.

i tried something like [0]['isMovie'] = 'Yes' but it seems it just changed the current value of the array to something like Ypiderman

1
  • you can't have your array values be both a string and an array at the same time. You need an array of objects/structs/arrays. Commented Jan 11, 2013 at 18:56

3 Answers 3

1

Your array structure could look like this:

array(
    array(
        'Name' => 'Batman',
        'isMovie' => true,
    ),
    array(
        'Name' => 'phpisuber01 is awesome',
        'isMovie' => false,
    ),
);

You would access the values by doing something like this:

echo $var[0]['Name']; //returns Batman
Sign up to request clarification or add additional context in comments.

Comments

1

you'll need to create a name index. You will have a 2 dimensional array like this:

$arr = array();
$arr[] = array('name'=> 'SpiderMan', 'isMovie'=> 'Yes');
$arr[] = array('name'=> 'Burger', 'isMovie'=> 'No');
$arr[] = array('name'=> 'Batman', 'isMovie'=> 'Yes');
$arr[] = array('name'=> 'Robin', 'isMovie'=> 'Yes');

Comments

1
$array[0] = array('0' => array[0], 'isMovie' => 'Yes');
$array[1] = array('1' => array[1], 'isMovie' => 'No');

... and so on

Comments

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.