0

This is $multidimensional data result :

[
    {
        "2018-11-02": [
            "2"
        ]
    },
    {
        "2018-11-02": [
            "8",
            "3"
        ]
    }
    {
        "2018-11-21": [
            "11",
            "35",
            "94",
            "98",
            "163"
        ]
    },
]

$filter = [3,98,11]

How to remove object and value in $multidimensional where value not exist in $filter and after unset, the result will be turned into an object like the one below :

{
  "3": 2018-11-02,
  "98": 2018-11-21,
  "11" : 2018-11-21
}

In my case, I am using unserialize :

for($w=0;$w<count($multidimensional);$w++) {
      $hasilId2[] = (object) [
        $multidimensional[$w]->date=> unserialize($multidimensional[$w]->invoiceid)
      ];
    }

I've already try using loop :

foreach($multidimensional as $key => $value) {
      if(!in_array($key, $filter)) {
        unset($multidimensional[$key]);
      }
    }
4
  • What should happen if a number is contained in multiple dates? What should happen if a filter value does not exist in the array? Also please present your attempt of solving it. Commented May 3, 2021 at 4:17
  • If a number exists on multiple dates it will create the same object with different numbers and the same date. If the filter value does not exist, it will delete the object, and will only get the id and date available in the filter So the main goal is to change the multidimensional value $ to a key in the new variable with Fill in the value of the date that is in $ multidimensional, like the example result that I expect below Commented May 3, 2021 at 5:06
  • In my case, I am usingI unserialize, i've already update my question, the present my attemnt of soling it Commented May 3, 2021 at 5:07
  • Can you adjust your question with an example that shows the result with both cases (One multiple and one does not exist)? I'd need to see exactly how it should look, because I dont think I understand what " create the same object with different numbers" means. Commented May 3, 2021 at 5:40

1 Answer 1

2

You need a loop for the main array and a loop for the internal arrays

So it is better to put 2 loop inside each other

There is also no need to uset

You can put the correct results in a separate variable and then use it

You wanted the key to that array to be the final array value

So I used the key function to get the array key and gave the new value like this:

$result[array value] = [array key];

And finally I printed the new array:

$multidimensional = [
    [
        "2018-11-02" => [
            "2"
        ]
    ],
    [
        "2018-11-02" => [
            "8",
            "3"
        ]
    ],
    [
        "2018-11-21" => [
            "11",
            "35",
            "94",
            "98",
            "163"
        ]
    ],
];

$filter = [3, 98, 11];
$result = [];
foreach ($multidimensional as $val1) {
    foreach (array_values($val1)[0] as $key => $val2) {
        if (in_array($val2, $filter)) {
            $result[$val2] = key($val1);
        }
    }
}
print_r($result);

Result:

Array
(
    [3] => 2018-11-02
    [11] => 2018-11-21
    [98] => 2018-11-21
)
Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much, your answer really helped me I get a new lesson from your answer. Thankyou

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.