Current location: Hot Scripts Forums » Programming Languages » PHP » PHP Array Mapping challenge


PHP Array Mapping challenge

Reply
  #1 (permalink)  
Old 02-03-10, 09:40 AM
carters-site's Avatar
carters-site carters-site is offline
Wannabe Coder
 
Join Date: Sep 2009
Location: Moline, IL
Posts: 100
Thanks: 2
Thanked 1 Time in 1 Post
Question PHP Array Mapping challenge

Got a rather quick one for you guys if you got time.

I am looking for a 1 liner PHP solution to do this. (not sure if it is possible through the array functions)

Basically I want to remap an array based on the one item present in each.

Example

array(
[0] => array(
[id] => 1234,
[name] => blah
[properties] => whatever
),
[1] => array(
[id] => 5678,
[name] => blah2
[properties] => foobar
)
);

I want to take this array and map it to

array( [1234] => array(
[name] => blah,
[properties] => whatever
),
[5678] => array(
[name] => blah2
[properties] => foobar
));

Basically remapping the array to one of the items present in each element. I am currently using a tempArray and looping through and this works fine but I was curious if there was a 1 line way using the array functions (possibly combining a few) to make a one line php call to do the same thing.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 02-03-10, 09:59 AM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
PHP: array_walk - Manual

Should be able to create a custom function that takes the first element of the subarray and makes it the key of the parent.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 02-03-10, 11:06 AM
carters-site's Avatar
carters-site carters-site is offline
Wannabe Coder
 
Join Date: Sep 2009
Location: Moline, IL
Posts: 100
Thanks: 2
Thanked 1 Time in 1 Post
That does not seem like a one line solution.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 02-03-10, 12:45 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
PHP Code:

<?php

$a
=array(array('id'=>1234,'name'=>'blah','properties'=>'whatever'),
        array(
'id'=>5678,'name'=>'blah2','properties'=>'foobar'));
$b=array();
var_dump($a);
array_walk($a,'c');function c($v,$k){global $b;$b[$v['id']]=$v;unset($b[$v['id']]['id']);}
var_dump($b);
?>

Quote:
array(2) {
[0]=>
array(3) {
["id"]=>
int(1234)
["name"]=>
string(4) "blah"
["properties"]=>
string(8) "whatever"
}
[1]=>
array(3) {
["id"]=>
int(5678)
["name"]=>
string(5) "blah2"
["properties"]=>
string(6) "foobar"
}
}
array(2) {
[1234]=>
array(2) {
["name"]=>
string(4) "blah"
["properties"]=>
string(8) "whatever"
}
[5678]=>
array(2) {
["name"]=>
string(5) "blah2"
["properties"]=>
string(6) "foobar"
}
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 02-03-10, 03:54 PM
carters-site's Avatar
carters-site carters-site is offline
Wannabe Coder
 
Join Date: Sep 2009
Location: Moline, IL
Posts: 100
Thanks: 2
Thanked 1 Time in 1 Post
Thumbs up

I stand corrected.
LOL!

Thanks wire. I actually am going to use that, because it's a little more obfuscated and if anyone else ever has to look at my code I can imagine a good wtf coming their mouth. I do enjoy those WTFs for sometimes causing your fellow programmers a little pain during the day relieves a little stress.

Mark this as solved.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks

Tags
array functions, array mapping


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript Array In PHP deekshas PHP 1 11-12-09 10:01 AM
PHP Multidimensional Array and Cookies indigo001 PHP 2 10-15-09 05:32 PM
[SOLVED] it works on localhost but not on hosting account? myslowquietlife PHP 42 12-19-08 10:31 AM
2 profitable script sites for sale cms-master.com General Advertisements 3 07-03-07 11:17 AM
linking to iframe not working :( j0d JavaScript 5 01-19-04 09:14 PM


All times are GMT -5. The time now is 03:16 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.