473,796 Members | 2,632 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

create array from members of an array of objects

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?

I tried with:

array_walk($lis t,create_functi on('$a,$b,$resu lt','$result[] =
$a->member2;'), &$result);

but I get the following error:

Warning: Call-time pass-by-reference has been deprecated - argument
passed by value; If you would like to pass it by reference, modify the
declaration of array_walk(). If you would like to enable call-time
pass-by-reference, you can set allow_call_time _pass_reference to true in
your INI file. However, future versions may not support this any longer.

Thank you
Regards
Feb 7 '07 #1
10 1596
Rik
Generale Cluster <al**@carraroso ftmasters.netwr ote:
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($lis t,create_functi on('$a,$b,$resu lt','$result[] =
$a->member2;'), &$result);
Tssk, single line, but a create_function ()... That's cheating :P

$newlist = array_map(creat e_function('$v' ,'return $v->member2'),$lis t);

--
Rik Wasmus
Feb 7 '07 #2
Generale Cluster 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?

I tried with:

array_walk($lis t,create_functi on('$a,$b,$resu lt','$result[] =
$a->member2;'), &$result);

but I get the following error:

Warning: Call-time pass-by-reference has been deprecated - argument
passed by value; If you would like to pass it by reference, modify the
declaration of array_walk(). If you would like to enable call-time
pass-by-reference, you can set allow_call_time _pass_reference to true in
your INI file. However, future versions may not support this any longer.

Thank you
Regards

Try:

array_walk($lis t,create_functi on('$a,$b,&$res ult','$result[] =
$a->member2;'), $result);

Specify the reference in the parameter list, not when passing the value.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Feb 7 '07 #3
Rik wrote:
Generale Cluster <al**@carraroso ftmasters.netwr ote:
>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($li st,create_funct ion('$a,$b,$res ult','$result[] =
$a->member2;'), &$result);

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

$newlist = array_map(creat e_function('$v' ,'return $v->member2'),$lis t);

--Rik Wasmus
Rik,

Sounds like a homework assignment to me... :-)
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Feb 7 '07 #4
"Generale Cluster" <al**@carraroso ftmasters.netwr ote in message
news:eq******** **@newsreader.m ailgate.org...
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.
foreach($list as $foo) $newlist[] = $foo->member2;
I tried with:

array_walk($lis t,create_functi on('$a,$b,$resu lt','$result[] =
$a->member2;'), &$result);
"Gloves".
http://thedailywtf.com/Articles/The_Complicator 's_Gloves.aspx

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi. net | rot13(xv***@bhg byrzcv.arg)
Feb 8 '07 #5
Jerry Stuckle ha scritto:
Rik wrote:
>Generale Cluster <al**@carraroso ftmasters.netwr ote:
>>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($l ist,create_func tion('$a,$b,$re sult','$result[] =
$a->member2;'), &$result);

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

$newlist = array_map(creat e_function('$v' ,'return $v->member2'),$lis t);

--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
fillHTMLSelectM ultiple($aValue s,"author[]","",array_map( create_function ('$a','return
$a->author_id;'),$ resource->aAuthors),'siz e="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!




Feb 9 '07 #6
Generale Cluster wrote:
Jerry Stuckle ha scritto:
>Rik wrote:
>>Generale Cluster <al**@carraroso ftmasters.netwr ote:

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_fun ction('$a,$b,$r esult','$result[] =
$a->member2;'), &$result);

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

$newlist = array_map(creat e_function('$v' ,'return $v->member2'),$lis t);

--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
fillHTMLSelectM ultiple($aValue s,"author[]","",array_map( create_function ('$a','return
$a->author_id;'),$ resource->aAuthors),'siz e="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.
js*******@attgl obal.net
=============== ===
Feb 9 '07 #7
Jerry Stuckle ha scritto:
Generale Cluster wrote:
>Jerry Stuckle ha scritto:
>>Rik wrote:
Generale Cluster <al**@carraroso ftmasters.netwr ote:
[CUT]
>
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.
[CUT]

comment, comment, comment and comment again :-)

bye!





Feb 14 '07 #8
Rik
On Wed, 14 Feb 2007 02:12:32 +0100, Generale Cluster
<al**@carraroso ftmasters.netwr ote:
Jerry Stuckle ha scritto:
>Generale Cluster wrote:
>>Jerry Stuckle ha scritto:
Rik wrote:
Generale Cluster <al**@carraroso ftmasters.netwr ote:
[CUT]
> 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.
[CUT]

comment, comment, comment and comment again :-)
Commenting in the single line?
Just tell them to leave everything between <?php ?alone and it should
be OK, no reason to forced single line statements. About that, if you're
worried about that, then why not just code it on the single line on normal
code? Linebreaks aren't actually required in PHP, so you could just leave
them in, and when the code needs to be updated you can whip it into some
kind of formatted readable shape quite fast with any competent texteditor.

But the main reason for me posting here in this thread is about
commenting: I'd like to take the oppertunity advocate the user of the /x
modifier for regexes. These pesky little things are often forgotten in the
commenting, while it's so easy.

Compare (not a very useful regex, just an illustration):

$regex = '/(a|b){2,4}([^\s]+?)c{2,4}(.*?)q/si'

And
$regex = '/{a|b}{2,4} #start matching at 2 to 4 a's or b's in a row
([^\s]+?) #capture all non whitespace untill the next match
c{2,4} #match 2 to 4 c's
(.*?) #capture any character
q #until q
/six'; //case-insensite, with comments

Now, that will be a lot easier altered and understood by coworkers I'd
imagine..
--
Rik Wasmus
Feb 14 '07 #9
Generale Cluster wrote:
Jerry Stuckle ha scritto:
>Generale Cluster wrote:
>>Jerry Stuckle ha scritto:
Rik wrote:
Generale Cluster <al**@carraroso ftmasters.netwr ote:
[CUT]
>>
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.
[CUT]

comment, comment, comment and comment again :-)

bye!
Yes, I agree code should be commented. But it still should be easy to
understand.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Feb 14 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3397
by: J. Campbell | last post by:
I'm comfortable with arrays from previous programming, and understand the advantages of c++ vectors...I just don't understand how to use them :~( Can you help me to use a vector<string> in the following compilable example instead of the string* array? Thanks, Joe #include <iostream>
10
1550
by: Thomas Mlynarczyk | last post by:
Hi, For objects (non-arrays) I can use for(i in object) to loop through all its members. For arrays, this does not always seem to work, and I am forced to use for(i=0; i<array.length;i++) instead. Is there a way to make an array accessible using the for-in loop? During my experiments I found that if the array is a member of an object, but not the first one, the for-in will work... seems a strange behaviour to me, so I'd be grateful if...
47
5094
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they are not at all. If you are doing *array", then you have to use only integer values for array index, as it was since ALGOL.
67
4489
by: Ike Naar | last post by:
Hi, Asking your advice on the following subject: Suppose I want to find out whether a given pointer (say, p) of type *T points to an element of a given array (say, a) of type T. A way to achieve this would be a linear search through the array: int ptrInArrSafe(T *p,T *a,int max) /* check whether p points to an element of a */
2
1278
by: Crirus | last post by:
I have some question related to the other post about iteration on a class members. I had found a way to figure out when a member of my class is array so I can pull out it's elements to iterate on that classes members, recursively I designed a Strong type class, inherited from CollectionBase. My class have a member of that type, so it contain lots of references fo objects of that strong type. Is there a general way to figure out that a...
11
6735
by: motion musso aka: sathia | last post by:
this is it, how can i get the current index? couldn't figure it out. thank you for(i=0;myarray.length<i;i++){ do_something(); //i need the current myarray } bye bye
10
5620
by: Raj | last post by:
I need a VB function to return array of collections like Private Type Employee empname as string address as string salary as integer deptno as integer End Type dim employees() as Employee
31
3238
by: siddhu | last post by:
why can't we have array of references.
4
1700
by: Ronald Raygun | last post by:
I want to store objects in ana array and then iterate through the array, retrieving a (reference) to each object in the array, and calling a method on the array. I am not sure how to do it, but this is pseudocode of what I want to do: <?php $m_fieldItems = createObjects(); $outstr = ''; for($i=0; $i < count($m_fieldItems); $i++)
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9533
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10461
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10019
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9057
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7555
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6796
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.