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
Solved

combine arrays php

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…

789 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