Forum Moderators: coopster

Message Too Old, No Replies

a bit different array_intersect or array_map

         

mcibor

10:30 pm on Jun 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi I have two arrays. One array is sort array with db names:
$sort = array("vname", "sname");

second array is the whole text to display for user:
$names = array("user_id"=>"Id of the user", "vname"=>"Vorname", "sname"=>"Surname", "tbl_name"=>"Name of the table");

What I need is to merge somehow these tables. Table sort is generated by the user and may only incorporate from 1 to 8 fields

I need the output array to be corresponding to both of them:

$output = array("vname"=>"Vorname", "sname"=>"Surname");

however I don't know how to do that. At first I thought, that $output = $names[$sort]; will do the thing,m but it didn't. I couldn't also find anything at www.php.net , however function array_map looks promising, but it makes an array of results' arrays (I want just one level).

Thanks for any answer
Michal Cibor

Timotheos

3:38 pm on Jun 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's a brute force method.

$sort = array("vname", "sname");
$names = array("user_id"=>"Id of the user", "vname"=>"Vorname", "sname"=>"Surname", "tbl_name"=>"Name of the table");

$output = array();

foreach ($sort as $keyname) {

$output[$keyname] = $names[$keyname];

}

You might want to check for the validity of $names[$keyname] depending on your setup.

Hope this is it.
Tim

mcibor

7:39 pm on Jun 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks!

I thought there might be a simple function to do that.

regards!
Michal Cibor