SV: [PHPTAL] Table with two array problem
Piotr Kroczyński
piotr.kroczynski at gmail.com
Tue Jun 28 17:50:21 CEST 2011
try this:
$tmpArray = array();
foreach($otherArray as $a){
$tmpArray[$a['idoffer']] = $a;
}
$otherArray = $tmpArray();
unset($tmpArray);
Pozdrawiam
Piotr Kroczyński
W dniu 28.06.2011 17:41, Teis Lindemark pisze:
> Okey, thanks for the quick answer.
>
> But my otherArray is at format Array([0] => Array([idoffer] => INTEGER
> [name] => STRING) [1] ...
>
> I think the reason is that one idoffer can have more than one name, but is
> there an easy way to format that array correct or get this work on this
> array?
>
> Thanks for the answer so far ;)
>
> Teis
>
> -----Opprinnelig melding-----
> Fra: phptal-bounces at lists.motion-twin.com
> [mailto:phptal-bounces at lists.motion-twin.com] På vegne av Robert Goldsmith
> Sendt: 28. juni 2011 17:06
> Til: Template Attribute Language for PHP
> Emne: Re: [PHPTAL] Table with two array problem
>
> I think I understand what you are saying. The easiest way to do this would
> be if 'idoffer' is the key for the second array (so you have an array of
> key/value pairs with the idoffer as the key and name as the value). Then you
> can effectively ask 'give me the value for this key I am giving you or, if
> the key doesn't exist then please give me a default value'.
>
> Imagine the following:
>
> $jobOffers = array(
> array('idOffer' => 'a', 'name' => 'AAA'),
> array('idOffer' => 'b', 'name' => 'BBB'), );
>
> $otherArray = array(
> 'a' => array('name' => 'AaAa'),
> 'c' => array('name' => 'CcCc'),
> );
>
> <table>
> <tr tal:repeat="offers jobOffers">
> <td tal:content="offers/name' />
> <td tal:content="otherArray/${offers/idOffer}/name |
> string:no match;" />
> </tr>
> </table>
>
> The important part here is the ${offers/idOffer} part which is replaced by
> PHPTal *before* it then tries to access the value from otherArray. For the
> first loop through, offers/idOffer is equal to 'a' so PHPTal tries to access
> otherArray/a/name which succeeds while on the second loop through it tries
> otherArray/b/name which doesn't exist so PHPTal moves on to the next option
> in the value chain (using the | character) and instead picks up the string
> 'no match'.
>
> I hope this is what you were trying to do :)
>
> If you need something more like 'in_array' where you need to hunt through
> the values in otherArray to find a match you will need to write a Tale but
> I'd also question the efficiency of doing that and would suggest trying to
> use array keys where possible :)
>
> Robert
>
> On 28 Jun 2011, at 15:38, Teis Lindemark wrote:
>
>> Hi,
>>
>> I am new to use PHPTal and so far, I like it. It’s great to separate php
> code and the xhtml code.
>>
>> One of my list need some data from two arrays. I have one array array1
> that I use tal:repeat=”offers jobOffers” and find the content like this:
> tal:content=”offers/name” for example. One of the columns in the table have
> the data in another array, with two columns idoffer and name. The first
> array also have idoffer (offers/idoffer). But what I need here is that
> otherarray/name is placed in the row where offers/idoffer and
> otherarray/idoffer is equal.
>>
>> I can’t figure this out and have been stuck here for a while.
>>
>> Hope someone can help me with this one.
>>
>> Teis
>> _______________________________________________
>> PHPTAL mailing list
>> PHPTAL at lists.motion-twin.com
>> http://lists.motion-twin.com/mailman/listinfo/phptal
>
> _______________________________________________
> PHPTAL mailing list
> PHPTAL at lists.motion-twin.com
> http://lists.motion-twin.com/mailman/listinfo/phptal
>
>
> _______________________________________________
> PHPTAL mailing list
> PHPTAL at lists.motion-twin.com
> http://lists.motion-twin.com/mailman/listinfo/phptal
>
More information about the PHPTAL
mailing list