Reply to Re: [PHP] joining array values

Your name:

Reply:


Posted by Brent Baisley on 05/12/05 16:15

Use the id number as the array key and a simple loop should join
everything together.
for($i=0; $i<count($result['id']); $i++) {
$id = $result['id'][$i];
$text = $result['text'][$i];
$relevance = $result['relevance'][$i];

$resultSummary[$id]['id'] = $id;
if ( isset($resultSummary[$id]['text']) ) {
$resultSummary[$id]['text'] .= ' '.$text;
} else {
$resultSummary[$id]['text'] = $text;
}
if ( isset($resultSummary[$id]['rel']) ) {
$resultSummary[$id]['relevance'] += ' '.$relevance;
} else {
$resultSummary[$id]['relevance'] = $relevance;
}
}

You actually wouldn't need to have any entry for id in the resulting
array since the id is the key for the array "row".

But you setup your array backwards, and you have a one multidimensional
array, not three arrays. With your setup, if there is a value missing
from any of the three "categories", then your data is out of sync.
Arrays are great for creating name/value pairs. But you should view a
two dimensional array as a row/column setup: $result[row][column]
So your array should be setup like:
$result[0]['id']
$result[0]['text']
$result[0]['relevance']
$result[1]['id']
$result[1]['text']
$result[1]['relevance']
....

This assures your data aligns properly and it's actually easier to
process in a loop.


On May 12, 2005, at 7:30 AM, Merlin wrote:

> Hi there,
>
> I do have a tricky problem with arrays.
>
> There are 3 arrays:
>
> $result[id][]
> $result[text][]
> $result[relevance][]
>
> Now there might be the case that there are more results with the same
> $id. I would like to count all the relevances together for the same
> id.
>
> For example:
> id text relevance
> 23 ok 2
> 42 joel 1
> 23 php 1
>
> Desired output:
> id text relevance
> 23 ok php 3
> 42 joel 1
>
> Has anybody an ide how to do that?
>
> Thank you for any help,
>
> Merlin
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
Brent Baisley
Systems Architect
Landover Associates, Inc.
Search & Advisory Services for Advanced Technology Environments
p: 212.759.6400/800.759.0577

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация