Solved

How to join the following array together

Posted on 2008-06-12
7
180 Views
Last Modified: 2008-06-15
PHP
How to join the following array together

i have 3 array
$arrayA = array('a','b','c');
$arrayB = array(2,3,6,5,7,8);
$arrayc = array('d','e','f','g');

i use this method at first( in code snippet

but i realize that this method only can be use when array ABC is in the same column

hence my column for 3 array are different how to do it?

the correct output should be]
"a"|"2"|"d"|"b"|"3"|"e"|"c"|"6"|"f"|""|"5"|"g"|""|"7"|""|""|"8"|""|
 

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

$arrayB = array(2,3,6,5,7,8);

$arrayc = array('d','e','f','g');

foreach($arrayA as $i => $datas)

{

$data1 = "$datas | {$arrayB[$i]} | {$arrayC[$i]}   |";

}

print_r($data1 );

Open in new window

0
Comment
Question by:firekiller15
  • 3
  • 2
  • 2
7 Comments
 
LVL 2

Accepted Solution

by:
jml948 earned 200 total points
ID: 21767001
Here's how i would do it
<?

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

$arrayB = array(2,3,6,5,7,8);

$arrayC = array('d','e','f','g');

$lenA = count($arrayA);

$lenB = count($arrayB);

$lenC = count($arrayC);
 

if($lenA > $lenB)

        $count = $lenA;

else

        $count = $lenB;
 

if($count < $lenC)

        $count = $lenC;
 

for($i = 0; $i < $count; $i++){

        $data1 .= "\"$arrayA[$i]\"|\"$arrayB[$i]\"|\"$arrayC[$i]\"|";

}

print_r($data1);

?>

Open in new window

0
 
LVL 3

Assisted Solution

by:Xorlev
Xorlev earned 50 total points
ID: 21767054
Exact output as above.
<?php

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

$arrayB = array(2,3,6,5,7,8);

$arrayC = array('d','e','f','g');
 

$count = max(count($arrayA), count($arrayB), count($arrayC));
 

for ($i = 0; $i < $count; $i++) {

        $data .= '"' . $arrayA[$i] . '"|"' . $arrayB[$i] . '"|"' . $arrayC[$i] . '"|';

}
 

echo $data; // "a"|"2"|"d"|"b"|"3"|"e"|"c"|"6"|"f"|""|"5"|"g"|""|"7"|""|""|"8"|""|

?>

Open in new window

0
 

Author Comment

by:firekiller15
ID: 21767083
jsut want to clarify with you
in code above u use if-else statement to find which array has the largest element then use the largest element to compare with other array?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:jml948
ID: 21767101
yes which can also be achieved by doing:

$count = max(count($arrayA), count($arrayB), count($arrayC));

as Xorlev stated.
0
 

Author Comment

by:firekiller15
ID: 21767233
How about if my array is from other function for example in code snippet
output i received is
| Array " | Array " | Array " | Array " | Array " | Array " | Array " | Array " | Array " | Array " | Array " | Array " | Array " 

why like that
fucntion a()

$query = "SELECT AMOUNT

           FROM  AMOUNTTABLE;

          

  

  $result = mysql_query($query);

  

  i=0;  

  while($row = mysql_fetch_array($result))

	 {

      $this->GetAMOUNTReturn[$i] = '"'.$row['AMOUNT'].'"'; 

	  $i++;

	 }

 }
 

function b()
 

  $query = "SELECT ADDRESS1,ADDRESS2,ADDRESS3,ADDRESS4,CITY

           FROM ADDRESSTABLE";   

	   

  $result = mysql_query($query);

  

 

 while($row = mysql_fetch_array($result))

	 {

      $this->GetAddressReturn[$i] = array('"'.$row['ADDRESS1'].'"|"'.$row['ADDRESS2'].'"|"'.$row['ADDRESS3'].'"|"'.$row['ADDRESS4'].'"|"'.$row['CITY'].'"'); 

	  $i++;

	 }

 }
 

function joinAll()

$lenA = count($this->GetAMOUNTReturn);

$lenB = count($this->GetAddressReturn);
 

 

if($lenA > $lenB)

        $count = $lenA;

else

        $count = $lenB;

 

for($i = 0; $i < $count; $i++){

        $data1 .= "\"$this->GetAMOUNTReturn[$i]\"|\"$this->GetAddressReturn[$i]\"";

}

print_r($data1);

?>

Open in new window

0
 

Author Comment

by:firekiller15
ID: 21767257
when i print_r($this->GetAMOUNTReturn) in join all i can read the value.. why?
0
 
LVL 3

Expert Comment

by:Xorlev
ID: 21767317
"when i print_r($this->GetAMOUNTReturn) in join all i can read the value.. why?"

In the second function you're setting an array with an array.

this->GetAddressReturn[$i] = array('"'.$row['ADDRESS1'].'"|"'.$row['ADDRESS2'].'"|"'.$row['ADDRESS3'].'"|"'.$row['ADDRESS4'].'"|"'.$row['CITY'].'"');

make it:
this->GetAddressReturn[$i] = '"'.$row['ADDRESS1'].'"|"'.$row['ADDRESS2'].'"|"'.$row['ADDRESS3'].'"|"'.$row['ADDRESS4'].'"|"'.$row['CITY'].'"'; 

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

863 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

28 Experts available now in Live!

Get 1:1 Help Now