SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    Combining Two Unequal Arrays by Matching Keys

    I have here two arrays. Array1 has a related value to array2. What I need is that I'm going to save both values at once. I used 'array_combine' function but my arrays are having unequal keys. Any help is greatly appreciated. Thank you.

    PHP Code:
    $type $_POST['dev_type'];
    $sn $_POST['dev_sn'];
    Array
    (
            [
    0] => 22
            
    [1] => 19
            
    [2] => 66
            
    [3] => 62
            
    [4] => 64
    )
    Array
    (
            [
    0] => sn1
            
    [1] => sn2
            
    [2] => sn3


  2. #2
    SitePoint Zealot 2ndmouse's Avatar
    Join Date
    Jan 2007
    Location
    West London
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not exactly sure what your question is. However, have you tried:

    array_diff_key — Computes the difference of arrays using keys for comparison
    Detect file changes remotely. SimpleSiteAudit is an early
    warning anti-hacker system which sends an alert on detection.

    PHP Find Orphan Files - Finds all the unreferenced files on your site.

  3. #3
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Thank you for that.
    here is what I really want.

    [CODE]
    Array
    (
    [0] => 22
    [1] => 19 =>sn1
    [2] => 66
    [3] => 62 =>sn2
    [4] => 64 =>sn3
    )
    [CODE]

    Ok. Ill try that.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    [0] => 22 
    [1] => 19 =>sn1 
    I still do not understand what is required ...

    Is the end result supposed to end up as:
    PHP Code:
    $array[0] = 22;
    $array[1] = "19sn1"
    OR
    PHP Code:
    $array[0] = 22;
    $array[1] = array(19"sn1"); // an array of results 
    OR
    PHP Code:
    $array[0] = 22;
    $array[1] = array(19 => "sn1");  // a single array of results where key => "value" 
    OR
    Something else?

  5. #5
    SitePoint Zealot 2ndmouse's Avatar
    Join Date
    Jan 2007
    Location
    West London
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm don't know if your code will allow, but have you considered using the values from the first array as keys in the 2nd array

    PHP Code:
    <?php

    $arr1 
    = array ("22"=>"","19"=>"sn1","66"=>"","62"=>"sn2","64"=>"sn3");
    echo 
    '<pre>';
    print_r($arr1);
    echo 
    '</pre>';

    ?>
    Returns:

    Array
    (
    [22] =>
    [19] => sn1
    [66] =>
    [62] => sn2
    [64] => sn3
    )
    Detect file changes remotely. SimpleSiteAudit is an early
    warning anti-hacker system which sends an alert on detection.

    PHP Find Orphan Files - Finds all the unreferenced files on your site.

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    What I need is that I'm going to save both values at once
    This part makes me wonder how this is going to be stored....

  7. #7
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,180
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    I don't see anything that's relatable based on what your final output should be. Can you explain further?

  8. #8
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    I always thought that I got the real problem.
    I found myself here, asking questions and answered by questions.
    I asked myself too about it. When I repeat my question
    over and over. I'm lost and didn't understand too.

    After how many hours,days. I manage to find the simple solution.
    Now I realized that 90% of the solution is in the question.
    Thank you for the responses.

    I hope I know the 'I' that knows what I know.

  9. #9
    SitePoint Zealot 2ndmouse's Avatar
    Join Date
    Jan 2007
    Location
    West London
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It would be interesting to know what your simple solution consisted of?
    Detect file changes remotely. SimpleSiteAudit is an early
    warning anti-hacker system which sends an alert on detection.

    PHP Find Orphan Files - Finds all the unreferenced files on your site.

  10. #10
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    i'm gonna guess instead of trying to merge it all... if(in_array)....
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •