SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Assigning values to arrays

    Hi. I have:

    PHP Code:
    ArrayAPITemplate
    (
        [
    0] => Array
            (
                [
    Id] => 96
                
    [Item] => func1 item1 sq1
                
    [HeaderId] => 3
                
    [Sequence] => 1
            
    )

        [
    1] => Array
            (
                [
    Id] => 92
                
    [Item] => prot1 item1 sq1
                
    [HeaderId] => 2
                
    [Sequence] => 1
            
    )

        [
    2] => Array
            (
                [
    Id] => 90
                
    [Item] => over1 item1 sq1
                
    [HeaderId] => 1
                
    [Sequence] => 1
            
    )

        [
    3] => Array
            (
                [
    Id] => 97
                
    [Item] => func2 item2 sq2
                
    [HeaderId] => 3
                
    [Sequence] => 2
            
    )

        [
    4] => Array
            (
                [
    Id] => 95
                
    [Item] => prot1 item2 sq2 new
                [
    HeaderId] => 2
                
    [Sequence] => 2
            
    )
    )

    ArrayValue
    (
        [
    0] => Array
            (
                [
    Value] => val8
                
    [Id] => 522
                
    [TemplateId] => 96
            
    )

        [
    1] => Array
            (
                [
    Value] => val5
                
    [Id] => 523
                
    [TemplateId] => 92
            
    )

        [
    2] => Array
            (
                [
    Value] => val1a
                
    [Id] => 524
                
    [TemplateId] => 90
            
    )

        [
    3] => Array
            (
                [
    Value] => val9
                
    [Id] => 525
                
    [TemplateId] => 97
            
    )

        [
    4] => Array
            (
                [
    Value] => val6
                
    [Id] => 526
                
    [TemplateId] => 95
            
    )

    ArrayAPITemplate is linked to ArrayValue by Id in the ArrayAPITemplate and ArrayValue is linked to ArrayAPITemplate by TemplateId. In other words, ArrayAPITemplate.Id == ArrayValue.TemplateId.

    I want to join the two arrays by creating a new array. But the problem is: not all the values in ArrayValue has a corresponding link to ArrayAPITemplate. In other words, ArrayAPITemplate can have 10 items but ArrayValue can have 9 or 8 values. The missing values will have to be filled by NULL

    How do I create the new array? Thanks.

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    How should the new array look? Cause I know what to do but I'm not sure how the two should be joined.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    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)
    It'd make life a lot simpler if you could show your arrays as code others could paste and fiddle around with:
    PHP Code:
    $x = array(0=>'this'2=>'that');
    $s var_export($x);
    echo 
    $s;
    //gives
    array ( => 'this'=> 'that', )

    //whereas what you have done, presumably with
    print_r$x 
    //gives
    Array ( [0] => this [2] => that 
    So we have to copy your code, and then start quoting all the strings. I say we, I mean the real gurus on here who actually know the answer to your problem.

    Because of my inquisitive mind (and lets face it, anything is better than actually doing the work I should be doing) I'd like to try and find the answer, but can't be @rsed formatting all the values.

    Just my POV, do you get my drift?

  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)
    You aren't getting these arrays out of a database are you?

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi! Thanks for you replies. The new array should look like:
    PHP Code:
    NewArray
    (
         [
    0] => Array(
                [
    Item] => func1 item1 sq1
                
    [HeaderId] => 3
                
    [Sequence] => 1
                
    [Value] => val8
                
    [TemplateId] => 96
            
    )
    )..
    etc... 
    Just join the two arrays using Id of ArrayAPITemplate with TemplateID of ArrayValue. I have thought of using a double 'for' loops but I do not know how to get around the problem of the missing Values in ArrayValue. Should should be assigned with NULL in NewArray like this:
    PHP Code:
    [10] => Array(
                [
    Item] => myItem
                
    [HeaderId] => X
                
    [Sequence] => X
                
    [Value] => 
                [
    TemplateId] => X
            

    My double 'for' loop looks like this:

    PHP Code:
    $k=0;
    for (
    $i=0$i<count($ArrayAPITemplate); $i++) {
          for (
    $j=0$j<count($ArrayValue); $j++) {
               if (
    $ArrayAPITemplate[$i]['Id'] == $ArrayValue[$j]['TemplateId']) {
                      
    $NewArray[$k]['Item'] = $ArrayAPITemplate[$i]['Item'];
                      
    $NewArray[$k]['HeaderId'] = $ArrayAPITemplate[$i]['HeaderId'];
                      
    $NewArray[$k]['Sequence'] = $ArrayAPITemplate[$i]['Sequence'];
                      
    $NewArray[$k]['Value'] = $ArrayValue[$j]['Value'];
                      
    $NewArray[$k]['TemplateId'] = $ArrayValue[$j]['TemplateId'];
                      
    $k++;
                } else {
                     
    //what should I do with ArrayValue with no corresponding TemplateId with ArrayAPITemplate?
                
    }
           }

    Please help. Thanks.


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
  •