SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict Divisive Cotton's Avatar
    Join Date
    Jun 2008
    Location
    Andy lives in London, UK
    Posts
    393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Altering an arraym value and duplicating it

    I've had my thinking hat on about this for the last couple of hours

    I have two arrays like this:

    PHP Code:
    Form_Controller::ah_form_fields():

    Array
    (
        [
    0] => Array
            (
                [
    id] => ah_url_name_input
                
    [title] => Give the feed a name here
                
    [desc] => Give your feed a label
                
    [std] => 
                [
    type] => text
                
    [section] => txt_section
                
    [choices] => 
                [class] => 
    ah_name
            
    )

        [
    1] => Array
            (
                [
    id] => ah_url_input
                
    [title] => Place the fields URL here
                
    [desc] => Place the fields URL here (desc)
                [
    std] => 
                [
    type] => text
                
    [section] => txt_section
                
    [choices] => 
                [class] => 
    ah_email
            
    )


    I have another array like so and this will increase with size with the same key names

    PHP Code:
    $option:

    Array
    (
        [
    0] => Array
            (
                [
    ah_url_name_input] => asdfsdaf
                
    [ah_url_input] => http://support.google.com/websearch/bin/answer.py?hl=en&answer=142143&topic=1221265&ctx=topic
            
    )


    I want clone the first two and place the values of the $option array in the std key so it will end up like this:

    PHP Code:

    Array
    (
        [
    0] => Array
            (
                [
    id] => ah_url_name_input
                
    [title] => Give the feed a name here
                
    [desc] => Give your feed a label
                
    [std] => asdfsdaf
                
    [type] => text
                
    [section] => txt_section
                
    [choices] => 
                [class] => 
    ah_name
            
    )

        [
    1] => Array
            (
                [
    id] => ah_url_input
                
    [title] => Place the fields URL here
                
    [desc] => Place the fields URL here (desc)
                [
    std] => http://support.google.com/websearch/bin/answer.py?hl=en&answer=142143&topic=1221265&ctx=topic
                
    [type] => text
                
    [section] => txt_section
                
    [choices] => 
                [class] => 
    ah_email
            
    )

        [
    3] => Array
            (
                [
    id] => ah_url_name_input
                
    [title] => Give the feed a name here
                
    [desc] => Give your feed a label
                
    [std] => 
                [
    type] => text
                
    [section] => txt_section
                
    [choices] => 
                [class] => 
    ah_name
            
    )

        [
    4] => Array
            (
                [
    id] => ah_url_input
                
    [title] => Place the fields URL here
                
    [desc] => Place the fields URL here (desc)
                [
    std] => 
                [
    type] => text
                
    [section] => txt_section
                
    [choices] => 
                [class] => 
    ah_email
            
    )


    So I'm going to need to loop through the array like so:

    PHP Code:


    foreach (Form_Controller::ah_form_fields() as $result) {

        foreach (
    $result as $key => $value) {

            if (
    $result[$key] == "std") {

                
    $result[$value] = //... something here

            
    }

        }


    Bit confused here about how to integrate the two together.

    The "cloning" is easy enough as I'll just save the results into a new array
    Let everyday be Christmas

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Sounds like you're trying to create an Object Class definition....
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Addict Divisive Cotton's Avatar
    Join Date
    Jun 2008
    Location
    Andy lives in London, UK
    Posts
    393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah good idea I'll try a different approach

    out of interest, is there a more elegant way of looping through a nested array other than using multiple foreach loops like so?


    PHP Code:
    foreach($option as $result) {
                        
                        foreach(
    $result as $key => $value) {
                               
                        echo 
    var_dump($result[$key]);
                            
                        }


    Let everyday be Christmas


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
  •