12-13-09, 02:14 PM
Newbie Coder
Join Date: Aug 2007
Posts: 23
Thanks: 4
Thanked 0 Times in 0 Posts
about arrays
Hi,
I have 2 arrays
I want to merge the arrays values to become in the result
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
12-13-09, 03:21 PM
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
12-13-09, 05:27 PM
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
__________________
Sorry about my bad language I'm not english person
I'm new php man
12-13-09, 07:21 PM
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:
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
The Following User Says Thank You to phpdoctor For This Useful Post:
12-13-09, 07:56 PM
-
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
The Following User Says Thank You to wirehopper For This Useful Post:
12-14-09, 01:38 AM
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.
__________________
Jerry Broughton
The Following User Says Thank You to job0107 For This Useful Post:
12-14-09, 10:20 AM
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 ,
the result is
I want to remove , after v only
Regards
Medo
__________________
Sorry about my bad language I'm not english person
I'm new php man
12-14-09, 01:57 PM
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:
__________________
01010000 01001000 01010000
12-14-09, 02:17 PM
-
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.
12-15-09, 07:17 AM
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
Jerry I tried your code and it was success but in the last of result there was a comma ,
the result is
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:
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 .
The Following User Says Thank You to job0107 For This Useful Post:
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off