php


How to replace reference to element with its value?


in case of tl;dr: setting up array with references and then changing a copy of said array still preserves references and elements of all copies are changed at the same time. Need a workaround of replacing reference with the value that it points to.
I'm getting a complicated problem with using references in PHP Arrays :(
I have a flat array of unique elements with two properties: order and level.
Order represents element's order number from 1 to n in like table of contents way. Level represents which level "subchapter" the element is.
Example:
First brackets are element IDs which are random but unique:
[1][order:1][level:1]
----[7][order:2][level:2]
----[4][order:3][level:2]
---- ----[2][order:4][level:3]
[3][order:5][level:1]
[6][order:6][level:1]
----[5][order:7][level:2]
.
.
.
Remember, this is flat array of elements, above its just a visual representation.
Now I have tried to put them into array of form:
[1][children] => [
[7],
[4][children] => [
[2]
]
]
[3],
[6][children] => [
[5]
]
Which would represent a tree structure -ish..
I do this by first ordering them by order:
foreach($elements as $element){
$ordered_elements[$element['order']] = $element;
}
Then I shift each element under the correct parent:
foreach($ordered_elements as &$child){
if($child['level'] > 1){
$ordered_elements[$last_parent[$child['level']-1]]['children'][$child['content_id']] = &$child; // I think this is problematic line!!!
}
$last_parent[$child['level']] = $child['sort_order'];
}
Some of the elements stayed on root (first) level that shouldnt be there:
foreach($ordered_elements as &$child){
if($child['level'] == 1){
$ordered_elements[$child['content_id']] = $child;
}
unset($ordered_elements[$child['sort_order']]);
}
Now when this is done, the template array is ready. Now I start getting some data from query with element_id and user_id.
I want to set up a new table "users" that would have this previously made array for each user and I would be able to change its elements per user.
users[1]['elements'] = $ordered_elements;
users[2]['elements'] = $ordered_elements;
This function should return element by reference from user's own pool of elements, so we can change it directly into the users[x]['elements'][x]:
function &get_element_from_array(&$array, $searchValue){
$status = false;
foreach($array as $id => &$subtree) {
if ($id === $searchValue) {
return $subtree;
}
if (isset($subtree['children'])) {
$subsearch = &$this->get_element_from_array($subtree['children'], $searchValue);
if ($subsearch != false) {
return $subsearch;
}
}
}
return $status;
}
That means If i want to change element 5 from user 2 I need to call it like this:
$element = &get_element_from_array(users[2]['elements'], 5);
$element['visited'] = true;
This is where the problem occurs: I have just changed this element in user2 AND user1 array of elements.
I hope I didnt wrote this thing too long, was going for good explanation.

Related Links

Send value though Ajax on Condition?
Querying many-to-many relation and some more data with Laravel (Lumen/Eloquent)
How to store HTML string in JSON in PHP?
Im having trouble refreshing my webpage [closed]
PHP exporting data to CSV in multiple sheets
SQL Injection Questions [duplicate]
SQL Query multiple where clause prioritize
Getting a post value from class instead of value
Using SimpleSAML with Yii2 - Multiple Autoload Issue
How to play MP3 format data in browser?
Sanitizing user data for use in the page title without encoding to entities?
PHP MySQL Showing image from Database with the right id
'Unexpected echo' PHP error message - outputting php variable in html input 'value' field [duplicate]
Azure WebJob returns “The system cannot find the file specified”
PayPal - Check tickets still available before sending the money
CodeIgniter: How to synchronize local and remote databases

Categories

HOME
memory
azure-functions
matrix
opencart
opencv-python
kendo-dropdown
bots
writefile
devexpress
ios-simulator
haxe
firebase-database
u-boot
angular2-template
kurento
bellman-ford
elastic-load-balancer
phpstorm-2017.1
bnf
google-tasks-api
closures
pe
maven-surefire-plugin
rxjs5
channel
substring
tortoisemerge
credit-card
location-services
shapeless
android-collapsingtoolbar
ms-media-foundation
winrm
assertions
sspi
grunt-contrib-watch
utf
python-jira
plane
opal-framework
fiware-wirecloud
morphline
asihttprequest
doctrine-extensions
xcode8.2
karabiner
spring.net
network-protocols
ltrace
ipywidgets
e
openquery
callouts
wolfram-language
noclassdeffounderror
codecharge
frame-grab
persistent-object-store
gravatar
univocity
android-expansion-files
android-studio-import
canopy
apple
replicaset
sequence-sql
phpredis
fuseesb
innerhtml
adhoc-polymorphism
inequality
producer
squeezebox
miglayout
tabletools
proxy-server
strcmp
ctp
libxml-js
delphi-xe4
proc-open
user-forums
towers-of-hanoi
clrprofiler
getproperty
haiku
fork-join
css-friendly
simile
ognl
pyjamas
bass
tessellation
icicles
tabbarcontroller
principles
rawcontacts
autobench
jvm-bytecode

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile