Jump to content

array_push HELP!!?


emilyfrazier

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
Link to comment
https://forums.phpfreaks.com/topic/11244-array_push-help/
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
Link to comment
https://forums.phpfreaks.com/topic/11244-array_push-help/#findComment-42070
Share on other sites

Archived

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

×
×
  • 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.