Reputation: 233
I'm trying to insert an array or (if there is one already declared) add element to it.
Print_r output of $data array:
Array
(
[0] => Array
(
[0] => 7727368
[1] => Array
(
)
)
[1] => Array
(
[0] => 7727284
[1] => Array
(
[0] => Array
(
[0] => 7543419783
)
)
)
[2] => Array
(
[0] => 7787473
[1] => Array
(
[0] => Array
(
[0] => 7771723347
)
[1] => Array
(
[0] => 8458457
)
)
)
)
PHP Code:
$idaukcji = $_POST['idaukcji'];
$modid = $_POST['modid'];
foreach($data as $item){
foreach($item[1] as $subitem){
if($subitem[0]==$idaukcji){
if(array_key_exists('1',$subitem)){
array_push($subitem[1],$modid);
}
else{
array_push($subitem, array($modid));
}
}
$tobase = base64_encode(serialize($item[1]));
$sql="UPDATE data SET allegroaucnum='$tobase' WHERE wfnum = '$item[0]'";
mysqli_query($conn,$sql) or die(mysqli_error($conn));
}
}
The script should check if there is an existing array on index [1] of most deeply nested array if it is declared then it should add $modid
to that array, If not then create the array with $modid
as an element.
Actual code gives what we can see above (in print_r) but if I add
echo $subitem[1][0];
after (inside else)
array_push($subitem, array($modid));
It does return $modid value which somehow is not shown in print_r
Thanks for the help in advance.
Upvotes: 0
Views: 72
Reputation: 51
try this function:
function flat_array($array, &$result)
{
if(!is_array($array)) {
return $array;
}
foreach ($array as $key => $item) {
if(is_array($item)) {
unset($array[$key]);
flat_array($item, $result);
} else {
$result[] = $item;
}
}
return $array;
}
execution code
flat_array($array, $result);
$result = array_unique($result); // if you want to make sure thar each value is unique
Upvotes: 1