0

I have an array that looks like this,

[0] => Array
    (
        [youtube_showreel_url_1] => youtube1.com
        [youtube_showreel_description] => youtube1.com - desc
    )

[1] => Array
    (
        [youtube_showreel_url_2] => youtube2.com
        [youtube_showreel_description] => youtub2.com - desc
    )

[2] => Array
    (
        [youtube_showreel_url_3] => youtube3.com
        [youtube_showreel_description] => youtube3.com - desc
    )

[3] => Array
    (
        [youtube_showreel_url_4] => youtube4.com
        [youtube_showreel_description] => youtube4.com - desc
    )

[4] => Array
    (
        [youtube_showreel_url_5] => youtube5.com
        [youtube_showreel_description] => youtube5.com - desc
    )

Is it possible with PHP to turn it into something that looks like this?

[0] => Array ( 
        [youtube_showreel_url_1] => youtube1.com  
        [youtube_showreel_description] => youtube1.com - desc 
        [youtube_showreel_url_2] => youtube2.com
        [youtube_showreel_description] => youtub2.com - desc
        [youtube_showreel_url_3] => youtube3.com
        [youtube_showreel_description] => youtube3.com - desc
        [youtube_showreel_url_4] => youtube4.com
        [youtube_showreel_description] => youtube4.com - desc
        [youtube_showreel_url_5] => youtube5.com
        [youtube_showreel_description] => youtube5.com - desc
    )

Can explode it or run it through a loop or something?

1
  • That should be pretty easy to do, but your result has many of the same key (i.e., youtube_showreel_description) so they'd overwrite. Could you number those like you did with the url_# ones? Commented Nov 29, 2011 at 16:40

6 Answers 6

1

Assuming your original data is held in a variable called $input:

// This will hold the result
$result = array();

foreach ($input as $index => $item) { // Loop outer array
  foreach ($item as $key => $val) { // Loop inner items
    $result[$key] = $val; // Create entry in $result
  }
}

// Show the result
print_r($result);

However, your input has the same key appearing in it more than once, and the later values will overwrite the first one. So you might want to do something like this:

foreach ($input as $index => $item) { // Loop outer array
  foreach ($item as $key => $val) { // Loop inner items
    $result[$key.$index] = $val; // Create entry in $result
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0
<?php

$aNonFlat = array(
    1,
    2,
    array(
        3,
        4,
        5,
        array(
            6,
            7
        ),
        8,
        9,
    ),
    10,
    11
);

$objTmp = (object) array('aFlat' => array());

array_walk_recursive($aNonFlat, create_function('&$v, $k, &$t', '$t->aFlat[] = $v;'), $objTmp);

var_dump($objTmp->aFlat);

/*
array(11) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
  [4]=>
  int(5)
  [5]=>
  int(6)
  [6]=>
  int(7)
  [7]=>
  int(8)
  [8]=>
  int(9)
  [9]=>
  int(10)
  [10]=>
  int(11)
}
*/

?>

source : http://ca.php.net/manual/fr/function.array-values.php#86784

Comments

0

array keys have to have unique names meaning that you wouldn't be able to have multiple youtube_showreel_description keys or else you just overwrite the value. You could rename the key to something like youtube_showreel_description_NN where NN is the number of the description similar to how you have the url.

Comments

0

there would be a collision on "youtube_showreel_description"

you should think of a better data structure for that

I would suggest you retain its multidimensional array structure

Array
(
    [0] => Array
        (
                [url] => youtube1.com
                [description] => youtube1.com - desc
        )
    [1] => Array
        (
                [url] => youtube2.com
                [description] => youtube2.com - desc
        )
    [2] => Array
        (
                [url] => youtube3.com
                [description] => youtube3.com - desc
        )
    [3] => Array
        (
                [url] => youtube1.com
                [description] => youtube3.com - desc
        )
    [4] => Array
        (
                [url] => youtube1.com
                [description] => youtube4.com - desc
        )
)

If you like it that way, I think its cleaner, and easier to parse

Comments

0

Not exactly, the [youtube_showreel_description] would overwrite itself after each declaration, you would need to use a unique identifier for each key. If the keys are not important you can just loop through your existing array with a foreach loop and set a regular iterative key and "know" that the array comes in sets where the first element is the url and the second is the description.

Edit: If the keys aren't important, you could use the url as your key and description as your value, this will make it easier to iterate over as each element pertains to only one item.

Comments

0

As everyone has already mentioned, you're going to get a collision on 'youtube_showreel_description'. How about something like this?:

// values (stays the same - no need to reformat)
$values = array(
  array(
    'youtube_showreel_url_1' => 'youtube1.com',
    'youtube_showreel_description' => 'youtube1.com desc'
  ),
  array(
    'youtube_showreel_url_2' => 'youtube2.com',
    'youtube_showreel_description' => 'youtube2.com desc'
  ),
  array(
    'youtube_showreel_url_3' => 'youtube3.com',
    'youtube_showreel_description' => 'youtube3.com desc'
  ),
  array(
    'youtube_showreel_url_4' => 'youtube4.com',
    'youtube_showreel_description' => 'youtube4.com desc'
  )
);

// the good stuff
$result = array_map(function($v) {
  return array(
    'url'         => array_shift($v),
    'description' => $v['youtube_showreel_description']
  );
}, $values);


// result
array(4) {
  [0]=>
  array(2) {
    ["url"] => string(12) "youtube1.com"
    ["description"] => string(17) "youtube1.com desc"
  }
  [1]=>
  array(2) {
    ["url"] => string(12) "youtube2.com"
    ["description"] => string(17) "youtube2.com desc"
  }
  [2]=>
  array(2) {
    ["url"] => string(12) "youtube3.com"
    ["description"] => string(17) "youtube3.com desc"
  }
  [3]=>
  array(2) {
    ["url"] => string(12) "youtube4.com"
    ["description"] => string(17) "youtube4.com desc"
  }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.