-1

I am trying to solve a question related to a much bigger script. However the simple example below shows clearly the problem I am attempting to solve in the bigger script. Here is the ready to go code

 $a=array("Volvo",22,18);
 $b=array("BMW",15,13);
 $c=array("Saab",5,2);
 $d=array("Land Rover",17,15);

  UNSET ($a); 
  $a=array_filter($a);//remove the null values 
  $a=array_values($a);//remove the keys 

$cars = array
  (
     $a,
     $b,
     $c,
     $d,
  );


print "<pre>";
print_r($cars);
print "</pre>";

My question is as follows...

Unset($a) removes the content of array($a). However as can be seen from the example (print_r) the array($a) is not completely destroyed. The keys remain. That causes a big problem later on as third party validators interpret this residual array data and return a validation error. The only way to get rid of the error is to get rid of the array($a) altogether.

As you can see I have tried array_filter($a) and array_values($a). But the output is unchanged.

Anyone know how to get rid of the array($a) programmatically in this example?

Many thanks

ps Here is the output from Print_r

 Array
     (
      [0] => 

    [1] => Array
      (
        [0] => BMW
        [1] => 15
        [2] => 13
     )

   [2] => Array
    (
        [0] => Saab
        [1] => 5
        [2] => 2
    )

    [3] => Array
      (
        [0] => Land Rover
        [1] => 17
        [2] => 15
     )

 )
  • _"as can be seen from the example (print_r)"_... I don't see any result from the `print_r()` in your question. – M. Eriksson Nov 30 '16 at 22:24
  • Turn on Error Reporting and see what you get – RiggsFolly Nov 30 '16 at 22:24
  • Notice: `Undefined variable: a in tst.php on line 12` – RiggsFolly Nov 30 '16 at 22:25
  • In PHP7 & 5.6 & 5.5 `$a` is destroyed and the `print_r()` shows array[0] as NULL – RiggsFolly Nov 30 '16 at 22:28
  • The output of what you posted here is Array ( [0] => [1] => Array ( [0] => BMW [1] => 15 [2] => 13 ) [2] => Array ( [0] => Saab [1] => 5 [2] => 2 ) [3] => Array ( [0] => Land Rover [1] => 17 [2] => 15 ) ). I'm not sure what it is that you think isn't happening – Steve Nov 30 '16 at 22:28
  • I have updated the question to include the output. I am trying to get rid of the [0] => as this is seen as a null (none) value by the third party validator – user2755309 Nov 30 '16 at 22:33
  • `Unset($a) removes the content of array($a).` no, it unset $a. However,the keys? There are no array keys in your example. And `have tried array_filter(` do you really now what these function are doing? – JOUM Nov 30 '16 at 22:36
  • Riggs---you are right. My problem is I need to get rid of the [0] =>. As you say it is a null value. I need the array to be completely destroyed--deleted inits entirety – user2755309 Nov 30 '16 at 22:36
  • check [this related answer](http://stackoverflow.com/questions/3654295/remove-empty-array-elements) – WEBjuju Nov 30 '16 at 22:36
  • `get rid of` then use array_filter on `$cars` and array_values after that to refresh the index – JOUM Nov 30 '16 at 22:38
  • Joum--i am attempting to show you what I have tried and yes I have taken the trouble to read up on these functions. Please respect I am doing my best to explain that – user2755309 Nov 30 '16 at 22:41
  • WEBjuju--this seems to be a very similar problem. Thank you for your help in pointing that out. I will try a few new things based on that. – user2755309 Nov 30 '16 at 22:44

2 Answers2

0

You can see this solution working over at 3v4l.

$cars = [
    [ 'Volvo', 22, 28 ],     // index 0
    [ 'BMW', 15, 13 ],       // index 1
    [ 'Add Cars', X, Y ]     // index i
];

unset($cars[0]);  // unset Volvo
unset($cars[1]);  // unset BMW
unset($cars[$i]); // unset index

If your cars are hard coded then this words fine, however, if you need to create checks against the cars then I'd suggest creating associative arrays on the cars like so:

$cars = [ 'all' =>
            [
                'brand' => 'Volvo',
                'X'     => 22,
                'Y'     => 28
            ],
            [
                'brand' => 'BMW',
                'X'     => 15,
                'Y'     => 13
            ]
        ];

foreach($cars['all'] as $car){
    if($car['brand'] == 'Volvo') {
        unset($car['brand']); // unset Volvo
    }
}
Jaquarh
  • 6,493
  • 7
  • 34
  • 86
  • Thank you KDOT. You were obviously writing this when i was testing the script i was referred to. I think your solution is definitely much more complete with the foreach method. I will incorporate this into the final solution. Thank you again – user2755309 Nov 30 '16 at 22:58
  • I just entered your second code snippet and print_r yielded the following output Array ( [all] => Array ( [brand] => Volvo [X] => 22 [Y] => 28 ) [0] => Array ( [brand] => BMW [X] => 15 [Y] => 13 ) ) – user2755309 Nov 30 '16 at 23:31
  • Is this what you were expecting KDOT (not sure that Volvo was unset)? – user2755309 Nov 30 '16 at 23:32
  • `unset($cars['all']['brand']);` – Jaquarh Dec 01 '16 at 03:39
0

Thanks to WEjuju i have a solution which is dead simple. Good job someone understands array_filter. It did the job as follows:

                $a=array("Volvo",22,18);
                $b=array("BMW",15,13);
                $c=array("Saab",5,2);
                $d=array("Land Rover",17,15);
                UNSET ($a);
                $cars = array
                  (
                        $a,
                        $b,
                        $c,
                        $d,
                  );

                print "<pre>";
                print_r($cars);
                print_r(array_filter($cars));//note the use of the filter after unset
                print "</pre>";

                ?>

The output is now....

Array
(
    [1] => Array
      (
        [0] => BMW
        [1] => 15
        [2] => 13
    )

 [2] => Array
    (
        [0] => Saab
        [1] => 5
        [2] => 2
    )

 [3] => Array
      (
         [0] => Land Rover
         [1] => 17
         [2] => 15
       )

 )

No pesky [0]=> interfereing with validation.