Array mainpulation

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
CardBoy
New php-forum User
New php-forum User
Posts: 5
Joined: Mon Jun 05, 2017 12:35 am

Tue Oct 24, 2017 4:19 am

Hi

I have an array declared as

Code: Select all

		static $array = array(
			'a0' => array(
				'b1' => array(
					'c0' => 5,
					'c1' => 'hi',
					),

				'b2' => array(
					'd0' => 5,
					'd1' => 'hi',
					),				),
			'a2' => 10,
			);
What I need to do is to loop over the array and extract a set of arrays such that these new arrays have an element removed, but leaving the original array in tact.
So the first array in the set would remove 'a0' and looks like

Code: Select all

		static $array = array(
			'a2' => 10,
			);
The second would remove [a0][b1] and looks like

Code: Select all

		static $array = array(
			'a0' => array(
				'b2' => array(
					'd0' => 5,
					'd1' => 'hi',
					),				),
			'a2' => 10,
			);
etc

Please can anyone help?

Thanks

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 526
Joined: Mon Feb 22, 2016 5:52 pm

Tue Oct 24, 2017 5:30 am

What you describe is the array_slice function found in the manual under Array Functions.

CardBoy
New php-forum User
New php-forum User
Posts: 5
Joined: Mon Jun 05, 2017 12:35 am

Tue Oct 24, 2017 6:09 am

Hi
Thanks for the reply
I did look at a array_slice but could not work out how to remove just [a0][b1]

Any ideas on hoe I can do this?

Thanks

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 526
Joined: Mon Feb 22, 2016 5:52 pm

Tue Oct 24, 2017 6:22 am

Experiment and read the examples and comments on the link given, you can't break anything and you'll learn a whole lot more in the process.

Post Reply