0

I have an array $bobby with the following arrays inside. It is sorted by id.

1
    id="1"  
    color="blue"
    size="7"
    height="10"
    beebop="z"

2
    id="2"  
    color="red"
    size="64"
    height="52"
    beebop="y"
3
    id="3"  
    color="pink"
    size="72"
    height="39"
    beebop="not_x"

I am having trouble creating the php function that will create a simplified array ($bobby_simplified) which only contains two values, id and color? So, the new array would look like this:

1
    id="1"  
    color="blue"
2
    id="2"  
    color="red"
3
    id="3"  
    color="pink"

Also, in that function, can we sort by color ascending?


I tried the following but with no luck:

            foreach ($bobby AS $bobby_simplified) {
                $id = $bobby_simplified['id'];
                $color = $bobby_simplified['color'];
            }
LF00
  • 27,015
  • 29
  • 156
  • 295
ian
  • 303
  • 3
  • 15
  • `Can I create a php function to create a new array` - yes. What have you tried? `can we sort by color ascending?` - yes. There's plenty of questions on how to sort arrays in PHP on Stackoverflow, like [this one](http://stackoverflow.com/questions/17364127/how-can-i-sort-arrays-and-data-in-php). – scrowler May 10 '17 at 01:28
  • Thanks Robbie Averill. I added the function I tried for the first part. – ian May 10 '17 at 01:39

4 Answers4

1

You can use the array_map() function to get an new array, and array_slice() to get first two elements of the subarray. Check the live demo.

$simplified = array_map(function($v){return array_slice($v, 0, 2, true);}, $array);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
LF00
  • 27,015
  • 29
  • 156
  • 295
  • Thanks Kris Roofe: In your fxn, I take it $v=$bobby in my function and $array=$bobby_simplified? – ian May 10 '17 at 01:41
  • $array is you unsimplified array, the array_map() funciton's output is you simplified array. – LF00 May 10 '17 at 01:42
1

You can sort the array alphabetically by color using:

usort($arr, function($a, $b) { return strcmp($a['color'], $b['color']); });
Enstage
  • 2,106
  • 13
  • 20
1

try something like this

foreach ($bobby AS $bobby_simplified){
$res = array("id"=>$bobby_simplified['id'],"color"=>$bobby_simplified['color']);
}
print_r($res);

enda
  • 30
  • 6
-1

try this, already tested, added sorting by color

$array = [1=>['id'=> '1', 'color'=> 'blue', 'size'=>'7', 'height'=>'10', 'beebop'=>'z'],
          2=>['id'=> '2', 'color'=> 'red', 'size'=>'64', 'height'=>'52', 'beebop'=>'y'],
          3=>['id'=> '3', 'color'=> 'pink', 'size'=>'72', 'height'=>'39', 'beebop'=>'not_x'],
         ];

foreach($array as $arr){

        $arr = array_splice($arr,0,2);
        print_r($arr); 

        $array2[] = $arr;
}
    echo "<br>";
    print_r($array2);

    echo "<br>";



function sortBy($field, &$array, $direction = 'asc')
{
    usort($array, create_function('$a, $b', '
        $a = $a["' . $field . '"];
        $b = $b["' . $field . '"];

        if ($a == $b)
        {
            return 0;
        }

        return ($a ' . ($direction == 'desc' ? '>' : '<') .' $b) ? -1 : 1;
    '));

    return true;
}

sortBy('color',   $array2, 'asc');
print_r($array2);
apelidoko
  • 782
  • 1
  • 7
  • 23