Home > Back-end >  PHP Multidimensional Array implode?
PHP Multidimensional Array implode?

Time:11-23

I have an array

'Somethings' => 
    array (
      'Something' => 
      array (
        0 => 
        array (
          '@att' => 
          array (
            'Code' => '0',
          ),
          'Fruit' => 'Apple',
        ),
        1 => 
        array (
          '@att' => 
          array (
            'Code' => '3',
          ),
          'Fruit' => 'Banana',
        ),
        2 => 
        array (
          '@att' => 
          array (
            'Code' => '1',
          ),
          'Fruit' => 'Pear',
        )

The list continues. What would be the best way to transform (implode I assume) all "Fruits" as comma separated OR in a new array containing only the name elements?

Fruits are a child of @att codes which is the only problem.

Thanks for the help!

CodePudding user response:

This is as simple as just iterating through the array and grabbing each fruit under the Fruit index, then inserting it to a separate array.

$fruits = [
    "somethings" => [
        "something" => [
            0 => [
                "@att" => [
                    "code" => "0"
                ],
                "Fruit" => "Apple",
            ],
            1 => [
                "@att" => [
                    "code" => "3"
                ],
                "Fruit" => "Banana",
            ],
            2 => [
                "@att" => [
                    "code" => "1"
                ],
                "Fruit" => "Pear",
            ]
        ],
    ],
];

$sortedFruits = [];

// Iterate through the fruits array and add each fruit into the sortedFruits list.
foreach ($fruits["somethings"]["something"] as $fruit) {
    array_push($sortedFruits, $fruit["Fruit"]);
}

var_dump($sortedFruits);
/*
 * Output:
 * array (size=3)
 *      0 => string 'Apple' (length=5)
 *      1 => string 'Banana' (length=6)
 *      2 => string 'Pear' (length=4)
 */

Or to get the values comma separated, PHP has a built in implode function:

implode(",", $sortedFruits)

CodePudding user response:

We can also use array_walk_recursive function for multidimensional arrays:

$your_array = array('Somethings' => 
        array(
          'Something' => 
          array(
            0 => 
            array(
              '@att' => 
              array(
                'Code' => '0',
              ),
              'Fruit' => 'Apple',
            ),
            1 => 
            array(
              '@att' => 
              array(
                'Code' => '3',
              ),
              'Fruit' => 'Banana',
            ),
            2 => 
            array(
              '@att' => 
              array(
                'Code' => '1',
              ),
              'Fruit' => 'Pear',
            )
          )
      )
      );
      
    
    $fruits_list="";
    $fruits_array = array();
    $callback = 
      function ($value, $key) use (&$fruits_array) {
         if($key == "Fruit"){
            array_push($fruits_array,$value);
            
         }
      };
    array_walk_recursive($your_array, $callback);
    $fruits_list = implode(',',$fruits_array);
    echo $fruits_list;
  • Related