Retrieving only one subset from an array based on ID


Hi there everyone!

I’ve got an array that looks like this:

    [0] => Array
            [id] => 1
            [name] => SESX Demo
            [safename] => sesx-demo
            [ip] =>
            [port] => 30120
            [dbhost] =>
            [dbname] => c1
            [dbuser] => schwim
            [dbpass] => pass
            [framework] => sesx

    [1] => Array
            [id] => 2
            [name] => Testing really long server name
            [safename] => testing-server
            [ip] =>
            [port] => 30120
            [dbhost] => localhost
            [dbname] => fivem
            [dbuser] => root
            [dbpass] => 
            [framework] => custom

    [2] => Array
            [id] => 3
            [name] => Scotch & Iron
            [safename] => scotch-iron
            [ip] =>
            [port] => 30120
            [dbhost] =>
            [dbname] => general
            [dbuser] => user
            [dbpass] => pass
            [framework] => esx


And I need to work with just one of the server ID’s at a time To explain better, as an example, I want to get all the vars for ID1 named SESX Demo… I tried to do a while inside the foreach loop to try to do it but that causes an infinite page load:

$server_id = ‘1’, so I had hoped my while would retrieve all the necessary vars while in that part of the loop.

foreach($serverlist AS $server){
	while($server['id'] == $server_id){
		$server_name 		= $server['name'];
		$server_safename	= $server['safename'];
		$server_host		= $server['host'];
		$server_port		= $server['port'];
		$server_dbhost		= $server['dbhost'];
		$server_dbname		= $server['dbname'];
		$server_dbuser		= $server['dbuser'];
		$server_dbpass		= $server['dbpass'];
		$server_framework	= $server['framework'];

What’s the best way to handle retrieving the data of only one subset of the array based on the value of one of the subset’s vars?


oh man, I’m dense.


changed to


got it working. Is this the best way to handle this?


Well, I think I’ve made it a little bit cleaner. I am sure it could be better though.

foreach($serverlist AS $key => $val){
	if($val['id'] == $server_id){
		foreach($val AS $k => $v){
			${'server_'.$k}	= $v;

echo 'server is: '.$server_name;


if the array is named $serverlist you can access each element in the first array like so


etc, etc…

There is no need to jump through loops or create variables for nothing.


But it won’t always be in position [0] and the server id won’t be in any particular relationship to that position either. If I need server with an ID of 7, I’ll have no idea where in the array it is. That’s why I did the loop(s).


Perhaps you should tell us about the real problem instead of asking about your attempt in solving it.

Why would you possibly need 7 server configs in one application?


When you build the array, couldn’t you build it in a different way such that the primary index is your “id” value?


Heya droop and thanks for the help!

That would definitely be more elegant but I haven’t figured out how to format a PDO query’s result yet.


PDO has a lot (like really a lot) different fetch modes where you can even combine them. What might work here is esp. PDO::FETCH_UNIQUE.