Jump to content

Array store many variable


sungpeng

Recommended Posts

$arr = array("somearray" => array($var => $var)); 

 

The $var value will keep changing. I need the "$arr" to store all the value that passed the $var. Like say index number 45 passed stored in $arr. index number 60 passed stored in $arr etc.

 

Result

$arr = array("somearray" => array(45 => 45, 60 => 60));

 

Is there a way to do it?

Link to comment
Share on other sites

Thank for telling me the above draft code is feasible. Another question link.

 

<?php
$arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42));

echo $arr["somearray"][6];    
echo $arr["somearray"][13];  
echo $arr["somearray"]["a"];  
etc
?> 

 

Can I have a "while" or "for" loop for (one echo $arr["somearray"][]; statement only) to echo all data in $arr instead of manually one by one key in echo $arr["somearray"][6];, echo $arr["somearray"][13];, echo $arr["somearray"]["a"];.

Link to comment
Share on other sites

What signifies an ID number. The key of the array index or the value. Use either or. The echo statement was to just show you it can be done and a rough example of how it is done. If you want the index use $key if you want the value of that index use $val. Simple as that.

 

However, seeing as you want to use them in a MySQL statement, I would suggest looking into array_keys and implode:

 

If you want to use the keys:

$query = mysql_query("SELECT * FROM table WHERE id IN('" . implode("', '", array_keys($arr['somearray'])) . "') ORDER BY id");

 

Of the values:

$query = mysql_query("SELECT * FROM table WHERE id IN('" . implode("', '", $arr['somearray']) . "') ORDER BY id");

 

Using the above two methods (either or) will be way more efficient than looping through your code and running a query for each item.

Link to comment
Share on other sites

<?php
session_start();
$indexnumber=$_GET[rid];
$arr = array("somearray" => array($indexnumber => $indexnumber)); 
$_SESSION['views']=$arr;

?><html>
<body><?php
$arr=$_SESSION['views'];
foreach ($arr['somearray'] as $key => $val) {
  
$query=mysql_query("select*from table where id=$val");
}
?></body>
</html>

 

Ok Thank check again why the above code cannot work?

 

Link to comment
Share on other sites

Ok above is the final result that I need. It still not working so I will break it apart to test.

<?php
session_start();
$indexnumber=$_GET[rid];
$arr = array("somearray" => array($indexnumber => $indexnumber)); 
$_SESSION['views']=$arr;

?><html>
<body><?php
$arr=$_SESSION['views'];
echo $arr["somearray"]["indexnumber"];  


?></body>
</html>

 

Can I know why the about code not working?

 

Link to comment
Share on other sites

Because you're indexing the array by the variable $indexnumber and not the string. Which do you want?

 

From the topic, I guess you want this:

echo $arr['somearray'][$indexnumber];

 

Although I don't know why you would do that rather than just:

echo $indexnumber;

Link to comment
Share on other sites

I am going to store many $indexnumber in $arr. Then call out one by one in mysql statement. It is still not working.

<?php
session_start();
$indexnumber=$_GET[rid];
$arr = array("somearray" => array($indexnumber => $indexnumber)); 
$_SESSION['views']=$arr;

?><html>
<body><?php
$arr=$_SESSION['views'];
echo $arr['somearray'][indexnumber];  


?></body>
</html>

Link to comment
Share on other sites

How to make it not overwrite the other ones.

intended result

$arr = array("somearray" => array(5 => 5, 9 => 9, 42 => 42));

It will store all variable passed.

 

Amend

Need to store many products in session array then check out, view all products without the person who is using it log in to the account.

Link to comment
Share on other sites

<?php
session_start();
$indexnumber=$_GET[rid];
$arr1 = array("somearray" => array($indexnumber => $indexnumber)); 
$arr2=$_SESSION['views'];
$arr=array_combine($arr2, $arr1);
$_SESSION['views']=$arr;

?><html>
<body><?php
$arr=$_SESSION['views'];

foreach ($arr['somearray'] as $key => $val) {
    echo "{$key} => {$val}<br />";
}


?></body>
</html>

 

Just checking why about code cannot work?  I still think is possible to use session array to store all indexnumber passed.

Link to comment
Share on other sites

<?php
session_start();
$indexnumber=$_GET[rid];
$arr1 = array("somearray" => array($indexnumber => $indexnumber)); 
$arr2=$_SESSION['views'];
$arr=array_merge($arr2, $arr1);

$_SESSION['views']=$arr;

?><html>
<body><?php
$arr=$_SESSION['views'];

foreach ($arr['somearray'] as $key => $val) {
    echo "{$key} => {$val}<br />";
}


?></body>
</html>

 

It's seen possible to use array merge?? I have seen people use session to store many id and later display all "shortlisted" in one page but I just cannot figure out how?

Link to comment
Share on other sites

<?php
session_start();
include ("".$_SERVER['DOCUMENT_ROOT']."/config.php");
$query=mysql_query("select * from stats where id='1'" );
$row=mysql_fetch_array($query);

$count="$row[counter]";
$_SESSION[$count]=$_GET[rid];

?><html>
<body><?php


while($index<=10000)
  {
$arr =$_SESSION[$index];
echo "$arr";  

  $index++;
  }


?></body>
</html>

 

row[counter] is a counter that will add one once you click on this page, it is within 10000. I don't see any problem in above code but why it cannot work. It time to use database.

Link to comment
Share on other sites

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.