Jump to content


Photo

array_push HELP!!?


  • Please log in to reply
2 replies to this topic

#1 emilyfrazier

emilyfrazier
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 05 June 2006 - 03:36 PM

Hi all,

I am struggling here. I am having problems with forming the array I want. The below code gives me both
$mat and $murl as keys in my array $materials. What I really want to do is the create the array $materials and then have the keys be $mat and the values be $murl. The foreach is what is confusing me. I can't seem to push onto the array unless I'm within the loop. The code is below but I will also explain here with subsitute variable values. So for every $_POST["material"] there are a few values: for example "Goat", "Fish", "Bird"....then for each $murl there is a bunch of those to0: for example "goat.html", "fish.html", "bird.html". So I want them to be key value pairs such as "Goat" => "goat.html". Here is the code:

$materials = array();
foreach($_POST["material"] as $mat){
array_push($materials, $mat);
}

foreach($_POST["murl"] as $murl){
array_push($materials, $murl);
}

foreach ($materials as $key => $value){
print $key . ": " . $value . "<br>";
}

Greatly appreciate your help in advance! Thanks,
Emily

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 05 June 2006 - 04:36 PM

If the number of entrys in $_POST['material'] and $_POST['murl'] will always be the same, try this:
<?php
for ($i=0;$i<count($_POST['material']);$i++)
     $materials[$_POST['material'][$i]] = $_POST['murl'][$i];
echo '<pre>' . print_r($materials,true) . '</pre>'; // debug line
?>

Ken


#3 emilyfrazier

emilyfrazier
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 05 June 2006 - 06:01 PM

Thanks, Ken...All works well now!

Emily




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users