0

I have an array like below

Array
(
    [0] => country-indonesia
    [1] => country-myanmar
    [2] => access-is_airport
    [3] => heritage-is_seagypsy
)

From that array I want to make separate array only for [country] ,[access], [heritage]

So for that I have to check array value by text before '-'. I am not sure how to do it. so i can't apply code here. I just have the array in PHP

1
  • make an explosion by explode("-", $val) within a loop. Commented Sep 4, 2018 at 3:45

5 Answers 5

2

A modified answer, if you want to get the specific types only.

<?php

$arr = [
  'country-indonesia',
  'country-myanmar',
  'access-is_airport',
  'heritage-is_seagypsy',
];

$new_array = [];
$types = ['country', 'heritage', 'access'];

foreach ($arr as $element) {
  $fac = explode('-', $element);
  foreach ($types as $type) {
    if ($fac[0] === $type) {
      $new_array[$type][] = $fac[1];
    }
  }
}

$country = $new_array['country'];
$access = $new_array['access'];
$heritage = $new_array['heritage'];


var_dump($new_array);
Sign up to request clarification or add additional context in comments.

Comments

2

A simple and easy solution in 3 lines of code using array_walk

<?php

$arr = [
    'country-indonesia',
    'country-myanmar',
    'access-is_airport',
    'heritage-is_seagypsy',
];

$new_array = [];
array_walk($arr, function($item) use (&$new_array){
    //if(false === strpos($item, '-')) return;
    list($key,$value) = explode('-', $item, 2);
    $new_array[$key][] = $value;
});


print_r($new_array);

Gives this output:

Array
(
    [country] => Array
        (
            [0] => indonesia
            [1] => myanmar
        )

    [access] => Array
        (
            [0] => is_airport
        )

    [heritage] => Array
        (
            [0] => is_seagypsy
        )

)

If you don't want empty and duplicate entries:

<?php

$arr = [
    'country-indonesia',
    'country-myanmar',
    'access-is_airport',
    'heritage-is_seagypsy',
];

$new_array = [];
array_walk($arr, function($item) use (&$new_array){
    if(false === strpos($item, '-')) return;
    list($key,$value) = explode('-', $item, 2);
    if(empty($value) || array_key_exists($key, $new_array) && in_array($value, $new_array[$key])) return;
    $new_array[$key][] = $value;
});


print_r($new_array);

Comments

1

you can do it by using explode and in_array functions

 <?php
   $arr = ["country-indonesia","country-myanmar","access-is_airport","heritage-is_seagypsy"];
  $newArr = array();
   foreach($arr as $k=> $val){
      $valArr = explode("-", $val);
        if(!in_array($valArr[0], $newArr)){
           $newArr[] = $valArr[0];
        }
   }

   print_r($newArr);

  ?>

live demo

2 Comments

Thanks for your answer. May be i can't explain well. I got an array from ur code with pre value. I want to set array like this $country= array([0]=>indonesia [1] => thailand)
@Lemon : then I recommend Kerkouch answer.
0

You need PHP's strpos() function. Just loop through every element of the array and try something like:

if( strpos($array[$i], "heritage") != false )
{
    // Found heritage, do something with it
}

(Rough example written from my cellphone while feeding baby, may have typos but it's the basics of what you need)

Read further here: http://php.net/manual/en/function.strpos.php

Comments

0
//first lets set a variable equal to our array for ease in working with i.e
// also create a new empty array to hold our filtered values 
$countryArray = array();
$accessArray = array();
$heritageArray = array();

$oldArray = Array(country-indonesia, country-myanmar, access-is_airport, heritage-is_seagypsy);

//Next loop through our array i.e

for($x = 0; $x < count($oldArray); $x++){
// now filter through the array contents
$currentValue = $oldArray[$x];
// check whether the current index has any of the strings in it  [country] ,[access], [heritage] using the method : strpos()

if(strpos($currentValue,'country')){
//if this particular value contains the keyword push it into our new country array //using the array_push() function.
array_push($countryArray,$currentValue);
}elseif(strpos($currentValue,'access')){
  // else check for the access string in our current value
  // once it's found the current value will be pushed to the $accessArray
array_push($accessArray,$currentValue);
}elseif(strpos($currentValue,'heritage')){
 // check for the last string value i.e access. If found this too should be pushed to //the new heritage array i.e
array_push($heritageArray,$currentValue);
}else{
// do nothing
}
}

//I believe that should work: cheers hope

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.