0

Can someone help me out.

I'm looking for an solution how to check if in $testArray there is an array with ['value']['content'] == 1

I have tried:

$bool = in_array('1', array_column($testArray, 'content'));
var_dump($bool);

But than I get an return FALSE, but if you take a look at the testArray below you see the first array has ['value']['content'] => '1';

<?php

        $testArray = array(
            array(
                'id' => '414-b9108bbe-66e3-4c1e-8a51-03b7b8a0d17c',
                'position' => 0,
                'type' => 'original',
                'value' => array
                    (
                        'comment' => '',
                        'content' => '1',
                        'info' => '',
                        'insight' => '',
                        'label' => '',
                        'report' => '',
                    )

            ),
            array(
                'id' => '414-b9108bbe-66e3-4c1e-8a51-03b7b8a0d18c',
                'position' => 0,
                'type' => 'original',
                'value' => array
                    (
                        'comment' => '',
                        'content' => '',
                        'info' => '',
                        'insight' => '',
                        'label' => '',
                        'report' => '',
                    )

            ),
        );

        echo '<pre>';
        $bool = array_search('content', array_column($testArray, 'value'));
        var_dump($bool);
        echo '</pre>';

I think its because of the value is an other Array because i do:

$bool = in_array('original', array_column($testArray, 'type'));

Output: true.

7
  • && $block['answer'][$which] === 1 ? Commented Sep 28, 2021 at 15:34
  • 1
    I fail to see a connection between $block['answer'] and those arrays, sorry. Commented Sep 28, 2021 at 16:45
  • Why does your code check for answer within the array, but the examples you list don't mention it? Are you passing in the entire $testArray1 or $testArray2 as the input, or only a subset of the array? Commented Sep 28, 2021 at 20:09
  • Forgot the function, its a very long array and alot of checks. There are alot of diffrent arrays. My bad to place it in the question im sorry.. I cant check of && $block['answer']['$wich'] because there start the array so there you have the $key[0] and $key[1] and there value and there the content.. If you know what i mean. Commented Sep 28, 2021 at 21:28
  • 1
    array_filter($testArray, function ($x) {return $x['value']['content']=== '1';}); Commented Sep 29, 2021 at 9:09

2 Answers 2

2

You can try this. (Sorry for my bad english,hope you can understand)

// This is a two-dimensional array, you only need to do the array_column operation once more
var_dump(in_array('1', array_column(array_column($testArray, 'value'), 'content')));
Sign up to request clarification or add additional context in comments.

1 Comment

Please add some explanation to your answer such that others can learn from it
0

Try as below simple you can find any value from your array

function searcharray($value, $key, $array) {
    
   foreach ($array as $k => $val) {
       if ($val[$key] == $value) {
           foreach ($val as $k1 => $val1) {
               if ($val1[$key] == $value) {
                   return true;
               }
           }
           return true;
       }else{
           foreach ($val as $k1 => $val1) {
               if ($val1[$key] == $value) {
                   return true;
               }
           }
           
       }
   }
   return false;
}

var_dump(searcharray('0','position',$testArray));`enter code here`

1 Comment

Please add some explanation to your answer such that others can learn from it

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.