0

I want to make an array like the one below:

$database = array(
 array(
 "key" => "Key1",
 "hwid" => "Hwid1"
 ),
 array(
 "key" => "Key2",
 "hwid" => "Hwid2"
 ),
);

How would I go about making this by inserting values. Here is what I have tried:

$array = array();
$array[array()["key"]] = "Key1";
$array[array()["hwid"]] = "HWID1";

Sadly the above code does not make the structure of the array that I wanted. How would I achieve this?

asked Dec 22, 2020 at 5:34

3 Answers 3

1
$array = [];
$array[] = ["key" => "Key1","hwid" => "Hwid1"];
$array[] = ["key" => "Key2","hwid" => "Hwid2"];

or

$array = [
 ["key" => "Key1","hwid" => "Hwid1"],
 ["key" => "Key2","hwid" => "Hwid2"]
];
answered Dec 22, 2020 at 5:36
1
$array = array();
$arr1 = ["id" => "id1", "hwid" => "hwid1"];
$arr2 = ["id" => "id2", "hwid" => "hwid2"];
array_push($array, $arr1, $arr2);
var_dump($array);

You don't need array index to be an array

answered Dec 22, 2020 at 5:42
0

Your parent array is not associative, the only way you can access to it is by int index which starts with 0:

try this:

$array = array();
$array[0]["key"] = "Key1";
$array[0]["hwid"] = "HWID1";
answered Dec 22, 2020 at 5:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.