0

I have an array of the type:

array(4) {[0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4"}

And I need to make a multidimensional array like this:

$data = array(
   array(
      $id => '1' ,
   ),
   array(
      '$id' => '2' 
   )
   ),
   array(
      '$id' => '3' 
   )
   ),
   array(
      '$id' => '4' 
   )
);

Where $id has constant value(let's say 6) and the value of the multidimensional array is the value from the first array.

Thanks

Leron

2 Answers 2

1
$data = array();

for($i=0;$i<count($firstArray);++$i) {
    $data[] = array('$id' => $firstArray[$i]);
}

Did you mean this?

Edit:

Sorry, I misread you question.

$data = array(); $id = '6';

for($i=0;$i<count($firstArray);++$i) {
    $data[] = array($id => $firstArray[$i]);
    // Or:
    // $data[] = array();
    // $data[$i][$id] = $firstArray[$i];
}

Edit 2:

Here is a testcode:

<?php

$firstArray = array("1", "2", "3", "4");

$data = array(); $id = '6';

for($i=0;$i<count($firstArray);++$i) {
    $data[] = array($id => $firstArray[$i]);
    // Or:
    // $data[] = array();
    // $data[$i][$id] = $firstArray[$i];
}

print_r($data);

?>

And the output:

Array
(
    [0] => Array
        (
            [6] => 1
        )

    [1] => Array
        (
            [6] => 2
        )

    [2] => Array
        (
            [6] => 3
        )

    [3] => Array
        (
            [6] => 4
        )

)
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, simple, and I hope this is what I need. Gonna try it ASAP and accept if it works
Yeah, just put it in my code, exactly what I need. Thanks! Have a nice day!
0

You mean you want the same index and then it's child like this as you did :

$data = array(
   array(
      $id => '1' ,
   ),
   array(
      '$id' => '2' 
   )
   ),
   array(
      '$id' => '3' 
   )
   ),
   array(
      '$id' => '4' 
   )
);

Then, how can be the same index of the array every time for data mapping? If you want the data in one array then make something like

$data = array($id => array(
1,2,3,4 // IT WILL BE HOLDED BY YOUR $id
));

And, as you said for Creating multidimensional array byt merging values then go through array_merge()

2 Comments

I'm not sure what I should use for achieving the result I post in my question, the reason to want the same id is because i'm gonna use it for inserts using CodeIgniters active records and it requires array like this.
As you said that there is constant that holds the associative array. It depends what you're gonna do. If you want a multiple indexes having multiple values then do $k = 0; foreach($basearr as $key => $valuesArray) { $data[$k] = array($valuesArray); // $valuesArray would be 1,2,3,4,5. .....1 $k++; }

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.