php - Pushing array into array inside of array
Get the solution ↓↓↓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.
Answer
Solution:
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
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: sqlstate[hy000] [1698] access denied for user 'root'@'localhost'
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.