PDA

View Full Version : Help with array_splice()



fastsol1
09-01-2012, 03:23 PM
So I have these 2 arrays for example


$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.


$count = count($form_fields)-1;
$new_fields = array_splice($form_fields, $count, 0, $add_fields);
print_r($new_fields);

Which according to my knowledge should result in this array


$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.

jscheuer1
09-01-2012, 04:10 PM
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:


$count = count($form_fields)-1;
$new_fields = array_splice($form_fields, $count, 0, 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.

fastsol1
09-01-2012, 04:14 PM
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.

jscheuer1
09-01-2012, 04:25 PM
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
$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, $count, 0, $add_fields);
echo '<pre>';
print_r($form_fields);
?>
</pre>

fastsol1
09-01-2012, 07:51 PM
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!