Is there a chance to array_slice is bugged (using PHP 8.0.12)
I were looking for best way to get few elements(limited) of array with possible offset so tried to array_slice but its keep return me wrong index then i expect
I have array with objects from database and they looks like(ill write only id)
$questions simplified to only id
$limit = 1;
$questions = array(
array(
'id' => 2,
'stage' => 1,
'question_order' => 0,
'many_answers' => 0,
'points' => 5.5,
'spread_points_in_time' => 0,
'question' => "Czy Johnny Deep grał Jack'a Sparow'a?",
'time' => "00:00:10"
),
array(
'id' => 3,
'stage' => 1,
'question_order' => 0,
'many_answers' => 0,
'points' => 4.5,
'spread_points_in_time' =>0,
'question' => "Producent laptopa Darka",
'time' => "00:00:00"
),
array(
'id' => 1,
'stage' => 1,
'question_order' => 0,
'many_answers' => 0,
'points' => 10,
'spread_points_in_time' => 0,
'question' => "Która klawiatura jest mechniczna",
'time' => "00:00:10"
),
array(
'id' => 8,
'stage' => 2,
'question_order' => 0,
'many_answers' => 0,
'points' => 30,
'spread_points_in_time' => 1,
'question' => "2*2",
'time' => "00:00:30"
),
array(
'id' => 4,
'stage' => 2,
'question_order' => 0,
'many_answers' => 0,
'points' => 30,
'spread_points_in_time' => 0,
'question' => "Który język programowania ma dostęp do komórek pamięci komputera?",
'time' => "00:00:30"
),
array(
'id' => 7,
'stage' => 2,
'question_order' => 0,
'many_answers' => 0,
'points' => 30,
'spread_points_in_time' => 0,
'question' => "2+2",
'time' => "00:00:30"
),
array(
'id' => 10,
'stage' => 3,
'question_order' => 1,
'many_answers' => 1,
'points' => 5,
'spread_points_in_time' => 0,
'question' => "Jaki jest symbol chemiczny srebra?",
'time' => "00:00:00"
),
array(
'id' => 11,
'stage' => 3,
'question_order' => 0,
'many_answers' => 1,
'points' => 5,
'spread_points_in_time' => 0,
'question' => "Jaka jest żywotność ważki?",
'time' => "00:00:00"
),
array(
'id' => 9,
'stage' => 3,
'question_order' => 0,
'many_answers' => 0,
'points' => 5,
'spread_points_in_time' => 0,
'question' => "W którym roku Titanic zatonął na Oceanie Atlantyckim 15 kwietnia podczas dziewiczej podróży z Southampton?",
'time' => "00:00:00"
)
);
echo json_encode(
array(
'all' => $questions,
'limit' => $limit,
'sliced' => array_slice($questions, 0, $limit),
)
);
so when i try array_splice(array, 0, 1) i would expect 1 so id should be 2 but instead i get object which have id 1
i also tried with flag to keep indexed but then i end up same but with true index of object with id 1 (then key is 2)
If i put limit as 2 then i get object with id 1 and also object with id 2 (flag to keep indexed also return me properly to objects 2 and 0)
Also tried to remove id from every object by
foreach($questions as $key => $question)
{
unset($questions[$key]['id'];
}
But this didnt changed anything
$questions source https://pastebin.com/wDfJdm9Z
After plenty of combinations i figured it out
I were sorting this array before everything with forcing keys
So in sorting it looked like
foreach($toSort as $key => $item)
{
foreach($ids as $idKey => $id)
{
if($item['id'] == $id)
{
$sorted[$idKey] = $item;
break;
}
}
}
So for example array could end up with wrong order of keys like:
array(
[1] => item2,
[0] => item1,
[2] => item3
);
I were expecting array gona shove key 0 to be index 0 but don't looks like
Setting something to exact key isn't same as set something to set at exact index
SOLUTION WAS add ksort($sorted) before return $sorted
I am trying to unset an item from session array, it unset once then brings back again.
for exg :-
if i click to remove itemId = 5, it removes from session array,
then i click to remove itemId = 6, then i see the old itemId = 5 is back in array again.
/*** delete item from cart ***/
public function deleteCartItem($id, $customerId, $cartId, $wishlistFlag=false) // Param: ProductID, CustomerID, CartID, WishlistFlag
{
$conn = $_SESSION['conn'];
$itemId = false;
$chk = array();
$sql = "DELETE FROM cart_items WHERE product_id=? AND cart_id=?";
$stmt = mysqli_prepare($conn, $sql);//$result = $conn->query($sql);
$stmt->bind_param('ii', $item['id'],$cartId);
$stmt->execute();
$itemId = $i;
echo $itemId;
$chk[] = $_SESSION['cart'];
echo'</br>';
print_r($chk);
}
}
if (isset($_SESSION['cart'][$itemId])){
unset($_SESSION['cart'][$itemId]);
}
}
<?php
$_SESSION['cart'] = array (
0 =>
array (
'id' => 6154353459,
'name' => 'pro one Night Out Mesh Tee',
'display_brand' => 'Widow',
'qty' => 1,
'price' => '28.0000',
'special_price' => 0,
'rule_price' => '',
'attributes' =>
array (
0 => 'BLACK',
1 => 'XX-Large',
),
'option80' => 18,
'option125' => 3457,
'child_product_id' => 61535897,
'giftcard' => '',
'whole_preorder' => 0,
'preorder_shipping_date' => NULL,
'available_qty' => 5,
'isRefundable' => true,
'max_sale_qty' => false,
'isFree' => 0,
'segment' =>
array (
'magento_product_id' => '65315459',
'product_id' => 'P19335935',
'quantity' => 1,
'name' => 'pro one Night Out Mesh Tee',
'brand' => 'Widow',
'price' => 28.0,
'special_price' => NULL,
'regular_price' => 28.0,
'size_type' => 'Regular',
'manufacturer' => 'Widow',
'trend' => 'WID goodie Hour 2020',
'microcategory' => 'Mesh Shirt',
'doll_category' => NULL,
'stock_quantity' => 10,
'sku' => 'S493660',
'child_stock_quantity' => 1,
'size' => 'XX-Large',
'swatch' => 'BLACK',
),
),
5 =>
array (
'id' => 615345465,
'name' => 'pro one Night Out Mesh Leggings',
'display_brand' => 'Widow',
'qty' => 1,
'price' => '30.0000',
'special_price' => 0,
'rule_price' => '',
'attributes' =>
array (
0 => 'BLACK',
1 => 'Medium',
),
'option80' => 18,
'option125' => 3454,
'child_product_id' => 6315890,
'giftcard' => '',
'whole_preorder' => '',
'preorder_shipping_date' => NULL,
'available_qty' => 37,
'isRefundable' => true,
'max_sale_qty' => false,
'isFree' => 0,
'segment' =>
array (
'magento_product_id' => '64334515465',
'product_id' => 'P193936',
'quantity' => 1,
'name' => 'pro one Night Out Mesh Leggings',
'brand' => 'Widow',
'price' => 30.0,
'special_price' => NULL,
'regular_price' => 30.0,
'size_type' => 'Regular',
'manufacturer' => 'Widow',
'trend' => 'WID goodie Hour 2020',
'microcategory' => 'Printed Leggings',
'doll_category' => NULL,
'stock_quantity' => 10,
'sku' => 'S493664',
'child_stock_quantity' => 10,
'size' => 'Medium',
'swatch' => 'BLACK',
),
),
2 =>
array (
'id' => 615445348,
'name' => 'pro two My Lashes Sunglasses',
'display_brand' => '',
'qty' => 1,
'price' => '15.0000',
'special_price' => 0,
'rule_price' => '',
'attributes' =>
array (
0 => 'BLACK',
1 => 'ONE SIZE',
),
'option80' => 18,
'option125' => 6212,
'child_product_id' => 6154345358,
'giftcard' => '',
'whole_preorder' => '',
'preorder_shipping_date' => NULL,
'available_qty' => 295,
'isRefundable' => true,
'max_sale_qty' => false,
'isFree' => 0,
'segment' =>
array (
'magento_product_id' => '6153453448',
'product_id' => 'P19645642180',
'quantity' => 1,
'name' => 'pro two My Lashes Sunglasses',
'brand' => '',
'price' => 15.0,
'special_price' => NULL,
'regular_price' => 15.0,
'size_type' => 'Regular',
'manufacturer' => 'CIEL',
'trend' => 'WID goodie Hour 2020',
'microcategory' => 'Cat Eye Sunglasses',
'category' => 'Accessories,Sunglasses,Cat Eye Sunglasses',
'special_category' => 'What\'s New,Char Test Category,Widow',
'doll_category' => NULL,
'stock_quantity' => 10,
'sku' => 'S486477',
'child_stock_quantity' => 10,
'size' => 'ONE SIZE',
'swatch' => 'BLACK',
),
),
3 =>
array (
'id' => 61465645461,
'name' => 'pro one Night Out goodie Dress',
'display_brand' => 'Widow',
'qty' => 1,
'price' => '45.0000',
'special_price' => 0,
'rule_price' => '',
'attributes' =>
array (
0 => 'BLACK',
1 => 'X-Large',
),
'option80' => 18,
'option125' => 3456,
'giftcard' => '',
'whole_preorder' => '',
'preorder_shipping_date' => NULL,
'available_qty' => 12,
'isRefundable' => true,
'max_sale_qty' => false,
'isFree' => 0,
'segment' =>
array (
'magento_product_id' => '616465461',
'product_id' => 'P1935446934',
'quantity' => 1,
'name' => 'pro one Night Out goodie Dress',
'brand' => 'Widow',
'price' => 45.0,
'special_price' => NULL,
'regular_price' => 45.0,
'size_type' => 'Regular',
'manufacturer' => 'Widow',
'trend' => 'WID goodie Hour 2020',
'microcategory' => 'goodie Dresses',
'category' => 'Clothing,Dresses,Mini,goodie Dresses',
'special_category' => 'Brandsgoodie,Char Test Category,Widow,Widow',
'doll_category' => NULL,
'stock_quantity' => 10,
'sku' => 'S493652',
'child_stock_quantity' => 10,
'size' => 'X-Large',
'swatch' => 'BLACK',
),
),
4 =>
array (
'id' => 6154566225,
'name' => 'Feelings Maxi Dress',
'display_brand' => 'Widow',
'qty' => 1,
'price' => '58.0000',
'special_price' => 0,
'rule_price' => '',
'attributes' =>
array (
0 => 'BLACK',
1 => 'Large',
),
'option80' => 18,
'option125' => 3455,
'child_product_id' => 61456455677,
'giftcard' => '',
'whole_preorder' => '',
'preorder_shipping_date' => NULL,
'available_qty' => 30,
'isRefundable' => true,
'max_sale_qty' => false,
'isFree' => 0,
'segment' =>
array (
'magento_product_id' => '614565225',
'product_id' => 'P1945652050',
'quantity' => 1,
'name' => 'Phantom Feelings Maxi Dress',
'brand' => 'Widow',
'price' => 58.0,
'special_price' => NULL,
'regular_price' => 58.0,
'size_type' => 'Regular',
'manufacturer' => 'Widow',
'trend' => 'WID goodie Hour 2020',
'microcategory' => 'Maxi Dresses',
'category' => 'Dresses,Midi & Maxi,Maxi Dresses',
'special_category' => 'Brandsgoodie,Char Test Category,Widow,Widow',
'doll_category' => NULL,
'stock_quantity' => 10,
'sku' => 'S485794',
'child_stock_quantity' => 10,
'size' => 'Large',
'swatch' => 'BLACK',
),
)
);
$itemId = 5; // comes dybamically;
if ( isset($_SESSION['cart'][$itemId])) {
unset($_SESSION['cart'][$itemId]);
}
?>
I am not getting it,
Any thoughts ?
You can also take a look at the sandbox- http://sandbox.onlinephpfunctions.com/code/6bab394d1da39a301ce82b974022da7b7efaface
Please refer to the whole code here - PHP SESSION UPDATE ISSUE
Thankyou
You're proably re-reading session-data so that deletion you're making is overridden.
<?php
$cart = $_SESSION['cart'];
unset($cart[5]);
echo 'deletion confirmed';
//later in execution..
//accidental re-read of cart from session-variable.
//BAD CODE STARTS HERE
$cart = $_SESSION['cart']; //error since you're re-reading cart from session.
//BAD CODE END
//calc totals in basket or something..
$totals = 0;
foreach($cart as $item) {
$totals = $totals + $item['qty'] * $item['price'];
}
//somewhere in the script, the session variable is updated.
$_SESSION['cart'] = $cart;
GOOD TO KNOW
PHP passes most variables by value as default, meaning that
$cart = $_SESSION['cart'];
copies the content of $_SESSION['cart'] into $cart.
Any change made to $cart will not automatically be updated in $_SESSION['cart'].
If you don't want a copy but a reference, you may use:
$cart = &$_SESSION['cart'];
and you will not need to save $cart into $_SESSION.
(Just make sure $_SESSION['cart'] is initialized when making the reference)
** AND **
Session variables tend to grow to a mess.. Use getters and setters in your application.
The best approach is to after unset get the updated array from session and declare it again
$cart_array = array (
0 =>
array (
'id' => 6154353459,
'name' => 'pro one Night Out Mesh Tee',
'display_brand' => 'Widow',
'qty' => 1,
'price' => '28.0000',
'special_price' => 0,
'rule_price' => '',
'attributes' =>
array (
0 => 'BLACK',
1 => 'XX-Large',
),
'option80' => 18,
'option125' => 3457,
'child_product_id' => 61535897,
'giftcard' => '',
'whole_preorder' => 0,
'preorder_shipping_date' => NULL,
'available_qty' => 5,
'isRefundable' => true,
'max_sale_qty' => false,
'isFree' => 0,
'segment' =>
array (
'magento_product_id' => '65315459',
'product_id' => 'P19335935',
'quantity' => 1,
'name' => 'pro one Night Out Mesh Tee',
'brand' => 'Widow',
'price' => 28.0,
'special_price' => NULL,
'regular_price' => 28.0,
'size_type' => 'Regular',
'manufacturer' => 'Widow',
'trend' => 'WID goodie Hour 2020',
'microcategory' => 'Mesh Shirt',
'doll_category' => NULL,
'stock_quantity' => 10,
'sku' => 'S493660',
'child_stock_quantity' => 1,
'size' => 'XX-Large',
'swatch' => 'BLACK',
),
),
5 =>
array (
'id' => 615345465,
'name' => 'pro one Night Out Mesh Leggings',
'display_brand' => 'Widow',
'qty' => 1,
'price' => '30.0000',
'special_price' => 0,
'rule_price' => '',
'attributes' =>
array (
0 => 'BLACK',
1 => 'Medium',
),
'option80' => 18,
'option125' => 3454,
'child_product_id' => 6315890,
'giftcard' => '',
'whole_preorder' => '',
'preorder_shipping_date' => NULL,
'available_qty' => 37,
'isRefundable' => true,
'max_sale_qty' => false,
'isFree' => 0,
'segment' =>
array (
'magento_product_id' => '64334515465',
'product_id' => 'P193936',
'quantity' => 1,
'name' => 'pro one Night Out Mesh Leggings',
'brand' => 'Widow',
'price' => 30.0,
'special_price' => NULL,
'regular_price' => 30.0,
'size_type' => 'Regular',
'manufacturer' => 'Widow',
'trend' => 'WID goodie Hour 2020',
'microcategory' => 'Printed Leggings',
'doll_category' => NULL,
'stock_quantity' => 10,
'sku' => 'S493664',
'child_stock_quantity' => 10,
'size' => 'Medium',
'swatch' => 'BLACK',
),
),
2 =>
array (
'id' => 615445348,
'name' => 'pro two My Lashes Sunglasses',
'display_brand' => '',
'qty' => 1,
'price' => '15.0000',
'special_price' => 0,
'rule_price' => '',
'attributes' =>
array (
0 => 'BLACK',
1 => 'ONE SIZE',
),
'option80' => 18,
'option125' => 6212,
'child_product_id' => 6154345358,
'giftcard' => '',
'whole_preorder' => '',
'preorder_shipping_date' => NULL,
'available_qty' => 295,
'isRefundable' => true,
'max_sale_qty' => false,
'isFree' => 0,
'segment' =>
array (
'magento_product_id' => '6153453448',
'product_id' => 'P19645642180',
'quantity' => 1,
'name' => 'pro two My Lashes Sunglasses',
'brand' => '',
'price' => 15.0,
'special_price' => NULL,
'regular_price' => 15.0,
'size_type' => 'Regular',
'manufacturer' => 'CIEL',
'trend' => 'WID goodie Hour 2020',
'microcategory' => 'Cat Eye Sunglasses',
'category' => 'Accessories,Sunglasses,Cat Eye Sunglasses',
'special_category' => 'What\'s New,Char Test Category,Widow',
'doll_category' => NULL,
'stock_quantity' => 10,
'sku' => 'S486477',
'child_stock_quantity' => 10,
'size' => 'ONE SIZE',
'swatch' => 'BLACK',
),
),
3 =>
array (
'id' => 61465645461,
'name' => 'pro one Night Out goodie Dress',
'display_brand' => 'Widow',
'qty' => 1,
'price' => '45.0000',
'special_price' => 0,
'rule_price' => '',
'attributes' =>
array (
0 => 'BLACK',
1 => 'X-Large',
),
'option80' => 18,
'option125' => 3456,
'giftcard' => '',
'whole_preorder' => '',
'preorder_shipping_date' => NULL,
'available_qty' => 12,
'isRefundable' => true,
'max_sale_qty' => false,
'isFree' => 0,
'segment' =>
array (
'magento_product_id' => '616465461',
'product_id' => 'P1935446934',
'quantity' => 1,
'name' => 'pro one Night Out goodie Dress',
'brand' => 'Widow',
'price' => 45.0,
'special_price' => NULL,
'regular_price' => 45.0,
'size_type' => 'Regular',
'manufacturer' => 'Widow',
'trend' => 'WID goodie Hour 2020',
'microcategory' => 'goodie Dresses',
'category' => 'Clothing,Dresses,Mini,goodie Dresses',
'special_category' => 'Brandsgoodie,Char Test Category,Widow,Widow',
'doll_category' => NULL,
'stock_quantity' => 10,
'sku' => 'S493652',
'child_stock_quantity' => 10,
'size' => 'X-Large',
'swatch' => 'BLACK',
),
),
4 =>
array (
'id' => 6154566225,
'name' => 'Feelings Maxi Dress',
'display_brand' => 'Widow',
'qty' => 1,
'price' => '58.0000',
'special_price' => 0,
'rule_price' => '',
'attributes' =>
array (
0 => 'BLACK',
1 => 'Large',
),
'option80' => 18,
'option125' => 3455,
'child_product_id' => 61456455677,
'giftcard' => '',
'whole_preorder' => '',
'preorder_shipping_date' => NULL,
'available_qty' => 30,
'isRefundable' => true,
'max_sale_qty' => false,
'isFree' => 0,
'segment' =>
array (
'magento_product_id' => '614565225',
'product_id' => 'P1945652050',
'quantity' => 1,
'name' => 'Phantom Feelings Maxi Dress',
'brand' => 'Widow',
'price' => 58.0,
'special_price' => NULL,
'regular_price' => 58.0,
'size_type' => 'Regular',
'manufacturer' => 'Widow',
'trend' => 'WID goodie Hour 2020',
'microcategory' => 'Maxi Dresses',
'category' => 'Dresses,Midi & Maxi,Maxi Dresses',
'special_category' => 'Brandsgoodie,Char Test Category,Widow,Widow',
'doll_category' => NULL,
'stock_quantity' => 10,
'sku' => 'S485794',
'child_stock_quantity' => 10,
'size' => 'Large',
'swatch' => 'BLACK',
),
)
);
if(!isset($_SESSION['cart'])) {
$_SESSION['cart'] = $cart_array;
} else {
$_SESSION['cart'] = (array) $_SESSION['cart'];
}
$itemId = 3;
if (isset($_SESSION['cart'][$itemId])) {
unset($_SESSION['cart'][$itemId]);
}
print_r($_SESSION['cart']);
Now it will be not back the remove item again, unless if you destroy the session
Why is there $item['id'] used in $stmt->bind_param( (instead of $id?). $item is not a parameter of the function.
Similarly, where does the variable $i come from in $itemId = $i;?
I have written a simplified version of your problem and it works as expected. It uses a form and buttons to give the array indices to the PHP script. If you have a different setup (like using Ajax) you need to clarify that.
session_start();
if (!isset($_SESSION['cart']) || sizeof($_SESSION['cart']) === 0)
{
echo('Resetting cart.' . PHP_EOL);
$_SESSION['cart'] = [0 =>'zero', 1 =>'one', 2 =>'two', 3 =>'three', 4 =>'four'];
}
echo('<pre>before ' . print_r($_SESSION['cart'], TRUE) . '</pre>');
$itemId = isset($_GET['itemid']) ? $_GET['itemid'] : FALSE;
if ($itemId !== FALSE)
{
if (isset($_SESSION['cart'][$itemId]))
{
echo('Unsetting »' . $itemId . '«.' . PHP_EOL);
unset($_SESSION['cart'][$itemId]);
}
else
echo('»' . $itemId . '« not in cart.' . PHP_EOL);
}
else
echo('No item ID given.' . PHP_EOL);
echo('<pre>after ' . print_r($_SESSION['cart'], TRUE) . '</pre>');
echo('<form method="get" action="' . $_SERVER['PHP_SELF'] . '">' . PHP_EOL);
foreach ($_SESSION['cart'] as $key=>$value)
echo('<button name="itemid" value="' . $key . '">' . $value . '</button>' . PHP_EOL);
echo('</form>' . PHP_EOL);
The manual has made it clear
If a globalized variable is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called.
To unset() a global variable inside of a function, then use the $GLOBALS array to do so:
<?php
function foo()
{
unset($GLOBALS['bar']);
}
$bar = "something";
foo();
?>
$stmt->bind_param('ii', $item['id'],$cartId);
$stmt->execute();
where is $item['id'] from?
Try using different variable names on your bind_param and declare their value before query execute() method.
On PHP 7.2, the built-in function exif_read_data returns different information to PHP 7.1
This is what I get when I use 7.2:
php -r 'var_export(exif_read_data("x.jpg", "ANY_TAG"));'
array (
'FileName' => 'x.jpg',
'FileDateTime' => 1543144462,
'FileSize' => 3564506,
'FileType' => 2,
'MimeType' => 'image/jpeg',
'SectionsFound' => 'ANY_TAG, IFD0, EXIF',
'COMPUTED' =>
array (
'html' => 'width="3968" height="2976"',
'Height' => 2976,
'Width' => 3968,
'IsColor' => 1,
'ByteOrderMotorola' => 1,
),
'ImageWidth' => 3968,
'ImageLength' => 2976,
'BitsPerSample' =>
array (
0 => 8,
1 => 8,
2 => 8,
),
'ImageDescription' => 'ptr',
'Make' => 'HUAWEI',
'Model' => 'STF-L09',
'Orientation' => 0,
'XResolution' => '72/1',
'YResolution' => '72/1',
'ResolutionUnit' => 2,
'Software' => 'STF-L09C432B120',
'DateTime' => '2018:11:24 15:11:58',
'YCbCrPositioning' => 1,
)
But when I run the exact same code on 7.1, I get much more EXIF data:
array (
'FileName' => 'x.jpg',
'FileDateTime' => 1543144462,
'FileSize' => 3564506,
'FileType' => 2,
'MimeType' => 'image/jpeg',
'SectionsFound' => 'ANY_TAG, IFD0, THUMBNAIL, EXIF, GPS, INTEROP',
'COMPUTED' =>
array (
'html' => 'width="3968" height="2976"',
'Height' => 2976,
'Width' => 3968,
'IsColor' => 1,
'ByteOrderMotorola' => 1,
'ApertureFNumber' => 'f/2.2',
'Thumbnail.FileType' => 2,
'Thumbnail.MimeType' => 'image/jpeg',
'Thumbnail.Height' => 384,
'Thumbnail.Width' => 512,
),
'ImageWidth' => 3968,
'ImageLength' => 2976,
'BitsPerSample' =>
array (
0 => 8,
1 => 8,
2 => 8,
),
'ImageDescription' => 'ptr',
'Make' => 'HUAWEI',
'Model' => 'STF-L09',
'Orientation' => 0,
'XResolution' => '72/1',
'YResolution' => '72/1',
'ResolutionUnit' => 2,
'Software' => 'STF-L09C432B120',
'DateTime' => '2018:11:24 15:11:58',
'YCbCrPositioning' => 1,
'Exif_IFD_Pointer' => 280,
'GPS_IFD_Pointer' => 8454,
'DeviceSettingDescription' => 'ipp' . "\0" . '',
'THUMBNAIL' =>
array (
'ImageWidth' => 512,
'ImageLength' => 384,
'Compression' => 6,
'Orientation' => 0,
'XResolution' => '72/1',
'YResolution' => '72/1',
'ResolutionUnit' => 2,
'JPEGInterchangeFormat' => 8802,
'JPEGInterchangeFormatLength' => 31647,
),
'DocumentName' => NULL,
'ExposureTime' => '30000000/1000000000',
'FNumber' => '220/100',
'ExposureProgram' => 2,
'ISOSpeedRatings' => 320,
'ExifVersion' => '0210',
'DateTimeOriginal' => '2018:11:24 15:11:58',
'DateTimeDigitized' => '2018:11:24 15:11:58',
'ComponentsConfiguration' => '' . "\0" . '',
'ShutterSpeedValue' => '298973/10000',
'ApertureValue' => '227/100',
'BrightnessValue' => '0/1',
'ExposureBiasValue' => '0/10',
'MeteringMode' => 5,
'LightSource' => 1,
'Flash' => 0,
'FocalLength' => '3950/1000',
'MakerNote' => 'Auto',
'SubSecTime' => '405238',
'SubSecTimeOriginal' => '405238',
'SubSecTimeDigitized' => '405238',
'FlashPixVersion' => '0100',
'ColorSpace' => 1,
'ExifImageWidth' => 3968,
'ExifImageLength' => 2976,
'InteroperabilityOffset' => 8424,
'SensingMethod' => 2,
'FileSource' => '',
'SceneType' => '',
'CustomRendered' => 1,
'ExposureMode' => 0,
'WhiteBalance' => 0,
'DigitalZoomRatio' => '100/100',
'FocalLengthIn35mmFilm' => 27,
'SceneCaptureType' => 0,
'GainControl' => 0,
'Contrast' => 0,
'Saturation' => 0,
'Sharpness' => 0,
'SubjectDistanceRange' => 0,
'GPSVersion' => '' . "\0" . '' . "\0" . '',
'GPSLatitudeRef' => 'N',
'GPSLatitude' =>
array (
0 => '51/1',
1 => '8/1',
2 => '49994201/1000000',
),
'GPSLongitudeRef' => 'W',
'GPSLongitude' =>
array (
0 => '2/1',
1 => '42/1',
2 => '59101467/1000000',
),
'GPSAltitudeRef' => '' . "\0" . '',
'GPSAltitude' => '7162/100',
'GPSTimeStamp' =>
array (
0 => '15/1',
1 => '11/1',
2 => '58/1',
),
'GPSProcessingMode' => 'GPS' . "\0" . '',
'GPSDateStamp' => '2018:11:24',
'InterOperabilityIndex' => 'R98',
'InterOperabilityVersion' => '0100',
)
Are there any modules or extensions or changes that I can make to get the full EXIF data when using 7.2? I'm on a shared Linux host, so have some limitations on what I can do.
This appears to be a bug in PHP - https://bugs.php.net/bug.php?id=72682 and https://abi-laboratory.pro/index.php?view=changelog&l=php&v=7.2.3
The only answer is to downgrade - or upgrade - to a version which doesn't have the bug.
I'm trying to build up a result array from a DB query and basicly I was wondering if the following would be possible
Array content:
Array
(
[0] => Array
(
[Section_Id] => 1
[Section_Name] => "Section1"
[Section_Parent] => NULL
[Section_Position] => 0
[Section_Depth] => 0
)
[1] => Array
(
[Section_Id] => 3
[Section_Name] => "Section2"
[Section_Parent] => NULL
[Section_Position] => 1
[Section_Depth] => 0
)
[2] => Array
(
[Section_Id] => 4
[Section_Name] => "SubSection1ForSection2"
[Section_Parent] => 3
[Section_Position] => 0
[Section_Depth] => 1
)
[3] => Array
(
[Section_Id] => 2
[Section_Name] => "SubSection1ForSection1"
[Section_Parent] => 1
[Section_Position] => 0
[Section_Depth] => 1
)
)
If you would Sort this one lets say on Section_Position it would return something like:
usort($sections, function($a, $b) {
return $a['section_position'] - $b['section_position'];
});
Section1
SubSection1ForSection2
SubSection1ForSection1
Section2
Whilst I need it to order the Sections with their respective childs:
Section1
SubSection1ForSection1
Section2
SubSection1ForSection2
I assume Somehow Duplicate Question is the way to think but I can't seem to find a way to make this work for me.
Is there a way to do this or do I have to make a workaround with forloop-gets so that I first get all the values for depth one and then using the name of the section to get all the values of depth two and so forth?
(Sorry for my english.)
Okay this might be an ugly solution but if you put everything in a function it look good :). The good thing is that it will work in your scenario.
Code:
$inputData = array(
array(
'Section_Id' => 1,
'Section_Name' => "Section1",
'Section_Parent' => NULL,
'Section_Position' => 1,
'Section_Depth' => 0,
),
array(
'Section_Id' => 2,
'Section_Name' => "Section2",
'Section_Parent' => NULL,
'Section_Position' => 0,
'Section_Depth' => 0
),
array(
'Section_Id' => 4,
'Section_Name' => "SubSection2ForSection2",
'Section_Parent' => 2,
'Section_Position' => 1,
'Section_Depth' => 1
),
array(
'Section_Id' => 5,
'Section_Name' => "SubSection1ForSection2",
'Section_Parent' => 2,
'Section_Position' => 0,
'Section_Depth' => 1
),
array(
'Section_Id' => 3,
'Section_Name' => "SubSection1ForSection1",
'Section_Parent' => 1,
'Section_Position' => 0,
'Section_Depth' => 1
)
);
$parentRecords = array();
$childRecords = array();
$sorted = array();
/* split in two collections */
foreach ($inputData as $sectionData) {
if (is_null($sectionData['Section_Parent'])) {
/* assume this is a parent */
$parentRecords[] = $sectionData;
} else {
/* assume we are on child row */
$childRecords[] = $sectionData;
}
}
/* now first order parents by Section_Position */
usort($parentRecords, function($a, $b) {
if ($a['Section_Position'] == $b['Section_Position']) {
return 0;
}
return $a['Section_Position'] > $b['Section_Position'] ? 1 : -1;
});
/* now the actual sorting */
foreach ($parentRecords as $parentData) {
$parentId = $parentData['Section_Id'];
/* now get all children of this parent */
$parentChildren = array();
foreach ($childRecords as $childData) {
if ($childData['Section_Parent'] == $parentId) {
$parentChildren[] = $childData;
}
}
/* now sort the children by Section_Position */
usort($parentChildren, function($a, $b) {
if ($a['Section_Position'] == $b['Section_Position']) {
return 0;
}
return $a['Section_Position'] > $b['Section_Position'] ? 1 : -1;
});
$sorted[] = $parentData;
$sorted = array_merge($sorted, $parentChildren);
}
echo '<pre>' . print_r($sorted, true) . '</pre>';
exit;
OUTPUT:
Array
(
[0] => Array
(
[Section_Id] => 2
[Section_Name] => Section2
[Section_Parent] =>
[Section_Position] => 0
[Section_Depth] => 0
)
[1] => Array
(
[Section_Id] => 5
[Section_Name] => SubSection1ForSection2
[Section_Parent] => 2
[Section_Position] => 0
[Section_Depth] => 1
)
[2] => Array
(
[Section_Id] => 4
[Section_Name] => SubSection2ForSection2
[Section_Parent] => 2
[Section_Position] => 1
[Section_Depth] => 1
)
[3] => Array
(
[Section_Id] => 1
[Section_Name] => Section1
[Section_Parent] =>
[Section_Position] => 1
[Section_Depth] => 0
)
[4] => Array
(
[Section_Id] => 3
[Section_Name] => SubSection1ForSection1
[Section_Parent] => 1
[Section_Position] => 0
[Section_Depth] => 1
)
)
NOTE: first sort is done by respect to parents Section_Position and then to child's Section_Position
U P D A T E
First I want to say sorry to moderators for the long long discussion that we had with #Akorna but I needed to give him this code and I think it will do the job for the future. So #Akorna the code that should work for you is this one:
$inputData = array(
array(
'section_id' => 333,
'section_depth' => 1,
'section_parent' => 332,
'section_position' => 0,
'section_title' => 'Introduction'),
array(
'section_id' => 334,
'section_depth' => 1,
'section_parent' => 332,
'section_position' => 1,
'section_title' => 'Glossary'),
array(
'section_id' => 335,
'section_depth' => 1,
'section_parent' => 332,
'section_position' => 2,
'section_title' => 'Commands'),
array(
'section_id' => 336,
'section_depth' => 1,
'section_parent' => 332,
'section_position' => 3,
'section_title' => 'Components'),
array(
'section_id' => 337,
'section_depth' => 2,
'section_parent' => 336,
'section_position' => 0,
'section_title' => 'Introduction'),
array(
'section_id' => 407,
'section_depth' => 2,
'section_parent' => 401,
'section_position' => 2,
'section_title' => 'Web Application'),
array(
'section_id' => 338,
'section_depth' => 2,
'section_parent' => 336,
'section_position' => 1,
'section_title' => 'AbstractContainer'),
array(
'section_id' => 406,
'section_depth' => 2,
'section_parent' => 401,
'section_position' => 1,
'section_title' => 'Web Application'),
array(
'section_id' => 339,
'section_depth' => 2,
'section_parent' => 336,
'section_position' => 2,
'section_title' => 'ActionsContainer'),
array(
'section_id' => 340,
'section_depth' => 2,
'section_parent' => 336,
'section_position' => 3,
'section_title' => 'BrowserIncompatibility'),
array(
'section_id' => 404,
'section_depth' => 2,
'section_parent' => 402,
'section_position' => 3,
'section_title' => 'Web Application'),
array(
'section_id' => 341,
'section_depth' => 2,
'section_parent' => 336,
'section_position' => 4,
'section_title' => 'CollapsibleContainer'),
array(
'section_id' => 342,
'section_depth' => 2,
'section_parent' => 336,
'section_position' => 5,
'section_title' => 'DetailsContainer'),
array(
'section_id' => 343,
'section_depth' => 2,
'section_parent' => 336,
'section_position' => 6,
'section_title' => 'DynamicMenu'),
array(
'section_id' => 403,
'section_depth' => 2,
'section_parent' => 402,
'section_position' => 1,
'section_title' => 'Web Application'),
array(
'section_id' => 344,
'section_depth' => 2,
'section_parent' => 336,
'section_position' => 7,
'section_title' => 'Settings'),
array(
'section_id' => 345,
'section_depth' => 2,
'section_parent' => 336,
'section_position' => 8,
'section_title' => 'SubfilesViewer'),
array(
'section_id' => 346,
'section_depth' => 2,
'section_parent' => 336,
'section_position' => 9,
'section_title' => 'Taxonomy Management'),
array(
'section_id' => 402,
'section_depth' => 1,
'section_parent' => 400,
'section_position' => 2,
'section_title' => 'Web Application'),
array(
'section_id' => 401,
'section_depth' => 1,
'section_parent' => 400,
'section_position' => 1,
'section_title' => 'Web Application'),
array(
'section_id' => 347,
'section_depth' => 2,
'section_parent' => 336,
'section_position' => 10,
'section_title' => 'UploadQueue'),
array(
'section_id' => 400,
'section_depth' => 0,
'section_parent' => null,
'section_position' => 5,
'section_title' => 'Web Application'),
array(
'section_id' => 332,
'section_depth' => 0,
'section_parent' => null,
'section_position' => 3,
'section_title' => 'Web Application')
);
/* first order by section_depth and then by section_position */
$inputData = array_orderby($inputData, 'section_depth', SORT_ASC, 'section_position', SORT_ASC);
$parents = array();
$sortedByParent = false;
while (!$sortedByParent) {
$elems = array_splice($inputData, count($inputData) - 1, 1);
if (!count($elems)) {
$sortedByParent = true;
$inputData = array_merge($inputData, $parents);
continue;
}
$elem = $elems[0];
if ($elem['section_depth'] == 0) {
if (!isset($elem['children'])) {
$elem['children'] = array();
}
$parents[] = $elem;
} else {
$inputData = put_in_parent($elem, $inputData);
}
}
/* now we have $inputData in nice format like
* parent(child, child, child(child, child(child, child)), child(child(child(child)))),
* parent(child, child, child(child, child(child, child)), child(child(child(child))))
* */
$inputData = merge_children_recursively(array_reverse($inputData));
function merge_children_recursively($inputData) {
$children = array();
foreach ($inputData as $row) {
if (isset($row['children'])) {
/* this ksort call is necessary because the key is actually section_position */
ksort($row['children']);
$rowCopy = $row;
unset($rowCopy['children']);
$children[] = $rowCopy;
$children = array_merge($children, merge_children_recursively($row['children']));
} else {
$children[] = $row;
}
}
return $children;
}
function put_in_parent($elem, $inputData) {
foreach ($inputData as $k => $row) {
if ($row['section_id'] == $elem['section_parent']) {
if (!isset($inputData[$k]['children'])) {
$inputData[$k]['children'] = array();
}
$inputData[$k]['children'][$elem['section_position']] = $elem;
break;
}
}
return $inputData;
}
function array_orderby() {
$args = func_get_args();
$data = array_shift($args);
foreach ($args as $n => $field) {
if (is_string($field)) {
$tmp = array();
foreach ($data as $key => $row) {
$tmp[$key] = $row[$field];
}
$args[$n] = $tmp;
}
}
$args[] = &$data;
call_user_func_array('array_multisort', $args);
return array_pop($args);
}
echo '<pre>' . print_r($inputData, true) . '</pre>';
exit;
I did remove some stuff from the input data so I could orient myself. Just try to give your input data to the logic and let me know what is the result.