2

I have an array with the following keys:

Array
{
    [vegetable_image] =>
    [vegetable_name] =>
    [vegetable_description] =>
    [fruit_image] =>
    [fruit_name] =>
    [fruit_description] =>
}

and I would like to split them based on the prefix (vegetable_ and fruit_), is this possible?

Currently, I'm trying out array_chunk() but how do you store them into 2 separate arrays?

[vegetables] => Array { [vegetable_image] ... }
[fruits] => Array { [fruit_image] ... }
1

5 Answers 5

1

This should work for you:

$fruits = array();
$vegetables = array();

foreach($array as $k => $v) {
    if(strpos($k,'fruit_') !== false)
        $fruits[$k] = $v;
    elseif(strpos($k,'vegetable_') !== false)
        $vegetables[$k] = $v;
}

As an example see: http://ideone.com/uNi54B

Sign up to request clarification or add additional context in comments.

Comments

1

Out of the Box

function splittArray($base_array, $to_split, $delimiter='_') {
   $out = array();
   foreach($to_split as $key) {
      $search = $key.delimiter;
      foreach($base_array as $ok=>$val) {
        if(strpos($ok,$search)!==false) {
           $out[$key][$ok] = $val;
        }
   }
   return $out;
 }

 $new_array = splittArray($array,array('fruit','vegetable'));

Comments

1

It is possible with array_reduce()

$array     = ['foo_bar' => 1, 'foo_baz' => 2, 'bar_fee' => 6, 'bar_feo' => 9, 'baz_bee' => 7];
$delimiter = '_';

$result = array_reduce(array_keys($array), function ($current, $key) use ($delimiter) {
        $splitKey                = explode($delimiter, $key);
        $current[$splitKey[0]][] = $key;
        return $current;
}, []);

Check the fiddle

Only one thins remains: you are using different forms (like "vegetable_*" -> "vegetables"). PHP is not smart enough to substitute language (that would be English language in this case) transformations like that. But if you like, you may create array of valid forms for that.

Comments

0

Use explode()

$arrVeg = array();
$arrFruit = array();
$finalArr = array();

foreach($array as $k => $v){
  $explK = explode('_',$k);
  if($explK[0] == 'vegetable'){
     $arrVeg[$k] = $v;
  } elseif($explK[0] == 'fruit') {
     $arrFruit[$k] = $v;
  }
}

$finalArr['vegetables'] = $arrVeg;
$finalArr['fruits'] = $arrFruit;

Comments

0

Use simple PHP array traversing and substr() function.

<?php
$arr = array();
$arr['vegetable_image'] = 'vegetable_image';
$arr['vegetable_name'] = 'vegetable_name';
$arr['vegetable_description'] = 'vegetable_description';
$arr['fruit_image'] = 'fruit_image';
$arr['fruit_name'] = 'fruit_name';
$arr['fruit_description'] = 'fruit_description';

$fruits = array();
$vegetables = array();
foreach ($arr as $k => $v) {
    if (substr($k, 0, 10) == 'vegetable_') {
        $vagetables[$k] = $v;
    }
    else if (substr($k, 0, 6) == 'fruit_') {
        $fruits[$k] = $v;
    }
}
print_r($fruits);
print_r($vagetables);

Working Example

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.