Jump to content

Archived

This topic is now archived and is closed to further replies.

emilyfrazier

array_push HELP!!?

Recommended Posts

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

Share this post


Link to post
Share on other sites
If the number of entrys in $_POST['material'] and $_POST['murl'] will always be the same, try this:
[code]<?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
?>[/code]

Ken

Share this post


Link to post
Share on other sites

×

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.