简体   繁体   中英

How to create new arrays from existing multidimensional array in PHP

I have the following multi-dimensional array (this is only an extract) and would like to dynamically create a set of one-dimensional arrays and add values.

What I have:

0 => 
     0 => Image
     1 => DE
     2 => Hamburg-S02-I01
     3 => 54
     4 => Button
1 => 
     0 => Image
     1 => GB
     2 => London-S01-I01
     3 => 51
     4 => Button
2 => 
     0 => Image
     1 => GB
     2 => London-S01-I04
     3 => 60
     4 => Button
3 => 
     0 => Image
     1 => DE
     2 => Berlin-S02-I01
     3 => 57
     4 => Button
4 => 
     0 => Image
     1 => DE
     2 => Hamburg-S02-I01
     3 => 52
     4 => Button

What I need:

$Hamburg = array('Hamburg-S02-I01', 'Hamburg-S02-I01');
$London= array('London-S01-I01', 'London-S01-I04');
$Berlin= array('Berlin-S02-I01');

I appreciate your help! Thanks!

Something like this might work:

$cities = array();
foreach($images as $image) {
    $parts = explode('-',$image[2]);
    $city = $parts[0];
    if(!isset($cities[$city])) $cities[$city] = array();
    $cities[$city][] = $image[2];
}

Try this... $myarray is an array containing your example, above.

$cities = array();
foreach($myarray as $key => $val){
    unset($tmp);
    $tmp = explode("-",$val[2]);
    if(!is_array($cities[$tmp[0]])){
        $cities[$tmp[0]] = array();
    }
    array_push($cities[$tmp[0]], $val[2]);       
}

I am...

  • Looping through your original array.

  • Using the variable $tmp to split appart the element with the city name.

  • Using just the city name, I'm testing if I have a new array element with that name.

  • If not, create that array inside $cities .

  • Either way, push the city string into the city array.

$new_array = array();
foreach ($array as $key => $val) {
    $new_array[explode('-',$val[2])[0]][] = $val;
}

That code travels through your array - that I arbitrarily named $array - and push every sub_array in it inside sub_arrays in another array which key will be whatever is before the first hyphen character in the third entry of the original sub_array. I don't know if it's what you were after, but maybe at least it will inspire you.

Content of new array : (3) [Hamburg] => (2) [0] => (5) [0] => Image [1] => DE [2] => Hamburg-S02-I01 [3] => 54 [4] => Button [1] => (5) [0] => Image [1] => DE [2] => Hamburg-S02-I01 [3] => 52 [4] => Button [London] => (2) [0] => (5) [0] => Image [1] => GB [2] => London-S01-I01 [3] => 51 [4] => Button [1] => (5) [0] => Image [1] => GB [2] => London-S01-I04 [3] => 60 [4] => Button [Berlin] => (1) [0] => (5) [0] => Image [1] => DE [2] => Berlin-S02-I01 [3] => 57 [4] => Button

$new_array = array();
foreach ($array as $key => $val) {
    $new_array[explode('-',$val[2])[0]][] = $val[2];
}

The second version pushes only the third entry, since it seems that's what you're after.

Output : (3) [Hamburg] => (2) [0] => Hamburg-S02-I01 [1] => Hamburg-S02-I01 [London] => (2) [0] => London-S01-I01 [1] => London-S01-I04 [Berlin] => (1) [0] => Berlin-S02-I01

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM