Link to home
Start Free TrialLog in
Avatar of Styleminds
Styleminds

asked on

combine arrays php

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']';

}

Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

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
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

Avatar of Styleminds
Styleminds

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial