Jump to content

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

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.