Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

please help, merging arrays into an associative one

Posted on 2013-06-02
4
227 Views
Last Modified: 2013-06-02
hi
i have a form to add attributes to a product. when the user click on the "add attribute" button, he has a text field for the option name and a text field to assign this attribute position in the display order, and a button to add to this attribute values.

when this button is clicked, a table row appears with a text field for the value name (lets say that the option is color: so the value name can be red), a text field for its position in the display list and a text field for this value price, if it is wanted. and a remove button.

the form works fine, and i have managed to collect each value values into an array (with jquery to send via AJAX).

so lets say i have a post which looks like: $_POST['option_value_name'] which is an array like so:
array('red','green','blue');
and
array('0','1','2');// for the position
and so on.

now i want to merge those arrays, to make it easier to deal with the database in one loop.

merge_array - just indexical, so not fitting.
i tried array_combine, which is good but only allow 2 arrays.

is there a way to combine into an associative array with more then 2 arrays?

or maybe there is a better way to deal with my situation that i did not thought of?

best regards
0
Comment
Question by:derrida
  • 2
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
Jagadishwor Dulal earned 500 total points
ID: 39214142
Are you looking for somethings like:

<?php
$opt1 = array("red" => "0");
$opt2 = array("green" => "1");
$opt3=array("blue"=>"3");

$result = array_merge($opt1, $opt2, $opt3);

echo '<pre>';
var_dump($result);
echo '</pre>';

Open in new window

0
 
LVL 1

Author Comment

by:derrida
ID: 39214152
hi
thanks for answering.
i know about array_merge and as i said that was my first try, but i want an associative array and not indexical.
0
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 39214159
The above example is associative array example can you please write the quite good scenario of your code to explain:
0
 
LVL 1

Author Closing Comment

by:derrida
ID: 39214162
ho god, i am so stupid. its associative, and you just saved my life:)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Google Maps API and PHP 25 49
How to set the Tinymce Editor image path 4 22
reverse engineer .sql from php files 11 35
PHP 5.6 and 7.x 4 23
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question