Current location: Hot Scripts Forums » Programming Languages » PHP » about arrays


about arrays

Reply
  #1 (permalink)  
Old 12-13-09, 02:14 PM
medo medo is offline
Newbie Coder
 
Join Date: Aug 2007
Posts: 23
Thanks: 4
Thanked 0 Times in 0 Posts
about arrays

Hi,

I have 2 arrays

PHP Code:

$x = array('a','b','c');

$y = array('s','t','u','v'); 
I want to merge the arrays values to become in the result

PHP Code:

"'a'='t','b'='u','c'='v'" 

please note the 1st value in the 2nd array [ y ] I want to disable it .

please help me to do that .
Regards
__________________
Sorry about my bad language I'm not english person

I'm new php man
Reply With Quote
  #2 (permalink)  
Old 12-13-09, 03:21 PM
phpdoctor's Avatar
phpdoctor phpdoctor is offline
Code Guru
 
Join Date: Feb 2007
Location: New Zealand
Posts: 767
Thanks: 4
Thanked 2 Times in 2 Posts
Why is 's' in the second array disabled? is it because array $x only has 3 values?
Also are the arrays going to be different sizes?

Cheers
__________________
01010000 01001000 01010000
Reply With Quote
  #3 (permalink)  
Old 12-13-09, 05:27 PM
medo medo is offline
Newbie Coder
 
Join Date: Aug 2007
Posts: 23
Thanks: 4
Thanked 0 Times in 0 Posts
I will use this to mysql class in update query from POST array

I will disable the 's' from the 2nd array becasue it will be the ID and I will use ID in where id = '1' for example

I will get the values for the 2nd array from the table by this function

PHP Code:

function get_fields($table)
{
$result mysql_query("SHOW COLUMNS FROM $table ");

     while (
$row mysql_fetch_assoc($result))
       {
          
$fields[] = $row[Field];
       }
       
$fields join(",",$fields);

__________________
Sorry about my bad language I'm not english person

I'm new php man
Reply With Quote
  #4 (permalink)  
Old 12-13-09, 07:21 PM
phpdoctor's Avatar
phpdoctor phpdoctor is offline
Code Guru
 
Join Date: Feb 2007
Location: New Zealand
Posts: 767
Thanks: 4
Thanked 2 Times in 2 Posts
Im still not really sure what you mean >.> What are you trying to create or do?

Have a look at this:
PHP Code:

$x = array('a','b','c'); 

$y = array('s','t','u','v');  

$merged = array();

foreach (
$x as $key => $val)
{
    
$merged[$y[$key]] = $val;

Havent tested it but it should work.
Only thing is, I haven't done any disabling... not sure what you mean yet.

Lex
__________________
01010000 01001000 01010000
Reply With Quote
The Following User Says Thank You to phpdoctor For This Useful Post:
medo (12-14-09)
  #5 (permalink)  
Old 12-13-09, 07:56 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
Reply With Quote
The Following User Says Thank You to wirehopper For This Useful Post:
medo (12-14-09)
  #6 (permalink)  
Old 12-14-09, 01:38 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
If you use wirehopper's suggestion and combine it with array_shift() then you can do it with two lines of code.
PHP Code:

<?php
$x 
= array('a','b','c');
$y = array('s','t','u','v');

$id array_shift($y);
$z array_combine($x,$y);

// Output the results. //
echo "ID = ".$id."<br />";
foreach(
$z as $key => $value){echo $key." = ".$value."<br />";}
?>
__________________
Jerry Broughton
Reply With Quote
The Following User Says Thank You to job0107 For This Useful Post:
medo (12-14-09)
  #7 (permalink)  
Old 12-14-09, 10:20 AM
medo medo is offline
Newbie Coder
 
Join Date: Aug 2007
Posts: 23
Thanks: 4
Thanked 0 Times in 0 Posts
Jerry I tried your code and it was success but in the last of result there was a comma ,

PHP Code:


$x 
= array('id','a','b','c'); 
$y = array('s','t','u','v');

$id array_shift($y);
$z array_combine($x,$y);

// Output the results. //

foreach($z as $key => $value){echo "'".$key."' = '".$value."',";} 
the result is
PHP Code:

'a' 't','b' 'u','c' 'v'
I want to remove , after v only


Regards
Medo
__________________
Sorry about my bad language I'm not english person

I'm new php man
Reply With Quote
  #8 (permalink)  
Old 12-14-09, 01:57 PM
phpdoctor's Avatar
phpdoctor phpdoctor is offline
Code Guru
 
Join Date: Feb 2007
Location: New Zealand
Posts: 767
Thanks: 4
Thanked 2 Times in 2 Posts
Wow I totally look like a noob... array_combine()!!!! Havent used that in ages :/

Well here's one way:
PHP Code:

$ids = array();

foreach(
$z as $key => $value)
{
    
$ids[] = "'$key' = '$value'";
}

echo 
implode(', '$ids); 
__________________
01010000 01001000 01010000
Reply With Quote
  #9 (permalink)  
Old 12-14-09, 02:17 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
Jerry's code was correct. The comma is just an output issue - the array is good.
Reply With Quote
  #10 (permalink)  
Old 12-15-09, 07:17 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by medo View Post
Jerry I tried your code and it was success but in the last of result there was a comma ,

PHP Code:


$x 
= array('id','a','b','c'); 
$y = array('s','t','u','v');

$id array_shift($y);
$z array_combine($x,$y);

// Output the results. //

foreach($z as $key => $value){echo "'".$key."' = '".$value."',";} 
the result is
PHP Code:

'a' 't','b' 'u','c' 'v'
I want to remove , after v only


Regards
Medo
If you are going to use identically sized arrays, then you don't need the array_shift() command.
As a matter of fact, you will get an error if you use it.
And to answer your question;
You are separating the array elements by commas, which leaves an extra comma at the end.
So, to remove the last comma, you can use the substr() function along with the strlen() function.
Like this:
PHP Code:

<?php
$x 
= array('id','a','b','c');
$y = array('s','t','u','v');

$z array_combine($x,$y);

// Output the results. //
foreach($z as $key => $value){$output .= "'".$key."' = '".$value."',";}
$output substr($output,0,strlen($output)-1);
echo 
$output;
?>
If we use your original arrays, then the code would look like this:
PHP Code:

<?php
$x 
= array('a','b','c');
$y = array('s','t','u','v');

$id array_shift($y);
$z array_combine($x,$y);

// Output the results. //
foreach($z as $key => $value){$output .= "'".$key."' = '".$value."',";}
$output substr($output,0,strlen($output)-1);
echo 
$output;
?>
And in this example we add the 'id' element to the $z array:
PHP Code:

<?php
$x 
= array('a','b','c');
$y = array('s','t','u','v');

$id["id"] = array_shift($y);
$z array_merge($id,array_combine($x,$y));

// Output the results. //
foreach($z as $key => $value){$output .= "'".$key."' = '".$value."',";}
$output substr($output,0,strlen($output)-1);
echo 
$output;
?>
__________________
Jerry Broughton

Last edited by job0107; 12-15-09 at 07:45 AM.
Reply With Quote
The Following User Says Thank You to job0107 For This Useful Post:
medo (12-17-09)
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
arrays, folders and more arrays OnlineDesignz PHP 2 01-01-07 09:08 PM
work on arrays within arrays - i'm stuck... nassau PHP 1 04-29-06 02:37 PM
Passing Session Arrays DAL PHP 4 01-07-06 05:56 PM
Arrays with sub arrays perleo PHP 1 09-30-05 07:15 PM
Can record arrays on database? mhs12grade1992 PHP 5 02-17-05 11:20 AM


All times are GMT -5. The time now is 08:05 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.