A
A
AlexLichi2017-08-29 17:36:19
PHP
AlexLichi, 2017-08-29 17:36:19

What is the best way to create a PHP 3D array from the old one so that it only has nested arrays with certain keys?

array example:

$array = array(
        "a1" => array(
              "b1" => array(
                    "d" => 1,
                    "f" => 2,
                    ),
              "b2" => array(
                    "d" => 3,
                    "f" => 4,
                    ),
              ),
        "a2" => array(
              "b1" => array(
                    "d" => 1,
                    "f" => 2,
                    ),
              "b2" => array(
                    "d" => 3,
                    "f" => 4,
                    ),
              ),
      );

print_r ($array);

You need to create a new one in which there will be only a nested array "a1" with a nested "b1".

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stalker_RED, 2017-08-29
@AlexLichi

$new = [
  'a1' => [
    'b1' => $array['a1']['b1']
  ]
];
If not satisfied, try to describe the problem in more detail.

A
AlexLichi, 2017-08-29
@AlexLichi

I've done so far, like
$array2 = $array;
unset($array2["a2"]);
But maybe there is some simple solution, if for example I know the keys of the nested arrays that I need to leave, so that they remain, and the rest of the nested arrays are deleted?
Otherwise, in my solution, you need to specify the array key that you want to delete.
I just have a lot of nested arrays.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question