too long

I want to create function that populates from a list of static values.

Example I want to look it like this;

Array
(
[0] => Array
    (
        [id] => 1
        [name] => CPU
    )

[1] => Array
    (
        [id] => 2
        [name] => Mouse
    )

[2] => Array
    (
        [id] => 3
        [name] => Keyboard
    )
)

I have this simple code created, but not that much:

<?php
echo "<pre>";
$pcparts=array
(
    array(01,"CPU"),
    array(02,"Mouse"),
    array(03,"Keyboard")
);
print_r($pcparts);
?>

But what if I have large numbers of values/data? I want only to loop it via how many data is listed in $pcparts.

How can I add those index? id and name.

I want to create only function. I think foreach is useful here.

From where are those values/data ? From what I see, it's only static content that you statically put in your array. In that case, your way of doing is already as good as it can get.

If your issue is that you want to minimize the space it takes in your file to populate your array, you may try with a coma separated string :

First, you line up your PC parts in a coma separated string

$pcparts = "CPU, Mouse, Keyboard, whatever";
$pcparts = explode(', ', $pcparts);

And there you have your array. You can also replace $pcparts in your explode with the string directly if you want it to be even thinner, as you wish.

Are you talking about something like this?

$pcparts = [
    ['id'=> 1, 'name' => 'CPU'],
    ['id'=> 2, 'name' => 'Mouse'],
    ['id'=> 3, 'name' => 'Keyboard']
];

print_r($pcparts);