par
jojolapine » 09 juil. 2010, 09:54
Bonjour,
je n'ai pas trop compris pourquoi vouloir absolument garder la deuxième couche (id_user) dans ton résultat?
IL se peut qu'il y ai plusieurs utilisateurs par photo?
Bref voilà ce que ça donne:
<?php
$tab = array(
array(
'4774155148' => array
(
'service_id' => 84,
'flickrPicId' => '4774155148',
'photolink' => 'http://farm5.static.flickr.com/4096/477 ... 5ee3a4.jpg',
'photodate' => 1278584637,
'user_id' => 44
),
'4774146102' => array
(
'service_id' => 84,
'flickrPicId' => '4774146102',
'photolink' => 'http://farm5.static.flickr.com/4099/477 ... c5356c.jpg',
'photodate' => 1278584192,
'user_id' => 44
),
'4774099090' => array
(
'service_id' => 84,
'flickrPicId' => '4774099090',
'photolink' => 'http://farm5.static.flickr.com/4079/477 ... 70e2ce.jpg',
'photodate' => 1278581958,
'user_id' => 44
),
'4748347869' => array
(
'service_id' => 84,
'flickrPicId' => '4748347869',
'photolink' => 'http://farm5.static.flickr.com/4120/474 ... 29a760.jpg',
'photodate' => 1277902930,
'user_id' => 44
),
'4748347723' => array
(
'service_id' => 84,
'flickrPicId' => '4748347723',
'photolink' => 'http://farm5.static.flickr.com/4115/474 ... 939953.jpg',
'photodate' => 1277902926,
'user_id' => 44
)
),
array
(
'4723647573' => array
(
'service_id' => 84,
'flickrPicId' => '4723647573',
'photolink' => 'http://farm2.static.flickr.com/1175/472 ... dc60fe.jpg',
'photodate' => 1277208645,
'user_id' => 2
),
'4724299716' => array
(
'service_id' => 84,
'flickrPicId' => '4724299716',
'photolink' => 'http://farm2.static.flickr.com/1407/472 ... a35fd7.jpg',
'photodate' => 1277208643,
'user_id' => 2
),
'4723647485' => array
(
'service_id' => 84,
'flickrPicId' => '4723647485',
'photolink' => 'http://farm2.static.flickr.com/1344/472 ... be7bee.jpg',
'photodate' => 1277208642,
'user_id' => 2
)
)
);
$results=array();
// première couche
foreach($tab as $entry1){
// seconde couche
foreach($entry1 as $entry2){
// création de l'entrée avec l'id de la photo
if(!isset($results[$entry2['flickrPicId']])){
$results[$entry2['flickrPicId']] = array();
}
// création de l'entrée avec l'id utilisateur
if(!isset($results[$entry2['flickrPicId']][$entry2['user_id']])){
$results[$entry2['flickrPicId']][$entry2['user_id']] = $entry2;
}
}
}
var_dump($tab,$results);
N'hésite à demander plus d'explications

Bonjour,
je n'ai pas trop compris pourquoi vouloir absolument garder la deuxième couche (id_user) dans ton résultat?
IL se peut qu'il y ai plusieurs utilisateurs par photo?
Bref voilà ce que ça donne:[php]<?php
$tab = array(
array(
'4774155148' => array
(
'service_id' => 84,
'flickrPicId' => '4774155148',
'photolink' => 'http://farm5.static.flickr.com/4096/477 ... 5ee3a4.jpg',
'photodate' => 1278584637,
'user_id' => 44
),
'4774146102' => array
(
'service_id' => 84,
'flickrPicId' => '4774146102',
'photolink' => 'http://farm5.static.flickr.com/4099/477 ... c5356c.jpg',
'photodate' => 1278584192,
'user_id' => 44
),
'4774099090' => array
(
'service_id' => 84,
'flickrPicId' => '4774099090',
'photolink' => 'http://farm5.static.flickr.com/4079/477 ... 70e2ce.jpg',
'photodate' => 1278581958,
'user_id' => 44
),
'4748347869' => array
(
'service_id' => 84,
'flickrPicId' => '4748347869',
'photolink' => 'http://farm5.static.flickr.com/4120/474 ... 29a760.jpg',
'photodate' => 1277902930,
'user_id' => 44
),
'4748347723' => array
(
'service_id' => 84,
'flickrPicId' => '4748347723',
'photolink' => 'http://farm5.static.flickr.com/4115/474 ... 939953.jpg',
'photodate' => 1277902926,
'user_id' => 44
)
),
array
(
'4723647573' => array
(
'service_id' => 84,
'flickrPicId' => '4723647573',
'photolink' => 'http://farm2.static.flickr.com/1175/472 ... dc60fe.jpg',
'photodate' => 1277208645,
'user_id' => 2
),
'4724299716' => array
(
'service_id' => 84,
'flickrPicId' => '4724299716',
'photolink' => 'http://farm2.static.flickr.com/1407/472 ... a35fd7.jpg',
'photodate' => 1277208643,
'user_id' => 2
),
'4723647485' => array
(
'service_id' => 84,
'flickrPicId' => '4723647485',
'photolink' => 'http://farm2.static.flickr.com/1344/472 ... be7bee.jpg',
'photodate' => 1277208642,
'user_id' => 2
)
)
);
$results=array();
// première couche
foreach($tab as $entry1){
// seconde couche
foreach($entry1 as $entry2){
// création de l'entrée avec l'id de la photo
if(!isset($results[$entry2['flickrPicId']])){
$results[$entry2['flickrPicId']] = array();
}
// création de l'entrée avec l'id utilisateur
if(!isset($results[$entry2['flickrPicId']][$entry2['user_id']])){
$results[$entry2['flickrPicId']][$entry2['user_id']] = $entry2;
}
}
}
var_dump($tab,$results);
[/php]
N'hésite à demander plus d'explications ;)