0

I'm trying to create a function that will assign new values to a specific indexes in a multidimensional array:

  1. I Have the array that looks like this:

    data[i]['checkin'];

    data[i]['checkout'];

    data[i]['content'][x]['price'];

    data[i]['content'][x]['profit'];

    data[i]['content'][x]['exchangerate'];

first parameter of my function will get the array, And second parameter will get the indexes that I want to redefine:

For example:

 function defineNewValues(&$arr, $keys) {
     //logic
 }

Call the function:

defineNewValues($myArray, [
'data.*.content.*.price' => 0,
'data.*.content.*.profit => 0,
]);

Im beliving that recursion is the key for my problem , But not really know how to solve it.

Thank You.

Eyal Biton
  • 11
  • 4
  • With the $keys array like that, you will have to write a parser to process that and convert it into some actual code that will run. Do you really need that when a simple couple of foreach loops will do it quite easily – RiggsFolly Oct 21 '21 at 15:12
  • I want the function to be versatile and can solve many cases different stracture of array, about the format .. im thinking to use explode the string by thr dot seperator – Eyal Biton Oct 21 '21 at 15:16
  • Then you had better start writing it – RiggsFolly Oct 21 '21 at 15:17
  • I tried more then 3 days with no success.. – Eyal Biton Oct 21 '21 at 15:30
  • For duplicate solution just use a string path and for new dynamic elements use `..` instead of `.*.`. as `'data..content..price'` – AbraCadaver Oct 21 '21 at 16:13

2 Answers2

0

could something like this be okay?

I only ask you to study this code not to implement it, for the simple reason that in the future you may have the same type of problem.


  function setValue($key,$value,&$array){
    $find_parts = explode(".", $key);
    $find = $find_parts[0]??null;
    if ($find!=null){
      if ($find == "*"){
        array_shift($find_parts);
        foreach($array as &$sub_array){
            setValue(implode(".",$find_parts),$value,$sub_array);
        }
      }else{
        if (count($find_parts)>1){
          if (array_key_exists($find,$array)){
            array_shift($find_parts);
            setValue(implode(".",$find_parts),$value,$array[$find]);
          }
        }else{
          if (array_key_exists($find,$array)){
            $array[$find] = $value;
          }
        }
      }
    }
  }

 function defineNewValues(&$arr, $keys) {
    foreach($keys as $key=>$value){
      setValue($key,$value,$arr);
    }
 }
 
 $myArray=[
    "data"=>[
        "a"=>[
            "content"=>[
                "aa"=>[
                    "price" => 3,
                    "profit" => 2,
                    "other" => 1
                ],
                "ab"=>[
                    "price" => 3,
                    "profit" => 2,
                    "other" => 2
                ]
            ]
        ],  
        "b"=>[
            "content"=>[
                "ba"=>[
                    "price" => 3,
                    "profit" => 2,
                    "other" => 4
                ],
                "bb"=>[
                    "price" => 3,
                    "profit" => 2,
                    "other" => 5
                ]
            ]
        ],
    ]    
 ];
 
 defineNewValues($myArray, [
    "data.*.content.*.price" => 0,
    "data.*.content.*.profit" => 0,
 ]);
 
 print_r($myArray);

/* OUTPUT
Array
(
    [data] => Array
        (
            [a] => Array
                (
                    [content] => Array
                        (
                            [aa] => Array
                                (
                                    [price] => 0
                                    [profit] => 0
                                    [other] => 1
                                )
                            [ab] => Array
                                (
                                    [price] => 0
                                    [profit] => 0
                                    [other] => 2
                                )
                        )
                )
            [b] => Array
                (
                    [content] => Array
                        (
                            [ba] => Array
                                (
                                    [price] => 0
                                    [profit] => 0
                                    [other] => 4
                                )
                            [bb] => Array
                                (
                                    [price] => 0
                                    [profit] => 0
                                    [other] => 5
                                )
                        )
                )
        )
)
*/
0

Because the keys you want to replace only occur at one level of the data, the solution doesn't really need to take the entire array structure into account. You can just replace every price and profit key.

array_walk_recursive($example, function(&$value, $key) {
    if (in_array($key, ['price', 'profit'])) {
        $value = 0;
    }
});

Based on your comment on the other answer, my opinion on the "correct and professional way" is that we should try to solve the problem in the simplest way possible, because simple solutions are easy to maintain.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • correct observation, what he needed was to understand how to approach a problem of this kind but I wrote him an example that he can use to understand how to do it. in all cases if he writes a nice code that takes into consideration all the cases it is not even to be maintained and is easy to recycle in the future. – Gianfrancesco Aurecchia Oct 21 '21 at 16:07
  • @GianfrancescoAurecchia I hope it did not seem like I meant to imply anything negative about your answer. That was not what I intended, and I do think understanding how to solve the problem as it was presented is also valuable. – Don't Panic Oct 21 '21 at 16:12
  • I'm sorry if I seemed rude, and indeed more points of view are better – Gianfrancesco Aurecchia Oct 21 '21 at 16:16
  • @GianfrancescoAurecchia Not at all. No worries! – Don't Panic Oct 21 '21 at 16:18