emilyfrazier Posted June 5, 2006 Share Posted June 5, 2006 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:[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>";}[/code]Greatly appreciate your help in advance! Thanks,Emily Quote Link to comment https://forums.phpfreaks.com/topic/11244-array_push-help/ Share on other sites More sharing options...
kenrbnsn Posted June 5, 2006 Share Posted June 5, 2006 If the number of entrys in $_POST['material'] and $_POST['murl'] will always be the same, try this:[code]<?phpfor ($i=0;$i<count($_POST['material']);$i++) $materials[$_POST['material'][$i]] = $_POST['murl'][$i];echo '<pre>' . print_r($materials,true) . '</pre>'; // debug line?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/11244-array_push-help/#findComment-42070 Share on other sites More sharing options...
emilyfrazier Posted June 5, 2006 Author Share Posted June 5, 2006 Thanks, Ken...All works well now! Emily Quote Link to comment https://forums.phpfreaks.com/topic/11244-array_push-help/#findComment-42096 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.