Jump to content

Several integers passed through as an array's element


Recommended Posts

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!

 

 

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'];
?>

 

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]

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.