2

I have a multidimensional array like this

$array['value'][1][1]

Now i would like to implement if loop like this

if ($value = $array['value'][1][1]) {
echo "It works";
} 

Now it works if i assign the values like [1][1],[2][1].

Is it possible to compare the whole array.

I mean if the array looks like

array[value][1][1],array[value][2][1],..........,array[value][n][1]

It works should be echoed.

I tried like this.

if ($value = $array['value'][][]) {
echo "It works";
} 

But its not working. Can anyone give me the correct syntax?

3
  • what's the problem with using a for loop iteratively through the array? Commented Dec 14, 2012 at 17:03
  • 1
    can you give a little more information on what you're trying to accomplish? i'd love to help, but i'm not quite understanding the scope of what you're trying to accomplish. are you trying to ensure the array has the proper number of dimensions, or trying to iterate over each entry in each array and if present, print "It Works"? Commented Dec 14, 2012 at 17:07
  • @JoshuaBurns I have some text fields. Many of them can be duplicated by clicking 'add another' button. There is a field called amazon product. Near that form field i would like to display a link button to search amazon products. This link should be displayed only when the array looks like forms[amazon][*][1]. I hope you get what i'm trying to do. I tried the for loop solution. But it works only for first field. Commented Dec 14, 2012 at 17:26

4 Answers 4

2

I'm not sure if this is what you're looking for but you could try this function $value is 1 in your case

function($array,$value)
    foreach($array['amazon'] as $val){
        if($value != $val[1])return FALSE;
    }
    return TRUE;
}

The function runs through $array['amazon'][*] and checks condition for each value. If found FALSE for any it returns

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

1 Comment

Hey carl, Thanks for the answer. I have some text fields. Many of them can be duplicated by clicking 'add another' button. There is a field called amazon product. Near that form field i would like to display a link button to search amazon products. This link should be displayed only when the array looks like forms[amazon][*][1]. I hope you get what i'm trying to do. I tried the for loop solution. But it works only for first field.
1

Based on your comments of what you're trying to accomplish, I think the following may solve your dilemma. Let me know if I'm going the wrong direction with this or have any additional questions/concerns.

<?php
$forms = array(
    'amazon' => array(
        0 => array(
            1 => 111,
        ),
        1 => array(
            1 => 222,
        ),
        2 => array(
            1 => 333
        )
    )
);

$value = 333;

$it_works = False;
foreach($forms['amazon'] as $array) {
    if($array[1] == $value) {
        $it_works = True;
        break;
    }
}

if($it_works === True) {
    print 'It Works!';
}
?>

6 Comments

Hello mate, I'm not sure how to use for each loop here. Actually I have a function display_field($field); I should pass the value to display the input field. So actually when passing the value like form[amazon][1][1] it displays the field. Is it possible to use regex? I mean like this form[amazon][regex goes here][1]
if i'm understanding correctly, you want to see if any of the arrays contain an array with a key "1". if this is the case, you can't use regular expressions to access multiple keys at the same time. hence the foreach to check each value.
No. Actually I would like to use regex to check value like 1,2,3,..etc.
If ($value = form[amazon][regex matches number][1]) { echo "link";}
Here is the full function code. I need to display the button after line 72
|
0

You can try this:

$itWorks = true;

for( $a = 0; $a < sizeof( $array[value] ); $a ++ ){
    if ( $value != $array[value][$a][1] ){
        $itWorks = false;
        break;
    }
}

if( $itWorks ){
    echo "It works";
}

Of course you must replace [value] with a legitimate value

Comments

0
function check() {
    for($i = 0; $i < count($array[$value]); $i++) {
        if($value != $array[$value][$i][1])
            return FALSE;
    }
    echo 'It works!';
}

1 Comment

return to what? Set the false to a variable

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.