0

I have an array which looks like the following

array:7 [▼
  0 => array:7 [▼
    "id" => "62"
    "name" => "creativeOption"
    "label" => "Other"
    "value" => "dfsdfsdfsdf"
    "someId" => "14"
  ]
  1 => array:7 [▼
    "id" => "60"
    "name" => "creativeOption"
    "label" => "checkboxSelection"
    "value" => "AnimSomething"
    "someId" => "14"
  ]
  2 => array:7 [▼
    "id" => "61"
    "name" => "creativeOption"
    "label" => "checkboxSelection"
    "value" => "Something"
    "someId" => "14"
  ]
  3 => array:7 [▼
    "id" => "59"
    "name" => "creativeNumber"
    "label" => "Something"
    "value" => ""
    "someId" => "14"
  ]
  4 => array:7 [▼
    "id" => "57"
    "name" => "creativeNumber"
    "label" => "Something"
    "value" => "Something 2 Info"
    "someId" => "14"
  ]
]

I am interested in getting the value where the name is creativeOption. At the moment, I have this

foreach ($array as $data) {
    foreach($data as $key => $value) {
        if($value == 'creativeOption') {

        }
    }
}

So that allows me to find all the creativeOption, but how would I then go about getting their value?

3
  • creativeOption is the value. What do you want to get? Commented Feb 24, 2016 at 13:12
  • @Mr.Engineer Maybe he's refering value to "value" => "blabla" Commented Feb 24, 2016 at 13:14
  • if($value['name'] == 'creativeOption') { Commented Feb 24, 2016 at 13:15

4 Answers 4

5

Just change your query a little

foreach ($array as $data) {
    if($data['name'] == 'creativeOption') {
        echo $data['value'];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

You could try array_filter:

$array = [
  0 =>  [
    "id" => "62",
    "name" => "creativeOption",
    "label" => "Other",
    "value" => "dfsdfsdfsdf",
    "someId" => "14"
  ],
  1 =>  [
    "id" => "60",
    "name" => "creativeOption",
    "label" => "checkboxSelection",
    "value" => "AnimSomething",
    "someId" => "14"
  ],
  2 =>  [
    "id" => "59",
    "name" => "creativeNumber",
    "label" => "Something",
    "value" => "",
    "someId" => "14"
  ],
];

print_r(array_filter($array, function($v, $k){ 
    return($v['name'] === 'creativeOption');
}));

Does that help? Just tested and updated my answer. Have fun.

Comments

0
foreach ($arrays as $array) {
    foreach ($array as $key => $value) {
        if($key=='name' && $value=='creativeOption'){
            echo $array['value'];
            echo '<br>';
        }
    }
}

tries like this it should work

Comments

-1

try this code.

foreach ($array as $data) {
        foreach($data as $key => $value) {
            if($value == 'creativeOption') {
                echo $key['value'];
            }
        }
    }

1 Comment

Did you even try this? Just no, it won't work. Check @HalayemAnis answer for the correct way to do this, but even then you don't need the second foreach.

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.