0

Good day. I am new at php and I have a small problem. I have an array.. I want to change stucture of it .. object_type sometimes the same.. I want to have multidimensional array with object_type a key and value all arrays with this object_type.

[0] => Array
    (
        [initiator_id] => 259
        [object_type] => 1
        [object_id] => 905
        [date] => 2021-11-16 06:24:16
  
    )

[1] => Array
    (
        [initiator_id] => 259
        [object_type] => 1
        [object_id] => 905
        [date] => 2021-11-16 04:54:54
    )

[2] => Array
    (
        [initiator_id] => 259
        [object_type] => 1
        [object_id] => 905
        [date] => 2021-11-16 04:53:58
    )

[3] => Array
    (
        [initiator_id] => 219
        [object_type] => 2
        [object_id] => 915
        [date] => 2021-11-16 04:53:58
    )
5
  • 1
    Welcome! Stack Overflow is not a code writing service. We are always glad to help and support new coders but you need to help yourself first. You are expected to try to write the code yourself. Please read How to create a Minimal, Reproducible Example and How do I ask a good question?. Commented Nov 16, 2021 at 14:13
  • 1
    Have a look at this topic: How can I create an array from the values of another array's key?. Does this help you along? Commented Nov 16, 2021 at 14:17
  • Lvn W, yes .. I tried several times.. it did not help... Commented Nov 16, 2021 at 14:27
  • something like this might help. Commented Nov 16, 2021 at 14:35
  • berend, thanks a lot. Commented Nov 16, 2021 at 14:59

1 Answer 1

1

Here you can find how to create Multidimensional Arrays. Click here PHP Multidimensional Arrays

And to learn PHP language www.w3schools.com is a very good site. You can find almost every example briefly explained here.

I hope this example may solve your issue.

<?php

$obj = array("initiator_id"=>"35", "object_type"=>"37", "object_id"=>"43");
$obj_2 = array("initiator_id"=>"135", "object_type"=>"237", "object_id"=>"343");

$array_list = array (
          array("Test 1",22,18),          
          array("Test 2",5,2),
          array("Test 3",17,15)
        );
        
$array_list[1]["new"] = $obj_2; 
  
        
$result = array_merge($array_list, $obj);      
    
  echo "<Pre>";
  print_r($result);
  echo "</pre>";

?>

Output:

Array
(
    [0] => Array
        (
            [0] => Test 1
            [1] => 22
            [2] => 18
        )

    [1] => Array
        (
            [0] => Test 2
            [1] => 5
            [2] => 2
            [new] => Array
                (
                    [initiator_id] => 135
                    [object_type] => 237
                    [object_id] => 343
                )

        )

    [2] => Array
        (
            [0] => Test 3
            [1] => 17
            [2] => 15
        )

    [initiator_id] => 35
    [object_type] => 37
    [object_id] => 43
)
Sign up to request clarification or add additional context in comments.

3 Comments

If you have the answer for the provided example, please share exact code snippet or at least describe the algorithm that will solve it. Adding links for generic documentation is not really helpful.
@AlexG I just edit the Answer. is this example helpful?
I think what you said, is perfectly valid, and shows how to manipulate arrays in a multi-dimensional way, but the question was quite vague, and I'm not sure that's what he meant. From what I can infer he was asking about the key type, and implying it could be an object, which is nonsense. What you put, implies a sensible question, which this was not. Good job for trying to rationalise the irrational though.

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.