Results 1 to 5 of 5

Thread: Help with array_splice()

  1. #1
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default Help with array_splice()

    So I have these 2 arrays for example
    PHP Code:
    $form_fields = array(
            array(
    "tag" => array("label"),
                  
    "text" => "product name",
                  
    "name" => "pname",
                  
    "class_fields" => TRUE,
                  
    "type" => "text",
                  
    "required" => TRUE,
                  
    "value_results_name" => "prod_name",
                  
    "admin_notes" => FALSE,
                  
    "options" => array(),
                  ),
            array(
    "tag" => array("label"),
                  
    "text" => "part number",
                  
    "name" => "pnum",
                  
    "class_fields" => TRUE,
                  
    "type" => "text",
                  
    "required" => TRUE,
                  
    "value_results_name" => "prod_number",
                  
    "admin_notes" => FALSE,
                  
    "options" => array(),
                  ));
    // 2nd array
    $add_fields = array(
                array(
    "tag" => array("function"),
              
    "text" => "vehicle:",
              
    "name" => "vehicle",
              
    "class_fields" => TRUE,
              
    "type" => "text",
              
    "required" => FALSE,
              
    "value_results_name" => "email",
              
    "admin_notes" => FALSE,
              
    "options" => array(),
              
    "function" => $results['v_year'].' '.$results['v_make'].' '.$results['v_model']
              ),
            ); 
    What I want is to insert the $add_fields array into the middle of the form_fields array. Now normally the form_fields array has several more indexes in it than just 2, I am just doing 2 for this example. I was trying to use array_splice() to join the 2 arrays in the index order I wanted but for some reason I can't get it to output any thing when I try this. I am not sure if maybe array_splice doesn't support the ability to do this with multi-dimensional arrays, so that might be the real issue. Here is the way I tried the splice, which is an example from the php.net page.
    PHP Code:
    $count count($form_fields)-1;
    $new_fields array_splice($form_fields$count0$add_fields);
    print_r($new_fields); 
    Which according to my knowledge should result in this array
    PHP Code:
    $new_fields = array(
            array(
    "tag" => array("label"),
                  
    "text" => "product name",
                  
    "name" => "pname",
                  
    "class_fields" => TRUE,
                  
    "type" => "text",
                  
    "required" => TRUE,
                  
    "value_results_name" => "prod_name",
                  
    "admin_notes" => FALSE,
                  
    "options" => array(),
                  ),
                    array(
    "tag" => array("function"),
              
    "text" => "vehicle:",
              
    "name" => "vehicle",
              
    "class_fields" => TRUE,
              
    "type" => "text",
              
    "required" => FALSE,
              
    "value_results_name" => "email",
              
    "admin_notes" => FALSE,
              
    "options" => array(),
              
    "function" => $results['v_year'].' '.$results['v_make'].' '.$results['v_model']
              ),
            array(
    "tag" => array("label"),
                  
    "text" => "part number",
                  
    "name" => "pnum",
                  
    "class_fields" => TRUE,
                  
    "type" => "text",
                  
    "required" => TRUE,
                  
    "value_results_name" => "prod_number",
                  
    "admin_notes" => FALSE,
                  
    "options" => array(),
                  )) 
    Any insight into this would be greatly appreciated.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    According to my reading that looks about right, except for where the manual states:

    If replacement array is specified, then the removed elements are replaced with elements from this array.

    If offset and length are such that nothing is removed, then the elements from the replacement array are inserted in the place specified by the offset. Note that keys in replacement array are not preserved.

    If replacement is just one element it is not necessary to put array() around it, unless the element is an array itself, an object or NULL.
    So, though it seems a little counterintuitive, I would try:

    PHP Code:
    $count count($form_fields)-1
    $new_fields array_splice($form_fields$count0, array($add_fields)); 
    print_r($new_fields); 
    Also, count($form_fields)-1 would be the last element in the $form_fields array. So if it works you would simply be appending to the end of it.

    Also if the $results array is undefined or any of those keys in it are undefined, that would cause a problem in an otherwise workable situation.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    I have tried that method too with the same result. And actually the $count with a longer array that I usually have would insert the other array one index in from the last index of the form_fields array.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I was wrong about the count, not sure why. I guess it inserts at that index, not after it as I assumed. I just did a test, and apparently (as in the examples on the man page) it's the existing input array that gets modified.

    This works:

    PHP Code:
    <?php
    $results 
    = array('v_year' => 1999'v_make' => 'ford''v_model' => 'fairlane');
    $form_fields = array( 
            array(
    "tag" => array("label"), 
                  
    "text" => "product name"
                  
    "name" => "pname"
                  
    "class_fields" => TRUE
                  
    "type" => "text"
                  
    "required" => TRUE
                  
    "value_results_name" => "prod_name"
                  
    "admin_notes" => FALSE
                  
    "options" => array() 
                  ), 
            array(
    "tag" => array("label"), 
                  
    "text" => "part number"
                  
    "name" => "pnum"
                  
    "class_fields" => TRUE
                  
    "type" => "text"
                  
    "required" => TRUE
                  
    "value_results_name" => "prod_number"
                  
    "admin_notes" => FALSE
                  
    "options" => array() 
                  )); 
    // 2nd array 
    $add_fields = array( 
                array(
    "tag" => array("function"), 
              
    "text" => "vehicle:"
              
    "name" => "vehicle"
              
    "class_fields" => TRUE
              
    "type" => "text"
              
    "required" => FALSE
              
    "value_results_name" => "email"
              
    "admin_notes" => FALSE
              
    "options" => array(), 
              
    "function" => $results['v_year'].' '.$results['v_make'].' '.$results['v_model'
              ), 
            ); 

    $count count($form_fields)-1;  
    array_splice($form_fields$count0$add_fields); 
    echo 
    '<pre>';
    print_r($form_fields);
    ?>
    </pre>
    Last edited by jscheuer1; 09-01-2012 at 04:31 PM. Reason: remove wrapping array() in splice
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    Ahh I see know, I don't actually need to assign the redesigned array to a new var I can just splice them together and then use the original array from before. Tested it with my full script and it works great, thanks!

Similar Threads

  1. Resolved array_splice vs array_unshift
    By ggalan in forum PHP
    Replies: 1
    Last Post: 09-19-2011, 03:22 PM

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
  •