Re: create array from members of an array of objects



Generale Cluster wrote:
Jerry Stuckle ha scritto:
Rik wrote:
Generale Cluster <alex@xxxxxxxxxxxxxxxxxxxxxx> wrote:

Hello,
I have the following situation:

$list[] is an array of MyElement objects.

MyElement has two members: MyElement->member1; MyElement->member2;

What I want is to get the following:

$newlist[] so that:

$newlist[0]=$list[0]->member2;
$newlist[1]=$list[1]->member2;
$newlist[2]=$list[2]->member2;
...

I need to do this using A SINGLE LINE OF CODE.
Is it possible? How to do this?

Nothing _needs_ to be done in a single line of code, and for some actions you shouldn't even want it for readability.

I tried with:

array_walk($list,create_function('$a,$b,$result','$result[] = $a->member2;'), &$result);

Tssk, single line, but a create_function()... That's cheating :P

$newlist = array_map(create_function('$v','return $v->member2'),$list);

--Rik Wasmus

Rik,

Sounds like a homework assignment to me... :-)



Hi, thanks to Rik for the answer; it's exactly what I intended.
It's not a homework assignment, I simply want to write code which is easy to integrate with HTML. I'm working together with another person who is not a programmer: he's an HTML author and I need to make the code structure as essential as possible. I've coded my own generic tag library which generates html code from data and in this particular case I have a function which build a "select multiple" HTML tag.
So, in my html page I have the following line:

<?php fillHTMLSelectMultiple($aValues,"author[]","",array_map(create_function('$a','return $a->author_id;'),$resource->aAuthors),'size="5"')?>

- $aValues is an array containing the key-value couples to be displayed in the select.
- "author[]" is the name and id of the HTML tag
- array_map(...) contains an array the keys which have to appear preselected in the select control.
- $resource is an object which is loaded by a self-made CRUD handler and contains a member variable which is an array of "MyElement" objects (where member2 is the authorId).

This way, my friend, who does not know php, is able to move the select anywhere on the page by simply moving the php code line, and I do not risk to have my code corrupted by a mistake by my friend.
I know I could build my array of selected values previously in my code, but I got the question in my mind and I physiologically NEEDED to have an answer :-)

I don't know if this may be called "extreme programming" ?
I'd rather call it "academic fury" :-D

bye!



That's the difference. I prefer code which is easily understandable.

You had trouble getting it to work in the first place. What happens when you have to look at it six months or a year from now when you have to work on it again? You'll go through all of his again to understand what it does.

If you want it to be a single line, make it a function call. But even if it's multiple lines he should be able to copy and paste.

I have a similar setup for one of my customers. They maintain the content, I maintain the (interspaced) code. They even build entirely new pages by copying/pasting sections of html and code from other pages.

Sure, once in a while they have a problem - but not often. But they're saving a bunch of money over having to pay me to make the frequent content changes.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.



Relevant Pages

  • Re: Methods of marking a menu with the current page.
    ... Would it be normal to have HTML tags inside a PHP script when they ... In a PHP page only the bits between the are php code. ... An array of 6 items called "menu", ... So, on the first loop $key contains "/index.php", $location ...
    (alt.html)
  • Re: Form Data -> Variables or an Array?
    ... Forgive me I am a PHP newbie. ... I have a small script that enables me to ... I want to use the HTML formatted form ... variables or an array, so that I can call the data later in the same ...
    (comp.lang.php)
  • Re: print "...."; Question
    ... I thought it would be better to have the entire array in php but now I am not sure if that makes sense. ... I don't use print for straight HTML - I just code the HTML and go into PHP when I need it. ... You don't need two print statements and an echo here. ...
    (comp.lang.php)
  • Re: create array from members of an array of objects
    ... $listis an array of MyElement objects. ... I'm working together with another person who is not a programmer: he's an HTML author and I need to make the code structure as essential as possible. ... $aValues is an array containing the key-value couples to be displayed in the select. ... This way, my friend, who does not know php, is able to move the select anywhere on the page by simply moving the php code line, and I do not risk to have my code corrupted by a mistake by my friend. ...
    (comp.lang.php)
  • Re: nested conditional that can identify parent page?
    ... and specify the files in the array including ... try to make as little php and html mix as possible, have a template ...
    (alt.php)