array of array

0 views
Skip to first unread message

jr

unread,
Jun 9, 2010, 6:30:40 PM6/9/10
to PHP Programming
hi everyone !
I have a basic problem, I know, sorry, but I am coming back to
programming, and needs to learn again php :(

so, I have :
==============code================================
$fan_by_url = array();

$fan_by_url[$url] = array($date_fans['date']=>$date_fans['fans']);
print_r($fan_by_url);

$fan_by_url[$url] = array($date_fans['date']=>$date_fans['fans']);
print_r($fan_by_url);
=================end code==============================
wich give me :
=================output==============================
Array
(
[http://www.facebook.com/21cMuseumHotels?v=wall] => Array
(
[2010-05-31 06:20:55] => 3538
)

)
Array
(
[http://www.facebook.com/21cMuseumHotels?v=wall] => Array
(
[2010-05-31 06:20:55] => 3538
)

)
================end output===============================

how to get this :

=================wanted output==============================
Array
(
[http://www.facebook.com/21cMuseumHotels?v=wall] => Array
(
[2010-05-31 06:20:55] => 3538
[2010-05-31 06:20:55] => 3538
)

)
================end of wanted output===============================

I tried various stuff like :

array_push($fan_by_url[$url] , $date_fans['date']=>
$date_fans['fans']);

but it doesn't work :(((

any help appreciated !
JR.

Cral

unread,
Jun 9, 2010, 9:44:04 PM6/9/10
to PHP Programming
the porbrem is the array can not duplicate key in php.


one way is follow this,the key ‘date’ is different

<?php
$fan_by_url = array();
$date_fans['date1']='2010-05-31 06:20:54';
$date_fans['date2']='2010-05-31 06:20:55';
$date_fans['fans']=3538;
$url='http://www.facebook.com/21cMuseumHotels?v=wall';

$fan_by_url[$url] = array(
$date_fans['date1']=>$date_fans['fans'],
$date_fans['date2']=>$date_fans['fans'],
);

print_r($fan_by_url);
?>


the other way is


<?php
$fan_by_url = array();
$date_fans['date']='2010-05-31 06:20:54';
$date_fans['fans']=3538;
$url='http://www.facebook.com/21cMuseumHotels?v=wall';

$fan_by_url[$url] = array(
array($date_fans['date']=>$date_fans['fans']),
array($date_fans['date']=>$date_fans['fans']),
);

print_r($fan_by_url);


try is youself!

Jasbir singh

unread,
Jun 10, 2010, 12:04:20 AM6/10/10
to php_pro...@googlegroups.com
Hi


One more way as follows:


<?php
$fan_by_url = array();
$date_fans['date']= array('2010-05-31 06:20:54', '2010-05-31 06:22:44');

$date_fans['fans']=3538;
$url='http://www.facebook.com/21cMuseumHotels?v=wall';

 $fan_by_url[$url] = array(
       array($date_fans['date'][0]=>$date_fans['fans']),
      array($date_fans['date'][1]=>$date_fans['fans']),
 );

foreach($fan_by_url as $key => $v)
{
        foreach($v as $key => $va)
        {
            foreach($va as $key => $vab)
            {
            echo $key." ".$vab."<br/>";   
           
            }       
       
        }   
   
}
 
 ?>


Thanks

Jasbir Singh
Mobile;9717004831

--
You received this message because you are subscribed to the Google Groups "PHP Programming" group.
To post to this group, send email to php_pro...@googlegroups.com.
To unsubscribe from this group, send email to php_programmi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/php_programming?hl=en.


jr

unread,
Jun 10, 2010, 2:48:35 AM6/10/10
to PHP Programming
thanks all for your answers, but it doesn't completely fill my
needs :s
I maube did not explain well my issue :(
In fact the question is maybe more :
I want an array of array, and being able to append the second array by
values like "key"=>"value".
I need a way to extend dynamically the second array....
if I test :
==================tested code====================
<?php
$fan_by_url = array();
$date_fans['date1']='2010-05-31 06:20:54';
$date_fans['date2']='2010-05-31 06:20:55';
$date_fans['fans']=3538;
$url='http://www.facebook.com/21cMuseumHotels?v=wall';

$fan_by_url[$url] = array(
$date_fans['date1']=>$date_fans['fans'],
$date_fans['date2']=>$date_fans['fans'],
);

print_r($fan_by_url);
?>
==============end of tested code========================
I got :
=================tested code output =====================
[2010-05-31 06:20:54] => 3538
[2010-05-31 06:20:55] => 3538
)

)

==========end of test code output==========================
That is fine, but how can I add a value to the array ?
To get :
=================wanted code output =====================
[2010-05-31 06:20:54] => 3538
[2010-05-31 06:20:55] => 3538
date => value
date => value
date => value
date => value
date => value
............
.........
........
......
)

)

==========end of wanted code output==========================
I tried : array_push($fan_by_url[$url],$date_fans['date2']=>
$date_fans['fans']);
but it doesnt work
==========tied code ==========================
<?php
$fan_by_url = array();
$date_fans['date1']='2010-05-31 06:20:54';
$date_fans['date2']='2010-05-31 06:20:55';
$date_fans['fans']=3538;
$url='http://www.facebook.com/21cMuseumHotels?v=wall';

$fan_by_url[$url] = array(
$date_fans['date1']=>$date_fans['fans'],
$date_fans['date2']=>$date_fans['fans'],
);
array_push($fan_by_url[$url],$date_fans['date2']=>$date_fans['fans']);
print_r($fan_by_url);
?>
==========end of trid code ==========================
result :

Parse error: parse error in line 12

:(((


any idea ?

On 10 juin, 06:04, Jasbir singh <jsingh.vi...@gmail.com> wrote:
> Hi
>
> One more way as follows:
>
> <?php
> $fan_by_url = array();
> $date_fans['date']= array('2010-05-31 06:20:54', '2010-05-31 06:22:44');
> $date_fans['fans']=3538;
> $url='http://www.facebook.com/21cMuseumHotels?v=wall';
>
>  $fan_by_url[$url] = array(
>        array($date_fans['date'][0]=>$date_fans['fans']),
>       array($date_fans['date'][1]=>$date_fans['fans']),
>  );
>
> foreach($fan_by_url as $key => $v)
> {
>         foreach($v as $key => $va)
>         {
>             foreach($va as $key => $vab)
>             {
>             echo $key." ".$vab."<br/>";
>
>             }
>
>         }
>
> }
>
>  ?>
>
> Thanks
>
> Jasbir Singh
> Mobile;9717004831
>
> > php_programmi...@googlegroups.com<php_programming%2Bunsu...@googlegroups.com>
> > .

宋登高

unread,
Jun 10, 2010, 3:13:32 AM6/10/10
to php_pro...@googlegroups.com


<?php
$fan_by_url = array();
$date_fans['date1']='2010-05-31 06:20:54';
$date_fans['date2']='2010-05-31 06:20:55';
$date_fans['fans']=3538;
$url='http://www.facebook.com/21cMuseumHotels?v=wall';

$fan_by_url[$url] = array();

$fan_by_url[$url][]=array($date_fans['date1']=>$date_fans['fans']);
$fan_by_url[$url][]=array($date_fans['date2']=>$date_fans['fans']);

 print_r($fan_by_url);
?>



To unsubscribe from this group, send email to php_programmi...@googlegroups.com.

jr

unread,
Jun 10, 2010, 4:05:28 PM6/10/10
to PHP Programming
Thanks, but it gives me that :
===============================================
[0] => Array
(
[2010-05-31 06:20:54] => 3538
)

[1] => Array
(
[2010-05-31 06:20:55] => 3538
)

)

)
===============================================
I would like that :
===============================================
Array
(
[http://www.facebook.com/21cMuseumHotels?v=wall] => Array
(
[2010-05-31 06:20:54] => 3538
[2010-05-31 06:20:55] => 3538
)
[ xxx ] => Array
(
[date 1] => v1
[date2] => v2
)

[ yyy ] => Array
(
[date 1] => v1
[date2] => v2
)
...
)
===============================================

On 10 juin, 09:13, 宋登高 <songdeng...@hudong.com> wrote:
> <?php
> $fan_by_url = array();
> $date_fans['date1']='2010-05-31 06:20:54';
> $date_fans['date2']='2010-05-31 06:20:55';
> $date_fans['fans']=3538;
> $url='http://www.facebook.com/21cMuseumHotels?v=wall';
>
> $fan_by_url[$url] = array();
>
> $fan_by_url[$url][]=array($date_fans['date1']=>$date_fans['fans']);
> $fan_by_url[$url][]=array($date_fans['date2']=>$date_fans['fans']);
>
>  print_r($fan_by_url);
> ?>
>
> > <php_programming%2Bunsu...@googlegroups.com<php_programming%252Buns...@googlegroups.com>
Reply all
Reply to author
Forward
0 new messages