...

View Full Version : Unset in Multidementional Array



hmvrulz
09-10-2009, 10:06 PM
i have an array $this-data['Phone'] ( am using cakephp framework)

output of this give this.



Array
(
[0] => Array
(
[number] => 23467887
)

[1] => Array
(
[number] =>
)

[2] => Array
(
[number] => 23563866
)

[3] => Array
(
[number] =>
)

[4] => Array
(
[number] => 11222
)

)


i wanna unset the [1] and [3] because it doesnt have any values for the number.

i want a an array returned like this



Array
(
[0] => Array
(
[number] => 23467887
)


[1] => Array
(
[number] => 23563866
)


[2] => Array
(
[number] => 11222
)

)


Note: the key values have changed


After the all the manipulation all this should be stored in $this->data['Phone']

Zangeel
09-10-2009, 10:09 PM
You try something like this:



unset($this->data['phone'][1], $this->data['Phone'][3]);

hmvrulz
09-10-2009, 10:26 PM
You try something like this:



unset($this->data['phone'][1], $this->data['Phone'][3]);


thanx for the fast reply. the [1] [3] was jsut an example.

incase any of the [number] is not entered in the form i wanna unset it.

Zangeel
09-10-2009, 10:55 PM
Well im not sure EXACTLY what you're trying to do, but unsetting is the same weather you have 1 array or 100 arrays within one array. See this example I wrote up



<?php

// Multi-Dim Array
$phone = array
(
1 => array('111-1111', '222-2222'),
2 => array('333-333', '444-4444'),
);

//Show all values
var_dump($phone);

//Unset the second array piece from the first array value
unset($phone[1][1]);

//Show again, now without the '222-2222'
var_dump($phone);

?>


The out put is this:




First Array Dump:

array
1 =>
array
0 => string '111-1111' (length=8)
1 => string '222-2222' (length=8)
2 =>
array
0 => string '333-333' (length=7)
1 => string '444-4444' (length=8)

Second Array Dump after unsetting:

array
1 =>
array
0 => string '111-1111' (length=8)
2 =>
array
0 => string '333-333' (length=7)
1 => string '444-4444' (length=8)

hmvrulz
09-10-2009, 11:24 PM
$new = array();
foreach ($this->data['Phone'] as $key => $var) {

if (!empty($var['number'])) {
$new[] = $var;
}
}
$this->data['Phone'] = $new;



this seems to work :D

Thanx Zangeel.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum