Solved

combine arrays php

Posted on 2010-09-08
4
353 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 30

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 30

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 30

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
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 …

757 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

20 Experts available now in Live!

Get 1:1 Help Now