Reputation: 163
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!
Upvotes: 0
Views: 464
Reputation: 163
$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
Upvotes: 0
Reputation: 148
$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")
)
)
);
Upvotes: 3