I have the following array:
array(4) {
[0]=> array(1) {
[0]=> array(3) {
[0]=> string(11) "art_7880" [1]=> string(1) "1" [2]=> int(2950)
}
[1]=> array(3) {
[0]=> string(8) "art_7880" [1]=> string(1) "1" [2]=> int(2950)
}
[2]=> array(3) {
[0]=> string(8) "art_7880" [1]=> string(1) "1" [2]=> int(2950)
}
[3]=> array(3) {
[0]=> string(8) "art_7883" [1]=> string(1) "1" [2]=> int(4335)
}
}
In the global array I would like to find an array with element (ex. element art_7880
) and then I would like to add in a global array one array with an element art_7880
.
For ex.: find element, ex. art_7880
In the global array, the element would be an array with art_7880
- [0]=> string(8) "art_7880" [1]=> string(1) "1" [2]=> int(2950)
I need to get this array [0]=> string(8) "art_7880" [1]=> string(1) "1" [2]=> int(2950)
and add or remove to the global array once.
This is the code I used:
foreach ($all_array as $keys => $elms) {
if(in_array('art_7880', $elms) && !in_array('art_7880', $arr_uniq)){
$arr_uniq[] = ''art_7880'';
var_dump($elms); //should been show `[0]=> string(11) "art_7880" [1]=> string(1) "1" [2]=> int(2950)`
}
But It doesn't work...
Can somebody please tell me where the error is?
Hmmm, I think the problem is that you have two sets of quotes around "art_7880", which end up giving you an error like "Parse error: syntax error, unexpected 'art_7880' (T_STRING)...". The following code should then provide the correct output:
foreach ($all_array as $keys => $elms) {
if(in_array('art_7880', $elms) && !in_array('art_7880', $arr_uniq)){
$arr_uniq[] = 'art_7880';
var_dump($elms);
}
}
Unless your issue is (possibly also) that you've got a 3D array here, but you're only treating it as a 2D one. (Because I noticed you've got a lot of nested array there.) In which case, this should give the correct output:
foreach ($all_array[0] as $keys => $elms) {
if(in_array('art_7880', $elms) && !in_array('art_7880', $arr_uniq)){
$arr_uniq[] = 'art_7880';
var_dump($elms);
}
}
Hope one of these code snippets were helpful. Good luck!