0

Need to create a multidimensional array by matching the keys in an array.

array 1:

[
 'slide_name_1'  => 'lorem ipsum',
 'slide_title_1' => 'lorem ipsum',
 'slide_name_2'  => 'lorem ipsum',
 'slide_title_2' => 'lorem ipsum',
]

I need to create this:

[0] => array (
       'slide_name_1'  => 'lorem ipsum 1',
       'slide_title_1' => 'lorem ipsum 1',
       )
[1] => array (
       'slide_name_2'  => 'lorem ipsum 2',
       'slide_title_2' => 'lorem ipsum 2',
       )

I was thinking of running some nested foreach loops and matching just the number portion of the keys (ex: substr($key, strrpos($key, '_') + 1);).

Of course this has proven to be more difficult than i had anticipated. Any suggestions would be greatly appreciated.

7
  • 1
    So, what is matching? Last number? And what is wrong with provided code for strrpos? Commented Mar 4, 2018 at 8:20
  • You could use explode('_', $key) and then use the last element of that. Commented Mar 4, 2018 at 8:22
  • If order and number of elements always the same, you can split array into chunks with array_chunk Commented Mar 4, 2018 at 8:24
  • I was thinking of matching just the number portion of the keys, yes. strrpos works fine to pull them. Where i run into a wall is creating a new multidem array. Commented Mar 4, 2018 at 8:25
  • 1
    @kash101 Create an associative array, and use that number as the key. Commented Mar 4, 2018 at 8:31

1 Answer 1

2

You are on the right track. No need for nested foreach loops though. Just use one.

Like:

$arr = array (
 'slide_name_1'  => 'lorem ipsum',
 'slide_title_1' => 'lorem ipsum',
 'slide_name_2'  => 'lorem ipsum',
 'slide_title_2' => 'lorem ipsum',
);

$result = array();
foreach( $arr as $key => $val ){
    $k = substr($key, strrpos($key, '_') + 1); //Get the number of the string after _

    //Normally, this line is actually optional. But for strict PHP without this will create error.
    //This line will create/assign an associative array with the key $k
    //For example, the $k is 1, This will check if $result has a key $k ( $result[1] ) 
    //If not set, It will assign an array to $result[1] = array()
    if ( !isset( $result[ $k ] ) ) $result[ $k ] = array(); //Assign an array if $result[$k] does not exist

    //Since you already set or initiate array() on variable $result[1] above, You can now push $result[1]['slide_name_1'] = 'lorem ipsum 2';
    $result[ $k ][ $key ] = $val . " " . $k; //Push the appended value ( $value and the number after _ )
}

//Return all the values of an array
//This will convert associative array to simple array(index starts from 0)
$result = array_values( $result ); 

This will result to:

Array

(
    [0] => Array
        (
            [slide_name_1] => lorem ipsum 1
            [slide_title_1] => lorem ipsum 1
        )

    [1] => Array
        (
            [slide_name_2] => lorem ipsum 2
            [slide_title_2] => lorem ipsum 2
        )

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

6 Comments

...and this will be O(n). It's much better than nested loops(minimum O(n^2))
Awesome.....I was on my forth nested foreach just to get semi close. This works perfectly. Thanks Much
Happy to help @kash101
@kash101 No worries. :) Please check my explanation above. Please let me know if you have questions
@Eddie Got it. Was trying do do something similar in one of the many nested loops by getting the key from the last element added to the array. was the closest i got. Of course i also ended up with 65 indexes on that attempt. Thanks again for the script and especially the explanation. Much appreciated!
|

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.