LLeoun Posted September 8, 2009 Share Posted September 8, 2009 Hi all, I have the following code: <?php $clientID="12"; $cityID="4"; $p = array('clientID'=> $clientID, 'cityID'=> $cityID, 'productsIDs'=> array('int'=>22,'int'=>63,'int'=>59)); echo '<pre>'; print_r($p); echo '</pre>'; ?> I need productsIDs, that is already one of the array's element, to be an array of 3 integers .. How can I do that? Right now the output of the script above is: Array ( [clientID] => 12 [cityID] => 4 [products] => Array ( [int] => 59 ) ) Where are the rest of the integers?? Thanks a ton in advance! Link to comment https://forums.phpfreaks.com/topic/173531-several-integers-passed-through-as-an-arrays-element/ Share on other sites More sharing options...
Jibberish Posted September 8, 2009 Share Posted September 8, 2009 array('int'=>22,'int'=>63,'int'=>59)); the 'int' part is the index, so your are overwriting it each time you add the next number. just do array(22,63,59)); Link to comment https://forums.phpfreaks.com/topic/173531-several-integers-passed-through-as-an-arrays-element/#findComment-914698 Share on other sites More sharing options...
LLeoun Posted September 8, 2009 Author Share Posted September 8, 2009 Thanks Jibberish. Could do, but is there any way to give a name to the second array (array(22,63,59)) so I can refer to it .. Thanks again Link to comment https://forums.phpfreaks.com/topic/173531-several-integers-passed-through-as-an-arrays-element/#findComment-914713 Share on other sites More sharing options...
Jibberish Posted September 8, 2009 Share Posted September 8, 2009 Well you can still call them things inside that array, the indexs just have to be called different things so that it doesn't overwrite them. <?php $p = array('clientID'=> $clientID, 'cityID'=> $cityID, 'productsIDs'=> array('product1'=>22,'product2'=>63,'product3'=>59)); ?> If this doesnt answer your question post the code on how you want to read the products out of the array later on, this might help me see what you want to do and how you want them to be stored. To read them currently you would do. <?php $p['productsIDs']['product1']; $p['productsIDs']['product2']; $p['productsIDs']['product3']; ?> Link to comment https://forums.phpfreaks.com/topic/173531-several-integers-passed-through-as-an-arrays-element/#findComment-914725 Share on other sites More sharing options...
DarkWater Posted September 8, 2009 Share Posted September 8, 2009 Why make yourself type all of that extra stuff? You can refer to the array elements with the numbered index (starting at 0): <?php $p = array('clientID'=> $clientID, 'cityID'=> $cityID, 'productsIDs'=> array(22, 63, 59)); print_r($p); /*Output: Array ( [clientID] => 12 [cityID] => 4 [products] => Array ( [0] => 22 [1] => 63 [2] => 59 ) ) */ ?> You can just do $p['productsIDs'][0], etc.[/code] Link to comment https://forums.phpfreaks.com/topic/173531-several-integers-passed-through-as-an-arrays-element/#findComment-914737 Share on other sites More sharing options...
LLeoun Posted September 8, 2009 Author Share Posted September 8, 2009 thanks a lot to both, it's clear now! Link to comment https://forums.phpfreaks.com/topic/173531-several-integers-passed-through-as-an-arrays-element/#findComment-914766 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.