Gert Kommer
Gert Kommer

Reputation: 1243

array_push multidimensional array

I have the following codeline

$return_array = array(
            $count_answers => array(
                    "name" => $domain,
                    "type" => $type,
                    "class" => $class,
                    "ttl" =>$ttl,
                    "data_lenght" => $data_l
                    )
     );

I want to add preference after data length with the following code

array_push($return_array[$count_answers]['preference'], $preference);

Warning: array_push() expects parameter 1 to be array, null given in \functions\functions.php on line 367

why is my first parameter not an array?

Upvotes: 0

Views: 20795

Answers (5)

Rodrigo Serzedello
Rodrigo Serzedello

Reputation: 327

foreach($arr_data_arrays as $key=>$line_arr) { // do an array looping at first

        $new_arr = array(); // create an array to be included on the second position
        $new_arr[0] = $line_arr;

        array_push($arr_data_arrays[$key][1],$new_arr);//include the whole array on the sec position
};

Easy like that!

Upvotes: 0

symcbean
symcbean

Reputation: 48357

Using array_push() with a multidimensional array is an oxymoron.

PHP arrays are hierarchical - not multi-dimensional. And array_push adds a numbered element with the specified value. Further, the usage of array_push() is clearly explained in the manual.

I want to add 'preference' after data length with the following code

Why do you want to do it with that code? It's failing and the reason should be obvious.

The code you should you should be using is:

$return_array[$count_answers]['preference']=$preference;

Upvotes: 0

Olaf Dietsche
Olaf Dietsche

Reputation: 74018

Because there's no element in $return_array indexed by 'preference'. You can append $preference with this instead

$return_array[$count_answers]['preference'][] = $preference;

or initialize with an empty array first

$return_array[$count_answers]['preference'] = array();

If you don't want to add an array of preferences, but just one element 'preference', append it with

$return_array[$count_answers]['preference'] = $preference;

Upvotes: 5

anuj arora
anuj arora

Reputation: 831

You should correct your code with below.

$return_array = array(
        $count_answers => array(
                "name" => $domain,
                "type" => $type,
                "class" => $class,
                "ttl" =>$ttl,
                "data_lenght" => $data_l
                )
 );

$preference['preference'] = "kkk";

Just change

$return_array[$count_answers]['preference']

with

$return_array[$count_answers]

in array_push, like below

array_push($return_array[$count_answers], $preference);

Upvotes: 1

jaudette
jaudette

Reputation: 2313

You don't need to use array_push, you can add the item directly.

$return_array[$count_answers]['preference'] = $preference;

array_push does not allow string as indexes, so your $preference would be at $return_array[$count_answers][0]

On your line 367, your are not providing an array, but an empty element in your current array.

Upvotes: 2

Related Questions