0

How to define variable inside array without key? This doesnt working and I dont know how...

$array = array("list" => array());

$list = $array["list"][] = array("sub_list" = array());
$list["sub_list"][] = "text1";
$list["sub_list"][] = "text2";
$list["sub_list"][] = "text2";

$list2 = $array["list"][] = array("sub_list" = array());
$list2["sub_list"][] = "text1";
$list2["sub_list"][] = "text2";
$list2["sub_list"][] = "text3";

Needed result:

$array = array(
   "list" => array(
      array(
        "sub_list" = array("text1", "text2", "text3")
      ),
      array(
        "sub_list" = array("text1", "text2", "text3")
      )
    )
);

It's not used in loop or for/foreach!

6
  • 2
    Is there any reason why you don't want to declare the array using the second method? Commented Jun 20, 2019 at 18:00
  • 1
    There is a typo "sub_list" = array() it should be => Commented Jun 20, 2019 at 18:01
  • @Dharman my mistake, ofc with => but same problem - sub_list are always empty arrays Commented Jun 20, 2019 at 18:05
  • Your question isn't clear. Can you rephrase the problem, please? Commented Jun 20, 2019 at 18:13
  • @NigelRen second method? Commented Jun 20, 2019 at 19:06

2 Answers 2

3
$array = [
    'list' => []
];

$list = [];
$list[] = 'text1';
$list[] = 'text2';
$list[] = 'text3';
$array['list'][]['sub_list'] = $list;

$array['list'][]['sub_list'] = $list;

$list = [];
$list[] = 'text4';
$list[] = 'text5';
$list[] = 'text6';
$array['list'][]['sub_list'] = $list;

And you will have :

$array = array(
   "list" => array(
      array(
        "sub_list" => array("text1", "text2", "text3")
      ),
      array(
        "sub_list" => array("text1", "text2", "text3")
      ),
      array(
        "sub_list" => array("text4", "text5", "text6")
      )
    )
);
Sign up to request clarification or add additional context in comments.

3 Comments

code working as I wrote, but impossible add something to exists array with $array['list'][]["SOMETHING_NEXT"] is created new array, show my request below... thanks
I don't know how your script work or need to work only with your question. So you need to store the index when you create a new list array. Then you can use the index to add something like $array['list'][0]['SOMETHING_NEXT'] as same deep as sub_list
I think something like $var = $array['list'][] but its maybe impossible, okay... index $i++; and $array['list'][ $i ]['SOMETHING_NEXT'] how to identify key
0
$array["list"][] = array("sub_list" => array());
$list= [];
$list[] = array("text1", "text2", "text3");
$list[] = array("text1", "text2", "text3");
$array["list"][]["sub_list"] = $list;

althought it's array inside array and this array is also in array

i need $array["list"][] as variable, when is called $array["list"][]["sub_list"] = $list; is created new one array, i need add "sub_list" to first array

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.