3

I am trying to build a function for easier array manipulation in my project.

I want to do it by passing reference. Hoping to be more productive and resource-saving way.

function add_element ($element=array(),&$data) {
    if(!empty($data)) {
      $data += $element;
    }
    return true;
  }

// $element can be array('one','two') or array('color'=>'blue','type'=>'card')

I am not experienced with references, thanks for any tip.

2
  • 1
    There's a function already out there called array_push() to add new things to an array. Commented Oct 14, 2013 at 16:29
  • @Coulton I want to do add element with key. For example; $arr = array('0'=>'zero','1'=>'one','2'=>'two'); I need to add $element = array('21'=>'twentyone','45'=>'fourthy-five'); When array_push($arr,$element); It creates extra index [3] [3]=>array([21]... I need [2]=>two,[21]=>twentyone.. Commented Oct 14, 2013 at 16:38

4 Answers 4

1

I believe that this will have the effect that you're looking for. We are passing in the original array as the reference and it adds whatever data you pass to it to the original array.

function add_element (&$original_array = array(), $data) {

  // Cast an array if it isn't already
  !is_array($data) ? (array)$data : null;

  if(!empty($data)) {
    $original_array = $original_array + $data;
  }
  return true;
}

$names_array = array("first_name" => "bob");
$data_to_add = array("second_name" => "fred");

// Add new variable
add_element($names_array, $data_to_add);

// Show the contents
print_r($names_array);

See it live here: http://www.tehplayground.com/#DJXofIeQK

However, I have just taken what you posted as a starting point there. The above is basically the same as the following, which requires no special function:

$names_array = array("first_name" => "bob");
$data_to_add = array("second_name" => "fred");

// Add new variable
$names_array = $names_array + $data_to_add;

// Show the contents
print_r($names_array);

See it here: http://www.tehplayground.com/#PAbhOHaPT

Sign up to request clarification or add additional context in comments.

3 Comments

This is exactly what I am looking for. Thank you for trying hard to have a solid understanding of my question.
No problem, all the best :)
I added a line in the beginning of the function, please review if you may want to update your reply. Since I will use it as a helper, it must help me. When I say add_element($create_variable,$elements); it should create it as well :) So here is the line: isset($data) || $data = array();
1

Try

array_push($data, $element);

The array_push function add a new data to the end of the array.

Check the details here: http://php.net/manual/de/function.array-push.php

1 Comment

I added comment to question.
1
function add_element($element=array(), &$data) {
    if(!empty($element) && !empty($data)) {
      $data = array_merge($data, $element);
    }
    return true;
}

$test = array(0=>'zero');
add_element(array(1=>'one'), $test);
print_r($test);

Comments

0

What about using :

$data[] = $element;

instead of

$data += $element;

(assuming you can't use array_push).

5 Comments

Nice, I didn't realise you could do that and I've been using PHP for a while lol. This is only useful though if you want numerical keys for your array. $data[] = $element; and $data[] = array() are not the same thing!
I can.. But in the code I want to write add_element($add,$data); and move, I don't want to do $data = add_element($add,$data);. that's why ı want to use reference. @Alexandre N.
array_push() will work the way you want it to, you pass a reference to it
@Coulton It does, but please see my lastest reply to your comment under question.
@Coulton I want to do add element with key. For example; $arr = array('0'=>'zero','1'=>'one','2'=>'two'); I need to add $element = array('21'=>'twentyone','45'=>'fourthy-five'); When array_push($arr,$element); It creates extra index [3] [3]=>array([21]... I need [2]=>two,[21]=>twentyone...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.