-2

Is there any way to display an array as a value of one array value only.

For example,

Array:
[1] = some value
[2] = an array of values
[3] = some value

$main_array = array(
    [] => 11,
    [] => 22,
    [] => $sub_array = array([] => a, [] => b, [] => c,);
    [] => 44,
);
var_dump($main_array);

Returns parse error -> this part is invalid:

[] => $sub_array = array([] => a, [] => b, [] => c,);
3
  • Okay, good luck with that, now what is your question for us? Commented Oct 5, 2017 at 11:30
  • If I make an array and try to assign multiple values to one of the values it errors out... is there another way to achieve this? Commented Oct 5, 2017 at 11:35
  • 1
    Did you try anything? Add it! if it shows an error, add it! Commented Oct 5, 2017 at 11:49

1 Answer 1

0

Your code to declare the $main_array array is riddled with syntax errors:

  1. You can't use an array of any kind as a key in an associative array. PHP: Can an array have an array as a key in a key-value pair?
  2. The $sub_array inside $main_array needs to use quotes around string values like this: ['a', 'b', 'c'].
  3. You absolutely can't separate elements of an array with a semicolon ; Use a comma ,

I think you want a multidimensional array:

A multidimensional array is an array containing one or more arrays.

So, to write your array in that format:

$main_array = [
    11,
    22,
    [
        'a',
        'b',
        'c',
    ],
    44,
];
Sign up to request clarification or add additional context in comments.

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.