You are here: Re: [PHP] joining array values « PHP « IT news, forums, messages
Re: [PHP] joining array values

Posted by Merlin on 05/12/05 17:13

Brent Baisley wrote:
> 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
>>
>>

Hi Brent,

thank you for that excellent example. I tried to get that working in a similar
way but failed. However, you brought in knowledge which puts me into trouble.
How can I output those arrays if they are the other way around?
I used to do it like this:
for ($i=0;$i<count($result[id]);$i++){
echo $result[id][$i];
echo $result[text][$i];
}

Now the array is the other way around, plus the array $resultSummary[$id]['id']
is not a 1,2,3 thing but there are missing values?!
I need those Id's to create proper links from the output.

Can you shed some more light on this?

Thank you for your excellent help.

Best regards, Merlin

 

Navigation:

[Reply to this 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

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