-2

I have two PHP arrays, $array1 and $array2:

$array1 = [55, 23, 45, 6, 0, 12];
$array2 = ['Apple', 'Oranges', 'Pears', 'Banana', 'Mango', 'Cherry'];

Is there a way to ensure that where there is a 0 in $array1 it is deleted and a new $array3 is formed with the 0 omitted. Secondly, the corresponding index value of $array2 is also deleted and a new $array4 is created with 'Mango' omitted, in this example.

Thus, $array3 and $array4 would be:

$array3 = [55, 23, 45, 6, 12];
$array4 = ['Apple', 'Oranges', 'Pears', 'Banana', 'Cherry'];

The important point here is that two new arrays are of equal length and maintain their corresponding indexes, if that makes sense.

Flinsch
  • 4,296
  • 1
  • 20
  • 29
Greenbird
  • 35
  • 5
  • 2
    What you have tried? – Ankur Tiwari Jan 17 '19 at 10:48
  • Welcome to StackOverflow. `Is there a way to ensure`, there are a lot of ways to do anything. But SO isn't meant to do your code for you, you need to code yourself and ask for help if you get stuck. Please visit the [help center](https://stackoverflow.com/help), take the [tour](https://stackoverflow.com/tour) to see what and [How to Ask](https://stackoverflow.com/help/how-to-ask). Do some research, search for related topics on SO; if you get stuck, post a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) of your attempt, noting input and expected output. – Alexandre Elshobokshy Jan 17 '19 at 10:49
  • I'm very new to php. I'm trying to add to a php loop which produces a table of the above said values. However, I need two arrays from this loop to create a chart. The issue I have is that I want to omit the 0s and the corresponding index in array2. I dont know how to do this. – Greenbird Jan 17 '19 at 10:51
  • Do both arrays always start off the same length as each other, or can one be larger than the other? If the latter, can we assume that we can truncate the longer array to ensure equal length? – Darragh Enright Jan 17 '19 at 10:51
  • Hint: `if ($array1[$i] !== 0) // do something with $array2[$i]`… – deceze Jan 17 '19 at 10:54
  • I think you have some typos above—do you mean that the filtered version of `$array1` is saved to `$array3`, and the filtered version of `$array2` is saved to `$array4`? – Darragh Enright Jan 17 '19 at 10:58
  • https://stackoverflow.com/a/369608/4244684 – Stender Jan 17 '19 at 11:45
  • optimally - why not create an array that is `['Apple' => 55, 'Oranges' => 23, 'Pears' => 45, 'Banana' => 6, 'Mango' => 0, 'Cherry' => 12]` and omit the ones where the value is 0 – Stender Jan 17 '19 at 11:54
  • If you also want to maintain the array keys from both arrays, perhaps https://3v4l.org/72E8W – The fourth bird Jan 17 '19 at 12:44

1 Answers1

0

You simply need to parse the first array and check if there is a 0 in it. If not add it to the new array the value corresponding to the index of Arrays2.

<?php
$Arrays1 = [55, 23, 45, 6, 0, 12];
$Arrays2 = ['Apple', 'Oranges', 'Pears', 'Banana', 'Mango', 'Cherry'];
$Arrays4 = array();
$search = 55;

foreach($Arrays1 as $index => $arr1_val){
    if($arr1_val !== $search){
        $Arrays4[] = $Arrays2[$index];
    }else{
        unset($Arrays1[$index]);
    }
}
print_r($Arrays4); // Array ( [0] => Oranges [1] => Pears [2] => Banana [3] => Mango [4] => Cherry ) 
print_r($Arrays1); // Array ( [1] => 23 [2] => 45 [3] => 6 [4] => 0 [5] => 12 )
executable
  • 3,365
  • 6
  • 24
  • 52
  • Thank you very much. This works perfectly. For future reference, what if the value I am looking for array1 is not 0 but say 'NA'? – Greenbird Jan 17 '19 at 18:06
  • You just replace the value 0 `if($arr1_val != 0){` by what you want. If it's answered your question please mark it as accepted – executable Jan 18 '19 at 07:57
  • I've marked it as accepted. When i do what you suggested, ie. replace 0 with NA, it also excludes the 0s. – Greenbird Jan 18 '19 at 08:59
  • What is the `0s` ? I added the variable `$search` for you – executable Jan 18 '19 at 09:01
  • The 0s refers to all the possible 0 in the first array. For example, if the first array has [3,5,0,"NA", 0] and array2 has ['apple', 'oranges', 'pears', 'lime, 'mango'] your solution outputs [3,5] and ['apples','oranges']. – Greenbird Jan 18 '19 at 09:23