How to make a list out of a 3D array inner data PHP

521

Below there is the code of the array. I'm trying to make a list of the field categories in the inner array and when the fiel is empty to write 'without category'. And I simply can't do it. I have been trying with two foreach nested saving the list into a new array but I can't quite get it right.

     Array(
 'type' => 'success',
 'value => array (
     0 => array (
         'id' => 1,
         'joke' => 'Chuck Norris uses ribbed condoms inside out, so he gets the pleasure.',
          'categories' => array());
     1 => array (
          'id' => 2,
          'joke' => 'MacGyver can build an airplane out of gum and paper clips. Chuck Norris can kill him and take it.',
          'categories' => array(              
            [0] => nerdy                        
            ));
      2 => array (
          'id' => 3,
          'joke' => 'MacGyver can build an airplane out of gum and paper clips. Chuck Norris can kill him and take it.',
          'categories' => array(              
            [0] => explicit
            ));
   );
)

//this is what I am trying without luck

$output = array();

    foreach($response as $row){
        foreach($row as $cat){
            $output[] = $cat['categories'];
        }
    }

Thanks!

38

Answer

Solution:

First of all there's wrong syntax in your array, semicolons instead of commas and array keys in square brackets. Here is the correct syntax.

$response = Array(
    'type' => 'success',
    'value' => array (
        0 => array ('id' => 1, 'joke' => 'Chuck Norris uses ribbed condoms inside out, so he gets the pleasure.', 'categories' => array()),
        1 => array ('id' => 2, 'joke' => 'MacGyver can build an airplane out of gum and paper clips. Chuck Norris can kill him and take it.',
            'categories' => array(0 => 'nerdy')),
        2 => array (
          'id' => 3,
          'joke' => 'MacGyver can build an airplane out of gum and paper clips. Chuck Norris can kill him and take it.',
          'categories' => array(
            0 => 'explicit'))));

Now the output. You want an output where 'without category' field will be a string, right? Soprint_r of the result array would look like this.

Array
(
    [0] => without category
    [1] => Array
        (
            [0] => nerdy
        )

    [2] => Array
        (
            [0] => explicit
        )

)

If yes, here is how you need to unwrap your array.

foreach($response as $row) {
    if (is_array($row)) {
        foreach($row as $cat) {
            if (empty($cat['categories'])) {
                $output[] = 'without category';
            } else {
                $output[] = $cat['categories'];
            }
        }
    }
}

People are also looking for solutions to the problem: php - How to secure user's images on a server?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.