PHP Array Creation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Prince of Code

    PHP Array Creation

    Hey Guys
    Lets assume we have two arrays
    one bug array - main array
    another user array - sub array

    The bug array contains the id of the user to whom the bug is
    assigned to.
    This Id is present in the user array for example
    bug - IE Error has assignedto id as 2
    this id 2 corresponds to user Peter in the user array
    how do i create a third array
    bugUserArray such that
    assigned to id of the bug user array should contain the complete
    detail of particular user
    as follows

    -----------------------------------------------------------------
    Bug Array
    Array
    (
    [0] => Array
    (
    [varbugId] => 71
    [varbugProjectId] => 23
    [varbugAssignedT oId] => 2
    [varbugTitle] => IE Error
    [varbugDescripti on] => The application ...
    )

    [1] => Array
    (
    [varbugId] => 72
    [varbugProjectId] => 45
    [varbugAssignedT oId] => 3
    [varbugTitle] => General Protection fault
    [varbugDescripti on] => GPF Fault occurs ...
    )

    [2] => Array
    (
    [varbugId] => 73
    [varbugProjectId] => 23
    [varbugAssignedT oId] => 2
    [varbugTitle] => Login Problem
    [varbugDescripti on] => User banned by the admin can...
    )

    )

    -------------------------------------------------------
    user Array
    Array
    (
    [0] => Array
    (
    [varUserId] => 1
    [varUserLoginNam e] => smith
    [varUserPassword] => password
    [varUserFullName] => Mr Smith
    [varUserEmail] => smith@gmail.com
    [varUserDateCrea ted] => 2005-12-15 15:17:41
    )

    [1] => Array
    (
    [varUserId] => 2
    [varUserLoginNam e] => Peter
    [varUserPassword] => Peter
    [varUserFullName] => Xavier Peter
    [varUserEmail] => peter@gmail.com
    )

    [2] => Array
    (
    [varUserId] => 3
    [varUserLoginNam e] => coolguy
    [varUserPassword] => password
    [varUserFullName] => Administrator
    [varUserEmail] => coolguy@gmail.c om
    )

    [3] => Array
    (
    [varUserId] => 4
    [varUserLoginNam e] => alzemer
    [varUserPassword] => alzemerlogin
    [varUserFullName] => Alzemer Alex
    [varUserEmail] => alzemer@gmail.c om
    )
    )

    bug user array
    -------------------------------------------------------------------
    Bug User Array
    Array
    (
    [0] => Array
    (
    [varbugId] => 71
    [varbugProjectId] => 23
    [varbugAssignedT oId] => Array
    (
    [varUserId] => 2
    [varUserLoginNam e] => Peter
    [varUserPassword] => Peter
    [varUserFullName] => Xavier Peter
    [varUserEmail] => peter@gmail.com
    )
    [varbugTitle] => IE Error
    [varbugDescripti on] => The application ...
    )

    [1] => Array
    (
    [varbugId] => 72
    [varbugProjectId] => 45
    [varbugAssignedT oId] => Array
    (
    [varUserId] => 4
    [varUserLoginNam e] => alzemer
    [varUserPassword] => alzemerlogin
    [varUserFullName] => Alzemer Alex
    [varUserEmail] => alzemer@gmail.c om
    )
    [varbugTitle] => General Protection fault
    [varbugDescripti on] => GPF Fault occurs ...
    )

    [2] => Array
    (
    [varbugId] => 73
    [varbugProjectId] => 23
    [varbugAssignedT oId] => Array
    (
    [varUserId] => 3
    [varUserLoginNam e] => coolguy
    [varUserPassword] => password
    [varUserFullName] => Administrator
    [varUserEmail] => coolguy@gmail.c om
    )
    [varbugTitle] => Login Problem
    [varbugDescripti on] => User banned by the admin can...
    )

    )

  • Oli Filth

    #2
    Re: PHP Array Creation

    Prince of Code said the following on 28/12/2005 08:37:[color=blue]
    > Hey Guys
    > Lets assume we have two arrays
    > one bug array - main array
    > another user array - sub array
    >
    > The bug array contains the id of the user to whom the bug is
    > assigned to.
    > This Id is present in the user array for example
    > bug - IE Error has assignedto id as 2
    > this id 2 corresponds to user Peter in the user array
    > how do i create a third array
    > bugUserArray such that
    > assigned to id of the bug user array should contain the complete
    > detail of particular user
    > as follows
    >[/color]
    <...SNIP ARRAY SAMPLE...>

    Rather than storing the userID as a member of the array, why not use it
    as the array key?

    e.g.:

    [0] => Array
    (
    [varUserId] => 1
    [varUserLoginNam e] => smith
    [varUserPassword] => password
    [varUserFullName] => Mr Smith
    [varUserEmail] => smith@gmail.com
    [varUserDateCrea ted] => 2005-12-15 15:17:41
    )
    ...

    should be:

    [1] => Array
    (
    [varUserLoginNam e] => smith
    [varUserPassword] => password
    [varUserFullName] => Mr Smith
    [varUserEmail] => smith@gmail.com
    [varUserDateCrea ted] => 2005-12-15 15:17:41
    )

    (Same principle could apply to the Bug Array.)

    Then to create your Bug User Array, it's a trivial matter of getting the
    relevant stuff out of the User array by key:

    $BugUserArray[$x] = $BugArray[$x];
    $BugUserArray[x]["varbugAssigned ToId"]
    = $UserArray[$BugArray[x]["varbugAssigned ToId"]];


    However, why would you want to replicate this much data? Why not just
    keep everything in their respective arrays?

    Also, this looks like a prime candidate for using a database - linking
    data between tables is all handled automatically.


    --
    Oli

    Comment

    • Peter Fox

      #3
      Re: PHP Array Creation

      Following on from Oli Filth's message. . .[color=blue]
      >Prince of Code said the following on 28/12/2005 08:37:
      >Rather than storing the userID as a member of the array, why not use it
      >as the array key?[/color]
      Because that's what PoC has been set to do as his coursework. He
      frequently posts his assignments here expecting us to do them for him.

      --
      PETER FOX Not the same since the deckchair business folded
      peterfox@eminen t.demon.co.uk.n ot.this.bit.no. html
      2 Tees Close, Witham, Essex.
      Gravity beer in Essex <http://www.eminent.dem on.co.uk>

      Comment

      Working...