Solved

combine arrays php

Posted on 2010-09-08
4
354 Views
Last Modified: 2012-05-10
Hello i have three arrays

$name(..here goes the details..);

$numbers(..here goes the details..);

$website(..here goes the details..);


I need to combine these arrays and make them share same key for getting result like this

foreach ($array as $c){

  echo $x['name'];
  echo $x['numbers'];
 echo $x['website']';

}

0
Comment
Question by:Styleminds
  • 3
4 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 33632072
Try this (not tested: remember taht three arrays must have same dimension):

$array = array();
for ($i=0;$i<count($name);$i++){
  $array['name'] = $name[$i];
  $array['numbers'] = $numbers[$i];
  $array['website'] = $website[$i];
}

Hope this helps
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 33632212
Previous code doesn't work. Snippet below is better but it doesn't create an associative array. See if it works for you, please.
<?php
  $name=array("name1", "name2", "name3");
  $numbers= array("1","2","3");
  $website=array("www.sito1.com","www.sito2.com","www.sito3.com");
  
$array = array();
for ($i=0;$i<count($name);$i++){
  $array['name'][$i] = $name[$i];
  $array['numbers'][$i] = $numbers[$i];
  $array['website'][$i] = $website[$i];
}
foreach ($array as $x){
  echo $x[0]."<br>";
  echo $x[1]."<br>";
  echo $x[2]."<br>";
}

?>

Open in new window

0
 

Author Comment

by:Styleminds
ID: 33632263
but that is not what i need what i need to get result

name1
1
www.sito1.com
name2
2
www.sito2.com
3
name3
www.sito3.com

I need the result to be same as i am fetching a database and getting rows related a certain table
0
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 33632436
Well, try this: it's not exactly as you wish but almost equal.
<?php
  $name=array("name1", "name2", "name3");
  $numbers= array("1","2","3");
  $website=array("www.sito1.com","www.sito2.com","www.sito3.com");

$array = array();
for ($i=0;$i<count($name);$i++){
  $array[$i]['name'] = $name[$i];
  $array[$i]['numbers'] = $numbers[$i];
  $array[$i]['website'] = $website[$i];
}
for ($i=0;$i<count($array);$i++){
    echo $array[$i]['name']."<br>";
    echo $array[$i]['numbers']."<br>";
    echo $array[$i]['website']."<br>";
}

?>

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

Suggested Solutions

Title # Comments Views Activity
Post form to two locations 1 30
php email 2 26
form button worked now it doesnt anymore 9 39
echo time from sql to input type="time" 9 17
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 …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

919 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

17 Experts available now in Live!

Get 1:1 Help Now