Skip to content
Advertisement

php foreach array id value

i have array:

array( id1: title_of_id_1, id2: title_of_id_2, id3: title_of_id_2, )

How to foreach get array only list id1, id2, id3?

Advertisement

Answer

You didn’t provided proper source code of your array. So I couldn’t understand properly I think, still I’m trying to answer.

If your ID number and text both are stored in $list array values, like this –

$list = array( ‘id1:text 1’, ‘id2:text 2’, … );

then you could do something like this

$idArr= array();

foreach ($list as $li) {
    $id = explode(":", $li)[0]; //first value before : sign
    //$id = trim($id); //to remove spaces if any (optional)
    array_push($idArr, $id);
}


print_r($idArr); //all your ids

If your array is like this

$list = array( id1 => ‘text 1’, id2 => ‘text 2’, … );

Then you can do like this

$idArr = array_keys($list);

print_r($idArr); // your ids
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement