0

If a have an array:

Array
(
    [1-title1] => Array
        (
            [0] => title = title erer
            [1] => 1-title1
            [2] => content = content 1
        )

    [2-title2] => Array
        (
            [0] => title = title dffgfghfdg
            [1] => 2-title2
            [2] => content = content 2
        )

and I want to get array:

Array
(
    [1-title1] => Array
        (
            [title] =>title erer
            [1] =>title1
            [content] =>content 1
        )

    [2-title2] => Array
        (
            [title] =>title dffgfghfdg
            [2] => title2
            [content] =>content 2
        )

What should I do?

0

3 Answers 3

1

A solution without references and no implicit key names.

foreach ($array as $key => $innerArray) {
    foreach ($innerArray as $innerKey => $value) {
        if (false !== strpos($value, ' = ')) {
            unset($array[$key][$innerKey]);
            list($innerKey, $value) = explode(' = ', $value, 2);
            $array[$key][$innerKey] = $value;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0
foreach($array as &$title){
    $newTitle = array();
    $titleName = explode(' = ', $title[0] , 2);
    $newTitle['title'] = end($titleName);
    $titleNum = explode('-', $title[1] , 2);
    $newTitle[$titleNum[0]] = $titleNum[1];
    $titleContent = explode(' = ', $title[2] , 2);
    $newTitle['content'] = end($titleContent);  
    $title = $newTitle;
}

Comments

0

try this

foreach($array as $ky=>$val)  {
  echo $ky;
  var_dump(array_keys($val));
}

7 Comments

Why iterate? var_dump(array_keys($array)).
i am try but i got 1-title1 2-title2 3-title3 4-title4
@Kenaniah because array_keys returns only parent key and if he wants to get title etc, he ca use array_keys
@Robin, I get that, but your answer only references the keys. What you literally posted didn't use values at all.
@Robin, if I had a penny for every person I met who was as rude as you are, I'd probably have a penny.
|

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.