0

I have array multi dimension from looping foreach. I want to insert new data from array at the end of array multi dimension like this :

Array
   (
     [setting_code] => gen_logo
     [setting_value] => logo.png
   )


Array
   (
[0] => Array
    (
        [setting_code] => gen_site_name
        [setting_value] => Codeigniter School CMS
    )

[1] => Array
    (
        [setting_code] => gen_email_info
        [setting_value] => [email protected]
    )

[2] => Array
    (
        [setting_code] => gen_meta_author
        [setting_value] => awnLabs.co
    )
  )

then the result I want like this :

Array
   (
[0] => Array
    (
        [setting_code] => gen_site_name
        [setting_value] => Codeigniter School CMS
    )

[1] => Array
    (
        [setting_code] => gen_email_info
        [setting_value] => [email protected]
    )

[2] => Array
    (
        [setting_code] => gen_meta_author
        [setting_value] => awnLabs.co
    )
[3] =>Array
   (
     [setting_code] => gen_logo
     [setting_value] => logo.png
   )
  )

how to get result like the above? thank's for help before :)

1
  • Have you considered $arr[] = , or array_push? Commented Nov 22, 2014 at 7:46

5 Answers 5

1

Simply can assign using $arr2[] = $arr1 or array_push(). Example:

$arr1 = Array
(
    "setting_code" => "gen_logo",
    "setting_value" => "logo.png",
);


$arr2 = array
(
    0 => array
    (
        "setting_code" => "gen_site_name",
        "setting_value" => "Codeigniter School CMS"
    ),
    1 => array
    (
        "setting_code" => "gen_email_info",
        "setting_value" => "[email protected]"
    ),
    2 => array
    (
        "setting_code" => "gen_meta_author",
        "setting_value" => "awnLabs.co",
    )
);

$arr2[] = $arr1;
// or 
//array_push($arr2, $arr1);

print "<pre>";
print_r($arr2);
print "</pre>";
Sign up to request clarification or add additional context in comments.

Comments

1

Use array_push to insert data at the end of array.

Comments

0

You should use array_merge()function.

Here is the example:

    $arr1 =
            Array
            (
            "setting_code" => "gen_logo",
             "setting_value" => "logo.png",
            );
    $arr2 = 
            Array
            (
            Array
                (
                    "setting_code" => "gen_site_name",
                "setting_value" => "Codeigniter School CMS"
            ),
            //Rest elements

            );
   $result = array_merge($arr2, $arr1);

Comments

0

array_push is simple and easy to for this situation. For example:

    $logo_1 = array
       (
         'setting_code' => 'gen_logo',
         'setting_value' => 'logo.png'
       );
    $logo_2 = array
           (
             'setting_code' => 'gen_logo',
             'setting_value' => 'logo.png'
           );
   $arr = array();
   array_push($arr, $logo_1);
   array_push($arr, $logo_2);
   var_dump($arr);

Comments

0
$var=array();
$test_arr['setting_code']='gen_site_name';
$test_arr['setting_value']='Codeigniter School CMS';
array_push($var, $test_arr);
$test_arr['setting_code']='gen_email_info';
$test_arr['setting_value']='[email protected]';
array_push($var, $test_arr);
$test_arr['setting_code']='gen_meta_author';
$test_arr['setting_value']='awnLabs.co';
array_push($var, $test_arr);
foreach ($var as $key=>$value){
    $arr[$key]=$value;
}

By using this code you will get the output you need, a small example of array_push

Thanks

Comments

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.