2

I'm getting an array from $json_a = json_decode($filecontent, true):

Array (
        [meta_titles] => Array ( 
                [0] => Just another site 
                [1] => Test test 
                [2] => This is a test
        )
        [tag_titles] => Array ( 
                [0] => Test 1 
                [1] => Value 2 
                [2] => String 3
        )
)

I would like to modify the array as following:

Array (
        [meta_titles] => Array ( 
                Just another site => Just another site 
                Test test  => Test test 
                This is a test => This is a test
        )
        [tag_titles] => Array ( 
                Test 1 => Test 1 
                Value 2  => Value 2 
                String 3 => String 3
        )
    )

So values become the keys. Would somebody have an idea?

EDIT: My code so far:

$json_a = json_decode($filecontent, true);

$newjson = array();
foreach($json_a as $category) {
    $newjson[$category] = array_combine(array_values($category), $category);
}                           
$json = json_encode($newjson, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);

Thanks a lot

2
  • Euh your link brings me to "What does “1” mean at the end of a php print_r statement?" which has nothing to do with the question. Commented Apr 3, 2016 at 16:29
  • Wups yes, somehow I didn't copied the correct link. I mean this one: stackoverflow.com/q/5422955/3933332 just use the same subArray for keys and values. Commented Apr 3, 2016 at 16:30

3 Answers 3

3

Solution using array_combine and array_values functions. To change array value in place mark it as reference with &:

$json = [ 
    'meta_titles' => [ 0 => 'Just another site' , 1 => 'Test test' , 2 => 'This is a test'],
    'tag_titles' => [ 0 => 'Test 1' , 1 => 'Value 2' , 2 => 'String 3']    
];

foreach($json as &$category) {
    $category = array_combine(array_values($category), $category);
}

$json = json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);

print_r($json);

The output:

{
    "meta_titles": {
        "Just another site": "Just another site",
        "Test test": "Test test",
        "This is a test": "This is a test"
    },
    "tag_titles": {
        "Test 1": "Test 1",
        "Value 2": "Value 2",
        "String 3": "String 3"
    }
}
Sign up to request clarification or add additional context in comments.

13 Comments

update your question according to this condition "there are other array names in it.", update the expected output, and I will extend my solution for you
@GuilVII, each parent key should be renamed to meta_titles , right (it's in your example)?
do you want to get same arrays with string keys but empty values?
wait, what does mean "need both"? the values are either filled or empty, or you want to mix up filling values randomly?
I would say that it should be a new question in SO. In that case, there must exist some preconditions, depending on which condition, I should decide which value would be empty, and which - should be filled
|
0

Try this:

$array = array (
    'meta_titles' => array ( 
            0 => 'Just another site',
            1 => 'Test test' ,
            2 => 'This is a test'
    )
);

$new_array['meta_titles'] = array();

foreach($array['meta_titles'] as $v) {
   $new_array['meta_titles'][$v] = $v;
}
echo "<pre>";
print_r($new_array); 

This gives:

Array
(
 [meta_titles] => Array
    (
        [Just another site] => Just another site
        [Test test] => Test test
        [This is a test] => This is a test
    )

)

Hope this helps.

1 Comment

Thanks for your answer. meta_title is supposed to be dynamic though...I tried: foreach($json_a as $category) { foreach($category as $string){ $json_a[$category][$string] = $string; } but it's not changing the key... }
0

You can try. reset($youArr); for get array like this

{ 
  Array {
          0 =》title
          1=》 description
           2 =》keywords
           }
}

And how to you can use array don't know keys?

2 Comments

Key is supposed to be equal to the value key => key. So json encode returns "Value":"Value".
@GuilVll if return 0,1,2. I can use this jast res[0] if value = value I need use loop for get each element...

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.