Solved

combine arrays php

Posted on 2010-09-08
4
358 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
[X]
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
  • 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

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

738 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