Solved

Problem with Merge 2 array on Key

Posted on 2009-04-11
9
404 Views
Last Modified: 2012-08-13
Hi Experts,

I have 2 huge array with the same lenght. I need your help to merge them become 1 array on key

For Example:
====Array 1====
Array
(
      [1] => 111 // my value1
      [2] => 222 // my value2
);            

====Array 2====
Array
(
      [1] => abcde.jpg //image name1
      [2] => 12345.jpg //image name2
);

And I need to merge them base on the key number to become:

====Array 3====
Array
(
      [1] => Array
            (
                  [0] => 111 // my value1
                  [1] => abcde.jpg //image name1
            );
      [2] => Array
            (
                  [0] => 222 // my value2
                  [1] => 12345.jpg //image name2
            );
);

I have the code below but it got something wrong, so it won't run.
Can anyone help me to fix it ?
<?

if ($_POST['action']=='upload')

	{

	$a = $_POST[position];

		/*Example print_r($a)	

		Array

		(

			[1] => 111

			[2] => 222

		);		

		*/

		

	$b = $_POST[imgfile];

		/*Example print_r($b)	

		Array

		(

			[1] => abcde.jpg

			[2] => 12345.jpg

		);

		*/
 

		$c = array();	

		foreach ($count='0'; $count < sizeof($a); $count++)

		{

		  $c[$count] = array($a[$count]$b[$count]);

		}

		

		echo "<pre>";

		print_r ($c); die();

	}	

?>

Open in new window

0
Comment
Question by:nguyenhoan
  • 5
  • 4
9 Comments
 
LVL 3

Expert Comment

by:Ludger Peters
ID: 24121572
you can add 2 arrays like the following
<?php
 

/**

 * @author ludjer

 */

$a = array("Value 1","Value 2");

$b = array("Value a1","Value a2");

$c = array($a,$b);
 

echo "<pre>";

print_r ($c); 

die();    

?>

Open in new window

0
 
LVL 3

Expert Comment

by:Ludger Peters
ID: 24121601
in your code your error is from foreach, the command you meant to use is
for($count='0'; $count < sizeof($a); $count++) and i think your code would work.

i also showed how your code should be


example of foreach and for loops

<?php

$arr = array(1, 2, 3, 4);

foreach ($arr as &$value) {

    echo $value."<br \>"; // this will output 1<br \>2<br \>3<br \>4 

    $value = $value * 2;

    echo $value."<br \>"; // this will output 2<br \>4<br \>6<br \>8 

}

// $arr is now array(2, 4, 6, 8)
 

//you where ment to use a for loop command

for ($i = 1; $i <= 10; $i++) {

    echo $i;//this will echo 12345678910

}

?>
 

this is how your code should be

<?

if ($_POST['action']=='upload')

   {

        $a = $_POST[position];

        $b = $_POST[imgfile];

        $c = array($a,$b);   

        echo "<pre>";

        print_r ($c); die();

    }       

?>

Open in new window

0
 

Author Comment

by:nguyenhoan
ID: 24121603
Oh that's not what I want, sorry =[
0
 
LVL 3

Accepted Solution

by:
Ludger Peters earned 300 total points
ID: 24121630
well then could you tell me exactly what you need to do, do you want to use foreach loop or a for loop

ill try make 2 versions one with for loop and other with foreach loop

hope this works
forloop

<?PHP

if ($_POST['action']=='upload')

        {

        $a = $_POST[position];

                /*Example print_r($a)   

                Array

                (

                        [1] => 111

                        [2] => 222

                );              

                */

                

        $b = $_POST[imgfile];

                /*Example print_r($b)   

                Array

                (

                        [1] => abcde.jpg

                        [2] => 12345.jpg

                );

                */

		$c = array();   

		for($count= 0 ; $count <= sizeof($a); $count++)

		{

		  $c[$count] = array($a[$count],$b[$count]);

		}

        echo "<pre>";

        print_r ($c); die();

        }       

?>

for each loop

<?PHP

if ($_POST['action']=='upload')

        {

        $a = $_POST[position];

                /*Example print_r($a)   

                Array

                (

                        [1] => 111

                        [2] => 222

                );              

                */

                

        $b = $_POST[imgfile];

                /*Example print_r($b)   

                Array

                (

                        [1] => abcde.jpg

                        [2] => 12345.jpg

                );

                */

		$c = array();

		$counter = 1;

		foreach($a as $variable)

		{

		  $c[$counter] = array($a[$counter],$b[$counter]);

		  $counter++;

		}

        echo "<pre>";

        print_r ($c); die();

        }       

?>

?>

Open in new window

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 3

Expert Comment

by:Ludger Peters
ID: 24121643
woops just realized 2 problems
a)line 23
for($count= 0 ; $count <= sizeof($a); $count++)
changed to
for($count= 1; $count <= sizeof($a); $count++)

b)line 63
remove ?> its not needed
0
 

Author Comment

by:nguyenhoan
ID: 24121650
Wow, they are both worth, THANKYOU ALOT, MASTER LUDJER !!!
0
 

Author Closing Comment

by:nguyenhoan
ID: 31569156
Perfect and Thankyou !
0
 

Author Comment

by:nguyenhoan
ID: 24121701
Oh, Ludjer, if you can, please countinue helping me in this question:
http://www.experts-exchange.com/index.jsp?qid=24314398

Its the Part2 of this question ^^
Thankyou.
0
 

Author Comment

by:nguyenhoan
ID: 24121708
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
php image upload 3 24
session dropped in IE 10 19
Editing login page in zencart. 2 14
mysql left join sentence 7 19
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now