how to make dynamic associative array in Yii? - php

I am trying to make a dynamic associtive array but the thing is it just save the last key-value pair how can i store all the key-value pairs?
foreach ($_POST as $var => $value) {
// Does the model have this attribute? If not raise an error
if ($model->hasAttribute($var))
$model->$var = $value;
elseif ($profile->hasAttribute($var)) {
$storage = array($var => $value);//associative array
} else {
//var_dump ($var);
$this->_sendResponse(500, sprintf('Parameter <b>%s</b> is not allowed for model <b>%s</b>', $var, $_GET['model']));
}
}

You have only below error:
$storage = array($var => $value);//associative array
This line is creating a new array $storage every time, that's why you are getting only last key value pair.
Try this:
$storage = array();// initialize it as array
$storage[$var] = $value;// assign $value in $key index of $storage

In your code, you're always assigning the $storage to a new array instead of appending it.(Correct me if I'm wrong).
You should append the array this way.
<?php
foreach ($_POST as $var => $value) {
// Does the model have this attribute? If not raise an error
if ($model->hasAttribute($var))
$model->$var = $value;
else if ($profile->hasAttribute($var)) {
if (!is_array($storage))
$storage = [];
$storage[$var] = $value; //associative array
} else {
//var_dump ($var);
$this->_sendResponse(500, sprintf('Parameter <b>%s</b> is not allowed for model <b>%s</b>', $var, $_GET['model']));
}
}

Related

Convert a nested Array to Scalar and then reconvert the scalar to nested array

I have an array that looks like this.
$array = [
0 => 'abc',
1 => [
0 => 'def',
1 => 'ghi'
],
'assoc1' => [
'nassoc' => 'jkl',
'nassoc2' => 'mno',
'nassoc3' => '',
'nassoc4' => false
]
];
The $array can have numeric keys or be an assoc array or a mixed one. The level of nesting is not known. Also the values of the array can also be bool or null or an empty string ''
I need to able to convert this into a scalar array with key value pairs. And then later reconvert it back to the exact same array.
So the scalar array could look like
$arrayScalar = [
'0' => 'abc',
'1[0]' => 'def',
'1[1]' => 'ghi',
'assoc1[nassoc]' => 'jkl',
'assoc1[nassoc2]' => 'mno',
'assoc1[nassoc3]' => '',
'assoc1[nassoc4]' => false
];
And then later be able to get back to the initial $array.
I wrote a parser and it does not currently handle bool values correctly.
I have a feeling this is at best a super hacky method to do what I am after. Also I have been able to test it only so much.
function flattenNestedArraysRecursively($nestedArray, $parent = '', &$flattened = [])
{
$keys = array_keys($nestedArray);
if (empty($keys)) {
$flattened[$parent] = 'emptyarray';
} else {
foreach ($keys as $value) {
if (is_array($nestedArray[$value])) {
$reqParent = (!empty($parent)) ? $parent . '|!|' . $value : $value;
$this->flattenNestedArraysRecursively($nestedArray[$value], $reqParent, $flattened);
} else {
$reqKey = (!empty($parent)) ? $parent . '|!|' . $value : $value;
$flattened[$reqKey] = $nestedArray[$value];
}
}
}
return $flattened;
}
function reCreateFlattenedArray($flatArray): array
{
$arr = [];
foreach ($flatArray as $key => $value) {
$keys = explode('|!|', $key);
$arr = $this->reCreateArrayRecursiveWorker($keys, $value, $arr);
}
return $arr;
}
function reCreateArrayRecursiveWorker($keys, $value, $existingArr)
{
//Outside to Inside
$keyCur = array_shift($keys);
//Check if keyCur Exists in the existingArray
if (key_exists($keyCur, $existingArr)) {
// Check if we have reached the deepest level
if (empty($keys)) {
//Return the Key => value mapping
$existingArr[$keyCur] = $value;
return $existingArr;
} else {
// If not then continue to go deeper while appending deeper levels values to current key
$existingArr[$keyCur] = $this->reCreateArrayRecursiveWorker($keys, $value, $existingArr[$keyCur]);
return $existingArr;
}
} else {
// If Key does not exists in current Array
// Check deepest
if (empty($keys)) {
//Return the Key => value mapping
$existingArr[$keyCur] = $value;
return $existingArr;
} else {
// Add the key
$existingArr[$keyCur] = $this->reCreateArrayRecursiveWorker($keys, $value, []);
return $existingArr;
}
}
}
Is there a better more elegant way of doing this, maybe http_build_query or something else I am not aware of.
Sandbox link -> http://sandbox.onlinephpfunctions.com/code/50b3890e5bdc515bc145eda0a1b34c29eefadcca
Flattening:
Your approach towards recursion is correct. I think we can make it more simpler.
We loop over the array. if the value is an array in itself, we recursively make a call to this new child subarray.
This way, we visit each key and each value. Now, we are only left to manage the keys to assign them when adding to our final resultant array, say $arrayScalar.
For this, we make a new function parameter which takes the parent key into account when assigning. That's it.
Snippet:
$arrayScalar = [];
function flatten($array,&$arrayScalar,$parent_key){
foreach($array as $key => $value){
$curr_key = empty($parent_key) ? $key : $parent_key . '[' . $key . ']';
if(is_array($value)){
flatten($value,$arrayScalar,$curr_key);
}else{
$arrayScalar[$curr_key] = $value;
}
}
}
flatten($array,$arrayScalar,'');
var_export($arrayScalar);
Demo: http://sandbox.onlinephpfunctions.com/code/1e3092e9e163330f43d495cc9d4acb672289a987
Unflattening:
This one is a little tricky.
You might have already noticed that the keys in the flattened array are of the form key1[key2][key3][key4] etc.
So, we collect all these individually in a new array, say $split_key. It might look like this.
array (
'key1',
'key2',
'key3',
'key4',
)
To achieve the above, we do a basic string parsing and added in-between keys to the array whenever we reach the end of the key string or [ or ].
Next, to add them to our final resultant array, we loop over the collected keys and check if they are set in our final array. If not so, set them. We now pass child array reference to our temporary variable $temp. This is to edit the same copy of the array. In the end, we return the result.
Snippet:
<?php
function unflatten($arrayScalar){
$result = [];
foreach($arrayScalar as $key => $value){
if(is_int($key)) $key = strval($key);
$split_key = [];
$key_len = strlen($key);
$curr = '';
// collect them as individual keys
for($i = 0; $i < $key_len; ++$i){
if($key[ $i ] == '[' || $key[ $i ] == ']'){
if(strlen($curr) === 0) continue;
$split_key[] = $curr;
$curr = '';
}else{
$curr .= $key[ $i ];
}
if($i === $key_len - 1 && strlen($curr) > 0){
$split_key[] = $curr;
}
}
// collecting them ends
//add them to our resultant array.
$temp = &$result;
foreach($split_key as $sk){
if(!isset($temp[ $sk ])){
$temp[ $sk ] = [];
}
$temp = &$temp[$sk];
}
$temp = $value;
}
return $result;
}
var_export(unflatten($arrayScalar));
Demo: http://sandbox.onlinephpfunctions.com/code/66136a699c3c5285eed3d3350ed4faa5bbce4b76

PHP - How to dynamically input multi-dimensional arrays only knowing the keys?

I have a multi-dimensional array with key value pairs. Some of the values of the keys are arrays, and some of the values in that array are arrays as well. It is only 3 branches deep (for now), but I am trying to recursively loop through the array, save the key for each level of the branch, and create a new array when it reaches a string that also mimics the structure of the original array. When it reaches a string, there should be a new object instantiation for each value.
The code below sort of does this, but only creates a flat array.
foreach ($data as $key => $value) {
if (!is_array($value)) {
$a_objects[$key] = new Component([$key], $value);
} else {
foreach ($value as $valueKey => $valueValue) {
if (!is_array($valueValue)) {
$a_objects[$key . "_" . $valueKey] = new Component([$key, $valueKey], $valueValue);
} else {
foreach ($valueValue as $k => $v) {
$a_objects[$key . "_" . $valueKey . "_" . $k] = new Component([$key, $valueKey, $k], $v);
}
}
}
}
Here is my own best attempt at this but it does not seem to save into the b_objects after some testing it does not seem to be saving the object instances.
$b_objects = [];
function create_r($data, $id)
{
foreach ($data as $key => $value) {
if (is_array($value) == true) {
array_push($id, $key);
create_r($value, $id);
} else {
array_push($id, $key);
$b_objects[$key] = new Component($id, $value);
$id = [];
}
}
}
$id = [];
create_r($data, $id);
echo "this is b_objects";
echo "<pre>";
print_r($b_objects);
echo "</pre>";
I verified that $id array contains the right "keys". I feel like this solution is pretty close but I have no idea how to use my $id array to mimic the structure of the $data.
I want to be able to say $b_objects[$level1key][$level2key][$level3key]...[$leveln-1key] = new Component...
Thanks!
I suggest you pass $b_objects by reference to your create_r() function. Otherwise each function call will instantiate a new $b_objects variable which is not used anywhere in the code afterwards.
Passing by reference allows the function to actually change the input array. Notice the & sign in the function declaration.
function create_r($data, $id, &$res)
{
foreach ($data as $key => $value) {
if (is_array($value) == true) {
array_push($id, $key);
create_r($value, $id, $res);
} else {
array_push($id, $key);
$res[$key] = new Component($id, $value);
$id = [];
}
}
}
$id = [];
$b_objects = [];
create_r($data, $id, $b_objects);

store nested object chain in a variable

so lets say i have a object like this
{
"status": "AlreadyVerified"
}
and i want to store propert key in variable so i can access property using that variable like
$key = 'status';
echo $object->$key
but what if i have a nested object like
{
"extra_info": {#305 ▼
+"status": "AlreadyVerified"
}
}
this wouldn't work
$key = 'extra_info->status';
echo $object->$key
how can i store nested object chain in a variable so i can access its property using that variable ?
preferably some way that works for both nested and flat objects (i guess that's what the're called !)
It can be possible by write helper function like this:
function deepFind($o, $key) {
$key = explode('->', $key);
$value = $o;
foreach ($key as $i=>$k) {
if (is_object($value) && isset($value->{$k})) {
$value = $value->{$k};
} elseif (is_array($value) && isset($value[$k])) {
$value = $value[$k];
} elseif ($i == count($key) - 1) {
$value = null;
}
}
return $value;
}
Usage:
$o = (object)[
"extra_info" => (object)[
"status" => "AlreadyVerified"
]
];
echo deepFind($o, 'extra_info->status');
Online demo
This is one way to do it, albeit potentially insecure depending on where $key comes from:
<?php
$object = new stdClass();
$object->extra_info = new stdClass();
$object->extra_info->status = 'AlreadyVerified';
$key = 'extra_info->status';
eval( 'echo $object->'.$key.';' );
Output:
AlreadyVerified
Additionally, if you wanted to parse $key then you could use a recursive function to access the nested value.

How to access object property of indeterminate depth

I have an object with data stored at multiple levels ( a JSON-decoded document ) like this:
$db = (object) array(
'simple_property' => 'value',
'complex_property' => (object) array(
'key' => 'value',
'nested' => (object) array(
'key' => 'value'
)
)
);
I want to be able to access and update data at any depth via reference. Example:
$db->{ $key } = $new_value
If $key is equal to 'simple_property', that works. But if $key is equal to 'complex_property->nested->key', it doesn't. Is there a way to accomplish what I want to, or am I looking at it incorrectly?
I don't think you can get it to work that way. You'll have to create a function (or class method) to do something like that. As an example:
function getRecursiveProperty($object, $path)
{
$array = explode('->', $path);
if (empty($array))
{
return NULL;
}
foreach ($array as $property)
{
if (!isset($object->$property))
{
return NULL;
}
if (!is_object($object->$property))
{
return $object->$property;
}
$object = $object->$property;
}
return $object->$property;
}
function setRecursiveProperty($object, $path, $value)
{
foreach (explode('->', $path) as $property)
{
if (!isset($object->$property))
{
return FALSE;
}
if (!is_object($object->$property))
{
$object->$property = $value;
return TRUE;
}
$object = $object->$property;
}
return FALSE;
}
$key = 'complex_property->nested->key';
echo getRecursiveProperty($db, $key); // value
setRecursiveProperty($db, $key, 'new_value');
echo getRecursiveProperty($db, $key); // new_value
Why you don't use $db = json_decode($json, true); instead of $db = json_decode($json);?
In this way you return an associative array instead of an object and you will not have these problems anymore.
json_decode ( string $json , bool $assoc)
json
The json string being decoded.
This function only works with UTF-8 encoded strings.
assoc
When TRUE, returned objects will be converted into
associative arrays.
More info: http://php.net/manual/en/function.json-decode.php

Accessing Certain JSON Elements

How would I go about accessing these elements from a Shopify JSON in PHP
JSON:
{
"orders":[
{ "note_attributes":[
{
"name":"field1",
"value":"xxxxxxxxxxxx"
},
{
"name":"field2",
"value":"xxxxxx"
},
{
"name":"field3",
"value":"xxxxxx"
}
],
This is just snippet of the object. How would I access each value and assign it to the name? Eg this $req['account_id'] would equal the value for that name tag. This is how am trying to do it but it's not working:
foreach ($order['note_attributes'] as $attributes) {
$req = array();
$req[$attributes->name] = $attributes[$attributes->value];
}
I would then like to echo $req['account_id'] and that would = xxxxxxxxx#gmail.com but it's not working any suggestions or fixes?
When iterating over Objects properties, one approach is to use (as you have) foreach
// accessing property
foreach ($objects as $object) {
echo $object->property;
}
// accessing key-value pair
foreach ($object as $key => $value) {
echo "$key => $value\n";
}
An example:
$attributes = array();
.
.
foreach($note_attributes as $note_attribute) {
$key = $note_attribute['name'];
$value = $note_attribute['value'];
$attributes[$key] = $value;
}
The structure should be in key-value pairs. ie.
attributes [
"account1234" => "abc#xxx.com",
"account_password" => "asdasdasd"
]
Hope this helps.
Update: as Roopendra mentioned, json_decode() with a TRUE flag with return an associative array, else stdClass.

Categories